D.
Contest Balloons
         time
limit per test

3 seconds

        memory
limit per test

256 megabytes

input

standard input

output

standard output

One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams
with more balloons, increased by
1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed.

You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly
forbidden by the rules. The team is then disqualified and isn't considered in the standings.

A contest has just finished. There are
n teams, numbered
1 through
n. The
i-th team has
ti balloons and weightwi.
It's guaranteed thatti
doesn't exceedwi
so nobody floats initially.

Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously,
he can't give away more balloons than his team initially has.

What is the best place Limak can get?

Input

The first line of the standard input contains one integer
n (2 ≤ n ≤ 300 000) — the number of teams.

The
i-th of
n following lines contains two integers
ti andwi
(0 ≤ ti ≤ wi ≤ 1018) —
respectively the number of balloons and the weight of the
i-th team. Limak is a member of the first team.

Output

Print one integer denoting the best place Limak can get.

Examples
Input
8
20 1000
32 37
40 1000
45 50
16 16
16 16
14 1000
2 1000
Output
3
Input
7
4 4
4 4
4 4
4 4
4 4
4 4
5 5
Output
2
Input
7
14000000003 1000000000000000000
81000000000 88000000000
5000000000 7000000000
15000000000 39000000000
46000000000 51000000000
0 1000000000
0 0
Output
2
Note

In the first sample, Limak has
20 balloons initially. There are three teams with more balloons (32,40
and
45 balloons), so Limak has the fourth place initially. One optimal strategy is:

  1. Limak gives
    6 balloons away to a team with
    32 balloons and weight
    37, which is just enough to make them fly. Unfortunately, Limak has only
    14 balloons now and he would get the fifth place.
  2. Limak gives
    6 balloons away to a team with
    45 balloons. Now they have
    51 balloons and weight
    50 so they fly and get disqualified.
  3. Limak gives
    1 balloon to each of two teams with
    16 balloons initially.
  4. Limak has
    20 - 6 - 6 - 1 - 1 = 6 balloons.
  5. There are three other teams left and their numbers of balloons are
    40,
    14 and
    2.
  6. Limak gets the third place because there are two teams with more balloons.

In the second sample, Limak has the second place and he can't improve it.

In the third sample, Limak has just enough balloons to get rid of teams
2,
3 and
5 (the teams with
81 000 000 000,
5 000 000 000 and
46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team6 and team

7).

题目大意:有n个队伍,每个队伍有t个气球,重量为w,当气球个数超过重量时就飞走。要将自己的气球给别人,使得自己的名次达到最大。

分析:用结构体,将其按气球数量从大到小排序,气球数量相同时按重量与气球差值大小从小到大排,再使用优先队列求出。

#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
struct text{
long long num, wei;
long long dis;
};
struct text_cmp{
bool operator()(text a, text b)
{
return a.dis > b.dis;
}
};
bool cmp(text& a,text& b)
{
if (a.num == b.num)return a.dis<b.dis;
else return a.num>b.num;
}
text a[300000];
priority_queue<text,vector<text>,text_cmp>q;
int main()
{
int n,i;
scanf("%d", &n); for (i = 0; i < n;i++)
{
scanf("%lld %lld", &a[i].num, &a[i].wei);
a[i].dis = a[i].wei - a[i].num+1; } sort(a + 1, a + n, cmp);
for (i = 1; i<n; i++)
{
if (a[i].num>a[0].num)
{
q.push(a[i]);
}
else
{
break;
}
}
int temp=i, min=i;
int j=i;
while (!q.empty())
{
text c;
c = q.top();
a[0].num -= c.dis;
q.pop();
if (a[0].num < 0)
break;
temp--;
for (; j < n; j++)
{
if (a[j].num>a[0].num)
{
q.push(a[j]);
temp++;
}
else
{
break;
}
min = min<temp ? min : temp;
} }
//printf("%d %d\n", min, temp);
if (temp > min)
printf("%d\n", min);
else
printf("%d\n", temp);
return 0;
}

CodeForces - 725D Contest Balloons 贪心的更多相关文章

  1. codeforces 725D . Contest Balloons(贪心+优先队列)

    题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...

  2. Canada Cup 2016 D. Contest Balloons 好题。优先队列 + 简单贪心

    http://codeforces.com/contest/725/problem/D 这题一看就是贪心的了,w - t最小的那个,肯定是优先打死. 但是一直都不会写,为什么呢,因为这个太像二分答案了 ...

  3. Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest(贪心)

    题目链接:https://codeforces.com/contest/1265/problem/C 题意 从大到小给出 $n$ 只队伍的过题数,要颁发 $g$ 枚金牌,$s$ 枚银牌,$b$ 枚铜牌 ...

  4. Contest Balloons

    Contest Balloons 题目链接:http://codeforces.com/problemset/problem/725/D 贪心+枚举 每次在排名在自己前面的选出w-t值最小的送气球,更 ...

  5. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  6. Codeforces 1154D - Walking Robot - [贪心]

    题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...

  7. Codeforces 830A. Office Keys (贪心二分 or DP)

    原题链接:http://codeforces.com/contest/830/problem/A 题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后 ...

  8. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  9. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

随机推荐

  1. subtle:有趣的伪平铺式窗口管理器

    Author:吴吉庆 email: jiqingwu@gmail.com home:http://hi.baidu.com/jiqing0925 create:2011-02-19 update:20 ...

  2. CVE-2017-12149JBoss 反序列化漏洞利用

    CVE-2017-12149 漏洞描述 互联网爆出JBOSSApplication Server反序列化命令执行漏洞(CVE-2017-12149),远程攻击者利用漏洞可在未经任何身份验证的服务器主机 ...

  3. Css之备忘录~

    1.background-size:contain/cover 两者的作用都是一样,一般用于设置小图标作为背景图,两者的区别是: Contain:不会失真     cover:会失真 2.a标签里面如 ...

  4. ASP.NET 网站部署到IIS上如何进行调试

    1:在一个网站成功部署后,有可能会遇到一些错误,但是又不能直接看出错误源(如果能在源程序里下断点进行调试就好了,这样就能准确的找出错误代码),下面介绍如何在一个已经部署的网站上进行断点调试(前提有网站 ...

  5. 钉钉头像大小设置 阿里cdn尺寸截取参数设置

    默认api的接口返回的avatar字段,是原始图片大小字段,尺寸和空间都是原始大小,如果想节省流量或统一尺寸,可以用阿里cdn自带的尺寸截取功能, 比如钉钉头像 avatar字段 返回值为原始大小ht ...

  6. 关于UDP数据报引发“异步错误”的疑问

    在UNP卷一第三版的第8章8.9小节中说到:如果udp服务器没有启动,udp客户端在使用sendto发送一行文本后,将会等待一个永远也不会出现的应答从而阻塞在recvfrom调用上. 由于服务器段不存 ...

  7. Wannacry样本取证特征与清除

    一.取证特征 1)网络域名特征 http://www.iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com 2)文件特征 母体文件 mssecsvc.exe c: ...

  8. 01-Coredump核心转存&&Linux程序地址分析【转】

    转自:http://www.itwendao.com/article/detail/404132.html 目录(?)[-] 一Core Dump核心转存 二Linux程序地址分析 一Core Dum ...

  9. LINUX修改、增加IP的方法,一张网卡绑定多个IP/漂移IP【转】

    临时增加IP命令:ifconfig eth0:1 ip地址 netmask 子网码 broadcast 广播地址 gateway 网关  ifconfig eth0:1 10.1.104.65 net ...

  10. 形参前的&&啥意思?

    C++2011标准的 右值引用 语法 去搜索“c++11右值引用” 右值引用,当传入临时对象时可以避免一次拷贝. 右值引用.举个例子 C/C++ code   ? 1 2 3 4 5 6 7 8 // ...