51nod 1351 吃点心(贪心)
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 吃点心(贪心)的更多相关文章
- 1351 topcoder 吃点心
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 先按low从大到小贪心再high从小到大贪心 #pragma c ...
- 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个人,每个人都有喜欢的两个点心,现在给他们排个队,一个一个吃,每个人只要有自己喜欢的点心就会 ...
- 51Nod 1344 走格子 | 贪心
Input示例 5 1 -2 -1 3 4 Output示例 2 贪心 #include <bits/stdc++.h> using namespace std; typedef long ...
- 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 ...
- 51nod 1321 收集点心(最小割)
给出一种最小割的方法. 设\(num1[i]\),\(num2[i]\)为第i种形状的点心的两种口味的数量 设\(type[i]\),\(type[i]\)为第i种形状的点心的两种口味 假设\(num ...
- 51nod 1402 最大值(贪心)
原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 思路:借鉴了这篇博文http://blog.csdn.n ...
- 51Nod 1432 独木舟 (贪心)
n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? Input 第一行包含 ...
- 51NOD 1432 独木舟(贪心
1432 独木舟 n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? ...
- 51nod 1574 排列转换(贪心+鸽巢原理)
题意:有两个长度为n的排列p和s.要求通过交换使得p变成s.交换 pi 和 pj 的代价是|i-j|.要求使用最少的代价让p变成s. 考虑两个数字pi和pj,假如交换他们能使得pi到目标的距离减少,p ...
随机推荐
- Linux基础命令---uniq
uniq 将文件中重复出现的行删除,结果送到标准输出或者指定文件.在使用uniq指令之前,必须使用sort对内容进行排序,否则没有效果.如果没有选项,则将匹配的行合并到第一个匹配项. 此命令的适用范围 ...
- Deploying docker registry v2
生成证书 openssl genrsa -out mydomain.key 2048 生成秘钥 openssl req -newkey rsa:4096 -nodes -sha256 -keyout ...
- ECharts + Jquery 做大屏展示
HTML <!doctype html> <html> <head> <meta charset="utf-8"> <meta ...
- 虚拟继承C++
C++中虚拟继承的概念 为了解决从不同途径继承来的同名的数据成员在内存中有不同的拷贝造成数据不一致问题,将共同基类设置为虚基类.这时从不同的路径继承过来的同名数据成员在内存中就只有一个拷贝,同一个函数 ...
- MyEclipse新建Server项目
- 定时释放Linux/CentOS缓存
#!/bin/bash used=`free -m | awk 'NR==2' | awk '{print $3}'` free=`free -m | awk 'NR==2' | awk '{prin ...
- Scrapy框架学习 - 使用内置的ImagesPipeline下载图片
需求分析需求:爬取斗鱼主播图片,并下载到本地 思路: 使用Fiddler抓包工具,抓取斗鱼手机APP中的接口使用Scrapy框架的ImagesPipeline实现图片下载ImagesPipeline实 ...
- php 网页版 ftp 小工具
<?php define('IN_ECS', true); session_start(); header("Content-type:text/html;charset=utf-8& ...
- php 获取文件后缀
/** * 获取文件后缀 * $path 本地存储临时文件路径 * */ private function getFileType($path){ $fp=fopen($path,'r'); $bin ...
- Lyft高管的技术团队管理实战
Lyft 的技术总监沈思维分享了他对于管理技术团队和打造工程文化的经验,也欢迎添加他的微信公众号"人家的屋顶"了解更多(微信公众号ID: othersroof).沈思维毕业于密歇根 ...