【Codeforces 747 C Servers 】+ 思维 或 优先队列
来源:互联网 发布:pp助手for mac 编辑:程序博客网 时间:2024/06/05 10:01
C. Servers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n.
It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct.
To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, …, ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.
Write the program that determines which tasks will be performed and which will be ignored.
Input
The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks.
Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds.
Output
Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers’ ids on which this task will be performed. Otherwise, print -1.
Examples
Input
4 3
1 3 2
2 2 1
3 4 3
Output
6
-1
10
Input
3 2
3 2 3
5 1 2
Output
3
3
Input
8 6
1 3 20
4 2 1
6 5 5
10 1 1
15 3 6
21 8 8
Output
6
9
30
-1
15
36
Note
In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).
In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task.
思维思路 : 更新每台机器最后一使用的开始时间结束询问
AC代码:
#include<bits/stdc++.h>using namespace std;const int K = 1e5 + 10;int t[K],k[K],d[K],vis[110];int main(){ int N,M; scanf("%d %d",&N,&M); for(int i = 1 ; i <= M ;i++){ int nl = 0; scanf("%d %d %d",&t[i],&k[i],&d[i]); for(int j = 1 ; j <= N; j++) if(!vis[j] || d[vis[j]] + t[vis[j]] <= t[i]) nl++; if(nl < k[i]) printf("-1\n"); else{ int ml = 0,ans = 0; for(int j = 1 ; j <= N && ml < k[i]; j++) if(!vis[j] || d[vis[j]] + t[vis[j]] <= t[i]){ ml++; vis[j] = i; // 记录下上次用 j 机器最后用过的询问,以便记录下最后一次用的时间 ans += j; } printf("%d\n",ans); } } return 0;}
优先队列思路 : 一个编号小的优先级高的优先队列,一个结束时间早的优先级高的优先队列~~
AC代码:
#include<bits/stdc++.h>using namespace std;struct node{ int t,id; bool operator < (const node &a) const{ return a.t < t; // 结束时间早的优先级高 }}st;priority_queue<int,vector<int>,greater<int> > q1; // 编号小的优先级高priority_queue <node> q2;int main(){ int N,M; scanf("%d %d",&N,&M); for(int i = 1 ; i <= N; i++) q1.push(i); int k,t,d; while(M--){ scanf("%d %d %d",&t,&k,&d); while(!q2.empty()){ st = q2.top(); if(st.t > t) break; q2.pop(); q1.push(st.id); } int nl = q1.size(); if(nl < k) printf("-1\n"); else{ int ans = 0; while(k--){ int i = q1.top(); q1.pop(); st.id = i; st.t = t + d; ans += i; q2.push(st); } printf("%d\n",ans); } } return 0;}
- 【Codeforces 747 C Servers 】+ 思维 或 优先队列
- codeforces 747C Servers
- codeforces 747C Servers
- 【codeforces 747C】Servers
- Codeforces 599C Day at the Beach【思维+优先队列】
- 【50.00%】【codeforces 747C】Servers
- Codeforces 591E Three States【优先队列Bfs+思维】
- CodeForces 140C 贪心+优先队列
- CodeForces 137C【贪心+优先队列】
- codeforces 45c dancing lessons(优先队列)
- [codeforces] 854C. Planning(优先队列)
- GYM 101128 C.Canvas Painting【思维+优先队列】
- Codeforces Round #387 (Div. 2) 747C Servers
- 【映射 && 优先队列】CodeForces
- 优先队列 CodeForces
- Codeforces Round #357 (Div. 2)-C. Heap Operations(优先队列)
- [CodeForces - 140C New Year Snowmen] 贪心 + STL 优先队列
- Codeforces Alpha Round #20 C. Dijkstra?(优先队列优化)
- 在页面里面播放音频文件
- Python学习笔记 __slots__ 限制类属性
- 链表的构建与排序
- springmvc之视图和视图解析器
- MySql基础1
- 【Codeforces 747 C Servers 】+ 思维 或 优先队列
- Lua编程笔录--Lua初识及语法一(注释,变量命名,全局/局部变量,数据类型)
- Apache禁用测试页(默认页)
- init.rc的触发顺序
- Chapter 1 Introduction
- 搜集的几道C语言和数据库题目
- Nexus Maven Repository
- 使用Python快速建立FTP服务器
- ES错误Error:Failed to parse mapping [data]:[_ttl] is removed in 5.0