UVA 1153 - Keep the Customer Satisfied(贪心)
来源:互联网 发布:access 导入sqlserver 编辑:程序博客网 时间:2024/05/17 22:25
题目链接:点击打开链接
思路:
我们按照时间从小到大排序, 因为时间最大2e6, 所以从小到大枚举时间, 当一个订单到了最后期限, 我们就看看它能否满足, 如果能就满足它, 如果不能, 我们看一下之前满足的订单中货物最多的一个是否比当前这个大, 如果大,就拿它换当前这个, 为后面节省空间。 用优先队列可以很方便的维护。
细节参见代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <ctime>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = acos(-1);const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int seed = 131;const ll INF64 = ll(1e18);const int maxn = 1e6 + 10;int T,n,m;struct node { int v, t; node(int v=0, int t=0):v(v), t(t) {} bool operator < (const node& rhs) const { return t < rhs.t; }}a[maxn];int main() { scanf("%d",&T); while(T--) { scanf("%d", &n); int maxt = 0; priority_queue<int> q; for(int i = 1; i <= n; i++) { scanf("%d%d", &a[i].v, &a[i].t); maxt = max(maxt, a[i].t); } int cnt = 1; sort(a+1, a+n+1); int cur = 0, ans = 0; for(int i = 0; i <= maxt; i++) { if(i) ++cur; while(cnt <= n && a[cnt].t == i) { if(cur >= a[cnt].v) { q.push(a[cnt].v); cur -= a[cnt].v; ++ans; } else if(!q.empty()) { int hehe = q.top(); if(hehe > a[cnt].v) { q.pop(); q.push(a[cnt].v); cur += (hehe - a[cnt].v); } } ++cnt; } } printf("%d\n", ans); if(T) printf("\n"); } return 0;}
0 0
- UVa 1153 Keep the Customer Satisfied 解题报告(贪心)
- UVa 1153 - Keep the Customer Satisfied(贪心)
- UVA 1153 - Keep the Customer Satisfied(贪心)
- Uva 1153 Keep the Customer Satisfied(贪心)
- 1153 - Keep the Customer Satisfied(贪心)
- uva 1153 - Keep the Customer Satisfied(贪心+优先队列)
- UVa 1153 - Keep the Customer Satisfied (贪心 + 优先队列)
- UVA 1153 Keep the Customer Satisfied(贪心2.0)
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
- uva uva 1153 - Keep the Customer Satisfied
- UVa - 1153 - Keep the Customer Satisfied
- UVA - 1153 Keep the Customer Satisfied
- UVA 1153 - Keep the Customer Satisfied
- UVA 1153(p255)----Keep the Customer Satisfied
- UVA 1153 Keep the Customer Satisfied
- 习题8-12 UVA - 1153 Keep the Customer Satisfied 顾客是上帝(优先队列贪心)
- UVA 1153Keep the Customer Satisfied 顾客是上帝 经典贪心 (SWE 2005)
- 1153 - Keep the Customer Satisfied(贪心+优先队列)
- 邮件服务器小结
- 获取结构体字节数简单的方法
- ViewHolder的二次封装
- 安装和使用CocoaPods
- Android的handler详解
- UVA 1153 - Keep the Customer Satisfied(贪心)
- jquery ajax 请求函数封装
- 全能型开源远程终端:MobaXterm
- innodb表最大列数限制
- window.prompt()输入对话框
- POJ1789——最小生成树
- POI 对excel表格基本操作Demo
- pom.xml中的java.home变量(eclipse默认的java.home变量)&& pom.xml详解&&maven常用配置的变量
- 【2016】八月份英语学习总结