前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Codeforces】1217B - Zmei Gorynich

【Codeforces】1217B - Zmei Gorynich

作者头像
喜欢ctrl的cxk
发布2019-11-08 15:49:21
5550
发布2019-11-08 15:49:21
举报
文章被收录于专栏:Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/100573092

Problem Description:

You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!

Initially Zmei Gorynich has xx heads. You can deal nn types of blows. If you deal a blow of the ii-th type, you decrease the number of Gorynich's heads by min(di,curX)min(di,curX), there curXcurX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows hihi new heads. If curX=0curX=0 then Gorynich is defeated.

You can deal each blow any number of times, in any order.

For example, if curX=10, d=7, h=10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX=10, d=11, h=100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.

Calculate the minimum number of blows to defeat Zmei Gorynich!

You have to answer t independent queries.

Input Specification:

The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.

The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 109) — the number of possible types of blows and the number of heads Zmei initially has, respectively.

The following n lines of each query contain the descriptions of types of blows you can deal. The ii-th line contains two integers di and hi (1 ≤ di,hi ≤ 109) — the description of the i-th blow.

Output Specification:

For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.

If Zmei Gorynuch cannot be defeated print −1.

Sample Input:

代码语言:javascript
复制
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100

Sample Output:

代码语言:javascript
复制
2
3
-1

解题思路:

题目大意是用n种攻击类型去杀一个有x个头的怪物,它没有脑袋了就会死掉,你攻击能伤害它d个头,只要它还活着就能长出h个头,请选择有效的攻击类型来用最少的攻击次数击败它。贪心算法,ans是最大有效伤害,maxd是斩杀线也是最大攻击伤害,需要注意最大有效伤害不一定是最大攻击伤害。若有效伤害不为正 说明伤害太低,无法击杀怪物。若怪物的脑袋数低于斩杀线,则能一次击杀怪物。若一次不能斩杀,剩下的脑袋就用有效伤害来计算,-1是因为当括号内的值恰好等于有效伤害ans时可以完成击杀,最后的+1是因为斩杀线maxd的那次攻击没有计算在内。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int n,x;
        cin >> n >> x;
        int d[n+1],h[n+1];
        int ans = -1,maxd = -1;   //ans最大有效伤害,maxd最大攻击伤害即斩杀线
        Up(i,1,n)
        {
            cin >> d[i] >> h[i];
            ans = max(ans,d[i]-h[i]);
            maxd = max(maxd,d[i]);
        }
        if(maxd >= x)   //低于斩杀线,可以一次击杀
        {
            cout << 1 << endl;
        }
        else if(ans <= 0)   //伤害太低,无法击杀
        {
            cout << -1 << endl;
        }
        else
        {    //x减去斩杀线maxd后,剩下的脑袋用最大有效伤害值来计算,-1是括号内的值刚好等于ans时可以击杀掉
            cout << (x-maxd+ans-1)/ans+1 << endl;   //+1是斩杀线那一刀
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档