GYM 101149 G.Of Zorcs and Axes(set)
来源:互联网 发布:人工智能现状和趋势 编辑:程序博客网 时间:2024/06/14 03:03
Description
从m个物品中选出n个,m个物品每个有重量w[i],价值c[i],要求选出的第i个物品的重量不小于a[i],价值不小于b[i],问是否有合法方案
Input
第一行一整数n表示要选出物品数量,之后n行每行两个整数a[i]和b[i]表示对选出的第i个物品的限制,之后输入一整数m表示所有物品数,之后m行每行两个整数w[i]和c[i]表示第i件物品的重量和价值(1<=n,m<=2e5,1<=a[i],b[i],w[i],c[i]<=1e9)
Output
如果存在合法方案则输出n个整数表示选出的第i个物品的编号,否则输出-1
Sample Input
3
3 4
5 1
2 6
4
5 7
4 5
3 3
5 2
Sample Output
2 4 1
Solution
对n个限制按b[i]降序排,对m件物品按c[i]降序排,游标法,每次把c[i]大于等于当前限制b[i]的物品拿出来,找一个重量大于等于a[i]的最小值作为这个限制选出的物品,用set维护当前可以用的物品,每次二分查找重量大于等于a[i]的物品,时间复杂度O(mlogm+nlogn)
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 222222int n,m,ans[maxn];struct node{ int w,c,id; bool operator<(const node&b)const { return w>b.w; }}a[maxn],b[maxn];typedef pair<int,int>P;set<P>s;set<P>::iterator it;int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d%d",&a[i].w,&a[i].c),a[i].id=i; scanf("%d",&m); for(int i=1;i<=m;i++)scanf("%d%d",&b[i].w,&b[i].c),b[i].id=i; sort(a+1,a+n+1); sort(b+1,b+m+1); s.clear(); int i=1,j=1,gg=0; while(i<=n) { while(j<=m&&b[j].w>=a[i].w)s.insert(P(b[j].c,b[j].id)),j++; it=s.lower_bound(P(a[i].c,-1)); if(it==s.end()) { gg=1; break; } ans[a[i].id]=(*it).second; s.erase(it); i++; } if(gg)printf("-1\n"); else { for(int i=1;i<=n;i++)printf("%d%c",ans[i],i==n?'\n':' '); } } return 0;}
0 0
- GYM 101149 G.Of Zorcs and Axes(set)
- G. Of Zorcs and Axes----贪心+STL
- gym/101149/ Of Zorcs and Axes set<pair<int, int > > it->second; it = s.lower_bound( make_pair(u[
- CFgym:Of Zorcs and Axes(贪心)
- GYM 101128 G.Game of Cards(博弈论)
- GYM 100247 E. Of Groups and Rights(dfs)
- Gym 101028G-The Tower of Evil
- Codeforces gym 101149 G 想法
- Gym - 100703G-G-Game of numbers-暴力
- ggplot: scales, axes, and legends
- GYM 101149 C.Mathematical Field of Experiments(水~)
- GYM 101149 H.Streets of Working Lanterns(贪心)
- GYM 101149 K.Revenge of the Dragon(机智)
- Gym 100942I Manhattan Project (set)
- GYM 100090 F.Asperger Syndrome(set)
- GYM 100488 K.Two Pirates(set)
- GYM 100971 D.Laying Cables(set)
- GYM 100694 E.SuperHyperMarket(set)
- 数据库-数据库、基本表、视图的创建,触发器的使用
- 【bzoj1036】[ZJOI2008]树的统计Count
- 最长公共子序列
- 行为型之中介者模式
- 转接IC GM8284DD:LVDS转TTL芯片 28位LVDS图像接收器
- GYM 101149 G.Of Zorcs and Axes(set)
- pycharm 查询方法
- 误将/root剪切复制到其他目录下出现bash-4.1# 如何修改
- 数组中的一些常用方法总结
- 【LeetCode】448. Find All Numbers Disappeared in an Array
- Java 通过魔数判断上传文件的类型
- 蓝桥杯 算法提高 我们的征途是星辰大海
- -PHP面向对象(OOP)编程入门-6.如何去使用对象中的成员
- Dell H310 Mini Raid卡,新加入热备盘显示Foreign的解决方法