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. 吴恩达深度学习笔记(deeplearning.ai)之卷积神经网络(CNN)(上)

    作者:szx_spark 1. Padding 在卷积操作中,过滤器(又称核)的大小通常为奇数,如3x3,5x5.这样的好处有两点: 在特征图(二维卷积)中就会存在一个中心像素点.有一个中心像素点会十 ...

  2. 转:C#线程系列讲座(1) BeginInvoke和EndInvoke方法

    转载自:http://www.cnblogs.com/levin9/articles/2319248.html 开发语言:C#3.0IDE:Visual Studio 2008本系列教程主要包括如下内 ...

  3. Django 自定义

    from django.db import models class MyCharfield(models.Field): def __init__(self,max_length,*args,**k ...

  4. MyEclipse如何修改XML文件默认行宽

    1.MyEclipse如何修改XML文件默认行宽 Windows--->Preferences--->搜索xml--->XML--->XML Source--->Form ...

  5. no matching editors or conversion strategy found

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionM ...

  6. EDK II之DXE Core的事件管理

    本文简单介绍一下UEFI中的事件管理: UEFI是不支持多进程的,但是UEFI支持多事件分发机制.UEFI只支持时钟中断,并基于时钟中断实现事件分发.类似于OS中基于时钟中断来实现基于时间片的多任务调 ...

  7. JS截取字符串中数字

    今天项目中需要在一个字符串中截取一个数字,然后数字参与运算.搜了一下,有好多好用的方式截取字符串. 1,使用parseInt() var str ="4500元"; var num ...

  8. memset与malloc性能测试(转)

    前一段跟同事聊项目组已有的一些工具,同事讲里面有太多的malloc与memset,对性能的影响比较大,因此今天就在自己的机器上测试了这两个函数,不多说,上数据.测试环境:2.2GHZ.2G内存mems ...

  9. intel FPGA使用

    https://www.altera.com/documentation/swn1503506366945.html https://files.cnblogs.com/files/shaohef/o ...

  10. 前端 --- 4 js

    一.js  描述 JavaScript 是脚本语言 JavaScript 是一种轻量级的编程语言.后来出现了node.js,可以作为后端语言来开发项目, js是一个既能做前端又能做后端的语言 Java ...