您的位置:首页 > 其它

codeforces 747C Servers

2016-12-20 22:52 330 查看
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.

题意:给定n个服务器编号1—n,q个任务,每个任务都有一个开始时间t,需要的服务器的个数k,和任务完成需要花的时间d,求每个任务完成所需要的服务器的id号的和最小是多少!

思路:暴力模拟,优先队列两个,一个用来存任务的结束时间,结束早的排在最前面,一个用来存服务器id,要求每个任务的服务器id和最小,那么每次选取的时候就选择id号的最小的几个服务器。

/*
_...---.._
,'          ~~"--..
/                   ~"-._
/                         ~-.
/              .              `-.
\             -.\                `-.
\              ~-.                 `-.
,-~~\                ~.
/     \                 `.
.       \                  `.
|        \                   .
|         \                   \
.         `.                  \
\                  \
`           `.                 \
`           \.                 \
`           \`.                \
.           \ -.               \
`               -.              \
.           `    -              \  .
`            \    ~-             \
`            .     ~.            \
.            \      -_           \
`                     -           \
.            |        ~.          \
`            |          \          \
.           |           \          \
`           |            `.         \
`          `              \         \
.          .              `.        `.
`          :                \         `.
\         `                 \          `.
\         .                 `.         `~~-.
\        :                   `         \   \
.        .                   \         : `.\
`        :                    \        |  | .
\        .                    \       |  |
\       :                     \      `  |  `
.                             .      | |_  .
`       `.                    `      ` | ~.;
\       `.                    .      . .
.       `.                   `      ` `
`.       `._.                 \      `.\
`        <  \                 `.     | .
`       `   :                 `     | |
`       \                     `    | |
`.     |   \                  :  .' |
"Are you crying? "        `     |    \                 `_-'  |
"It's only the rain."  :    | |   |                   :    ;
"The rain already stopped."`    ; |~-.|                 :    '
"Devils never cry."       :   \ |                     `   ,
`    \`                      :  '
:    \`                     `_/
`     .\       "For we have none. Our enemy shall fall."
`    ` \      "As we apprise. To claim our fate."
\    | :     "Now and forever. "
\  .'  :    "We'll be together."
:    :    "In love and in hate"
|    .'
|    :     "They will see. We'll fight until eternity. "
|    '     "Come with me.We'll stand and fight together."
|   /      "Through our strength We'll make a better day. "
`_.'       "Tomorrow we shall never surrender."
sao xin*/
#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define INF 0x3f3f3f3
#define pi acos(-1)
//const LL INF = 1e15;
const int maxn=1e3+5;
const int maxx=1e5+5;
//const double q = (1 + sqrt(5.0)) / 2.0;   // 黄金分割数
/*
std::hex    <<16进制   cin>>std::hex>>a>>std::hex>>b
cout.setf(ios::uppercase);cout<<std::hex<<c<<endl;
//f[i]=(i-1)*(f[i-1]+f[i-2]);   错排
priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
priority_queue<int,vector<int>,less<int> >que4;////最大值优先
//str tmp vis val cnt  2486
*/
struct point {
int id;
int ed;
bool operator < (const point &a) const {
return ed > a.ed;//结束时间早的优先级高
}
} p;
priority_queue<int,vector<int>,greater<int> >que;
priority_queue<point >q;
int n,m;
void solve()
{
cin >> n >> m;
priority_queue<int, vector<int>, greater<int> >que;//定义用来存服务器id号,从小到大
priority_queue<point> q;
for(int i = 1; i <= n; i++) {
que.push(i);
}
int t,sever,d;
while(m--) {
cin >> t >> sever >> d;
while(!q.empty()) {//解除当前的服务器结束时间小于给定的t,并且加入到另一个队列中
p = q.top();
if(p.ed > t) {
break;
}
q.pop();
que.push(p.id);
}
int len = que.size();
if(len >= sever) {
int cnt = 0;
while(sever--) {
int sid = que.top();
que.pop();
p.id = sid;
p.ed = t+d;
cnt += p.id;
q.push(p);
}
cout << cnt << endl;
}else{
printf("-1\n");
}
}
}

int main()
{
solve();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: