CodeForces 747C Servers(优先队列)
2018-01-02 20:23
459 查看
题目链接:http://codeforces.com/problemset/problem/747/C点击打开链接
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
output
input
output
input
output
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的数据只有100 因此复杂度可为q*nlogn<10^7
用最小堆维护结束之间最小的
然后每次查询将堆里符合条件的都取出来排序找服务器号数最小的进行处理再放回堆里
#include <bits/stdc++.h>
using namespace std;
struct xjy
{
int num;
int time;
bool operator < (const xjy &r)const
{
if(time==r.time)
{
return num>r.num;
}
return time>r.time;
}
};
priority_queue<xjy> q;
int cmp1(xjy a,xjy b)
{
return a.num<b.num;
}
int main()
{
int n,m;
cin >> n >> m;
for(int i=1;i<=n;i++)
{
xjy mid;
mid.num=i;
mid.time=0;
q.push(mid);
}
for(int i=0;i<m;i++)
{
int mid[3];
cin >> mid[0] >> mid[1] >> mid[2];
vector<xjy > re;
while(!q.empty())
{
if(q.top().time<=mid[0])
{
re.push_back(q.top());
q.pop();
}
else
break;
}
if(re.size()>=mid[1])
{
int sum=0;
sort(re.begin(),re.end(),cmp1);
for(int j=0;j<mid[1];j++)
{
sum+=re[j].num;
re[j].time=mid[0]+mid[2];
}
for(int j=0;j<re.size();j++)
q.push(re[j]);
cout << sum << endl;
}
else
{
cout << "-1" << endl;
for(int j=0;j<re.size();j++)
q.push(r
b151
e[j]);
}
}
}
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的数据只有100 因此复杂度可为q*nlogn<10^7
用最小堆维护结束之间最小的
然后每次查询将堆里符合条件的都取出来排序找服务器号数最小的进行处理再放回堆里
#include <bits/stdc++.h>
using namespace std;
struct xjy
{
int num;
int time;
bool operator < (const xjy &r)const
{
if(time==r.time)
{
return num>r.num;
}
return time>r.time;
}
};
priority_queue<xjy> q;
int cmp1(xjy a,xjy b)
{
return a.num<b.num;
}
int main()
{
int n,m;
cin >> n >> m;
for(int i=1;i<=n;i++)
{
xjy mid;
mid.num=i;
mid.time=0;
q.push(mid);
}
for(int i=0;i<m;i++)
{
int mid[3];
cin >> mid[0] >> mid[1] >> mid[2];
vector<xjy > re;
while(!q.empty())
{
if(q.top().time<=mid[0])
{
re.push_back(q.top());
q.pop();
}
else
break;
}
if(re.size()>=mid[1])
{
int sum=0;
sort(re.begin(),re.end(),cmp1);
for(int j=0;j<mid[1];j++)
{
sum+=re[j].num;
re[j].time=mid[0]+mid[2];
}
for(int j=0;j<re.size();j++)
q.push(re[j]);
cout << sum << endl;
}
else
{
cout << "-1" << endl;
for(int j=0;j<re.size();j++)
q.push(r
b151
e[j]);
}
}
}
相关文章推荐
- codeforces 747C Servers
- CodeForces - 574A Bear and Elections 简单的优先队列的使用
- codeforces 567 E President and Roads (优先队列+迪杰斯特拉+tarjan)
- codeforces 658B B. Bear and Displayed Friends(优先队列)
- Codeforces 3D 优先队列
- CodeForces 3D Least Cost Bracket Sequence (贪心+优先队列)
- CodeForces 128B String 后缀数组 或 优先队列维护 求第K小子串
- Codeforces 691D. Swaps in Permutation (并查集 + 优先队列)
- codeforces 747C Servers
- Codeforces 639A Bear and Displayed Friends (优先队列)
- Codeforces 722D Generating Sets【优先队列+贪心】
- 【CodeForces - 754D Fedor and coupons】 贪心+优先队列
- [codeforces] 854C. Planning(优先队列)
- codeforces 721D D. Maxim and Array (STL||优先队列)
- CodeForces 140 C. New Year Snowmen 详解 (贪心+优先队列)
- codeforces 747C Servers
- Magic Powder - 1 CodeForces - 670D1 (优先队列)
- CodeForces - 913D(贪心+优先队列)
- Codeforces 913D - Too Easy Problems(贪心+优先队列)
- CodeForces 377B Preparing for the Contest 贪心(二分加优先队列)