Gym
来源:互联网 发布:s7总决赛现场数据 编辑:程序博客网 时间:2024/05/18 01:01
题意:
告诉你n 个操作过程, + 代表 在t 时刻来了k 个车, - 代表在t 时刻 来了k 个人, 每个人都要开走车, 没车的话 一直在队列里等, 告诉你每一天开始有多少辆车, 求所有人的等待时间总和, 如果有人拿不到车就输出 inf。
思路:
数形结合的思想。
n 个操作可以分成n-1 段, 记录每一段的人和 等待时间。
那么一开始有x 辆车的话, 那么只需要将大于x 的段减掉x 乘以等待时间即可。
那么这样 与每一段的顺序无关, 直接按照人排序, 然后枚举x, 二分查找左边界, 做个前缀和 差分即可。
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <string>#include <iostream>#include <cctype>#include <sstream>#include <vector>using namespace std;const int maxn = 100005;struct node1{ char cmd[4]; int t, k;}p[maxn];struct node{ int num, len;}nod[maxn];bool cmp(const node& lhs, const node& rhs){ return lhs.num < rhs.num;}long long pre[maxn];long long pre1[maxn];int main(){ freopen("expect.in","r",stdin); freopen("expect.out","w", stdout); int n,q; while(~scanf("%d %d",&n, &q)){ int x, y; int sum = 0; long long peo = 0LL; for (int i = 0; i < n; ++i){ scanf("%s%d%d", p[i].cmd, &p[i].t, &p[i].k); if (p[i].cmd[0] == '-'){ peo += p[i].k; } else { peo -= p[i].k; } } if (p[0].cmd[0] == '-') sum = p[0].k; else sum = -p[0].k; int cnt = 0; for (int i = 1; i < n; ++i){ nod[++cnt].num = sum; nod[cnt].len = p[i].t - p[i-1].t; if (p[i].cmd[0] == '-'){ sum += p[i].k; } else { sum -= p[i].k;// sum = max(sum, 0); } } sort(nod + 1, nod + cnt + 1, cmp); for (int i = 1; i <= cnt; ++i){ pre[i] = pre[i-1] + (long long)nod[i].num * nod[i].len; pre1[i] = pre1[i-1] + nod[i].len; }//// for (int i = 1; i <= cnt; ++i){// printf("%d %d\n", nod[i].num, nod[i].len);//// }// printf("peo = %lld\n", peo); for (int i = 0; i < q; ++i){ long long ans = 0LL; int x; scanf("%d",&x); if (x < peo) { printf("INFINITY\n"); continue; } int l = 1, r = cnt; while (l <= r){ int m = l + r >> 1; if (nod[m].num > x){ r = m - 1; } else { l = m + 1; } }// printf("r = %d\n", r); ans = (pre[cnt] - pre[r]) - x * (pre1[cnt] - pre1[r]); printf("%I64d\n", ans); } } return 0;}/**This is ACM North Eastern European Regional Contest,sponsored by International Business Machines.The. Best. Contest. Ever.A Great Opportunity for all contestants.ab Ab A Abc AB Abcd ABc Abcde AbCOh No Extra Spaces.And,Punctuation Ruin Everything10 4 1 21 2 3 4 5 6 7 8 9 1010 9 8 7 6 5 4 3 2 1EEESEEEESS686 100- 2 3- 4 3+ 5 2+ 7 4- 10 3+ 13 36 100- 2 3- 4 3+ 5 2+ 7 4- 10 3+ 13 35 100+ 2 1+ 4 2+ 5 3+ 9 10+ 10 11**/
阅读全文
0 0
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- 时间段重复校验-js
- Retrofit源码分析 (四. Retrofit 官网说明,Copy)
- Mysql时区问题
- 一直没搞清楚的数组sort方法传入一个function是怎么运行的
- 三、解决Springboot集成ActivitiModel提示输入密码的问题
- Gym
- Tomcat集群配置学习篇(MARK)
- javaweb基础之--Servlet
- EasyNVR H5无插件摄像机直播解决方案前端解析之:videojs初始化的一些样式处理
- C# ADO.NET动态数据的增删改查(第五天)
- android——studio中去除标题栏
- vue项目中使用echarts
- Retrofit源码分析 (五. Retrofit 深入分析)
- redis 连接报错