http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351

题意:

思路:

要么先选low值大的,要么先选high值大的,分两种情况讨论。

每次只要选了的low值和>=x或者c-未选的high值和>=x就肯定满足了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = +; int n,c,x,lowtot,hightot; struct node
{
int low,high;
bool operator< (const node& rhs) const
{
return low > rhs.low;
}
}a[maxn]; bool cmp(node a, node b)
{
return a.high>b.high;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
lowtot = hightot = ;
scanf("%d%d%d",&n,&c,&x);
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].low,&a[i].high);
lowtot += a[i].low;
hightot += a[i].high;
}
int sum = ;
int ans = ;
sort(a+,a+n+);
int tmp = hightot;
for(int i=;i<=n;i++)
{
sum += a[i].low;
ans++;
hightot -= a[i].high;
if(sum>=x || c-hightot>=x) break;
} sum = ;
int ans2 = ;
sort(a+,a+n+,cmp);
hightot = tmp;
for(int i=;i<=n;i++)
{
sum += a[i].low;
ans2++;
hightot -= a[i].high;
if(sum>=x || c-hightot>=x) break;
}
printf("%d\n",min(ans,ans2));
}
return ;
}

51nod 1351 吃点心(贪心)的更多相关文章

  1. 1351 topcoder 吃点心

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 先按low从大到小贪心再high从小到大贪心 #pragma c ...

  2. Cow and Snacks(吃点心--图论转换) Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)

    题意:https://codeforc.es/contest/1209/problem/D 有n个点心,有k个人,每个人都有喜欢的两个点心,现在给他们排个队,一个一个吃,每个人只要有自己喜欢的点心就会 ...

  3. 51Nod 1344 走格子 | 贪心

    Input示例 5 1 -2 -1 3 4 Output示例 2 贪心 #include <bits/stdc++.h> using namespace std; typedef long ...

  4. 51Nod 1091 线段重叠 | 贪心

    Input示例 5 1 5 2 4 2 8 3 7 7 9 Output示例 4 first try: O(n^2):二层循环,减法取最大 后五个time limit exceeded #includ ...

  5. 51nod 1321 收集点心(最小割)

    给出一种最小割的方法. 设\(num1[i]\),\(num2[i]\)为第i种形状的点心的两种口味的数量 设\(type[i]\),\(type[i]\)为第i种形状的点心的两种口味 假设\(num ...

  6. 51nod 1402 最大值(贪心)

    原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 思路:借鉴了这篇博文http://blog.csdn.n ...

  7. 51Nod 1432 独木舟 (贪心)

    n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? Input 第一行包含 ...

  8. 51NOD 1432 独木舟(贪心

    1432 独木舟   n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? ...

  9. 51nod 1574 排列转换(贪心+鸽巢原理)

    题意:有两个长度为n的排列p和s.要求通过交换使得p变成s.交换 pi 和 pj 的代价是|i-j|.要求使用最少的代价让p变成s. 考虑两个数字pi和pj,假如交换他们能使得pi到目标的距离减少,p ...

随机推荐

  1. 20165305 苏振龙《Java程序设计》第四周学习总结

    第五章 继承: 面向对象中,为避免多个类间重复定义共同行为.(简单说就是将相同的程序代码提升为父类.) 特点: 这里接触到了新的关键词,extends,在java语言中用estends来继承父类的行为 ...

  2. limit小结

    1. Limit接受一个或两个数字参数.参数必须是一个整数常量.如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目. 2. 初始记录行的偏移量是 0(而不是 ...

  3. 英文名为什么最好不用joe?JOE英文名的寓意是什么?

    英文名为什么最好不用joe?JOE英文名的寓意是什么? Joe 的意思是乔,人名.意为,上帝还会赐予 Joe 乔(男子名, 对不相识者非正式的称呼; Joseph的昵称)(=GIJoe)[美俚]美国兵 ...

  4. pyspider源码解读--调度器scheduler.py

    pyspider源码解读--调度器scheduler.py scheduler.py首先从pyspider的根目录下找到/pyspider/scheduler/scheduler.py其中定义了四个类 ...

  5. python shutil模块简单介绍

    python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...

  6. How to use CAR FANS C800 Diagnostic Scan Tool to do diagnosis operation

    How to use Heavy Duty Diagnostic CAR FANS C800 Diagnostic Scan Tool to do diagnosis operation Here i ...

  7. node 按行读取文件

    var readline = require('readline'); var fs = require('fs'); var os = require('os'); var fReadName =  ...

  8. Linux 配置 JDK

    1. 上传 JDK 2. 解压文件 tar -xvf 文件名 3. 配置环境变量: 指令 vim /etc/profile 以上格式是不变的,使用时只改变 JAVA_HOME 和 JAVA_BIN 的 ...

  9. MyEclipse如何清除废弃的工作空间

    1.MyEclipse如何清除废弃的工作空间Windows--->Preferences--->General--->Startup and Shutdown--->Works ...

  10. EDK II之USB设备驱动程序的加载与运行

    本文简单介绍一下USB设备的驱动程序是如何匹配设备以及被加载的: 上文(UDK中USB总线驱动的实现框架)提到USB总线枚举设备的最后一步是调用gBS->ConnectController()去 ...