CodeForces-1121C System Testing
題目鏈接
https://vjudge.net/problem/CodeForces-1121C
題面
Description
Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab.
There are \(n\) solutions, the \(i\)-th of them should be tested on \(a_i\) tests, testing one solution on one test takes \(1\)
At any time moment \(t\) when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id \(i\)
Consider some time moment, let there be exactly \(m\)
\[ d = round\left(100\cdot\frac{m}{n}\right), \]
where \(round(x) = \lfloor{x + 0.5}\rfloor\) is a function which maps every real to the nearest integer.
Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test \(q\), and the caption says "System testing: \(q\)%". Find the number of interesting solutions.
Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter.
Input
The first line contains two positive integers \(n\) and \(k\) (\(1 \le n \le 1000\), \(1 \le k \le 100\)) standing for the number of submissions and the number of testing processes respectively.
The second line contains \(n\) positive integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 150\)), where \(a_i\) is equal to the number of tests the \(i\)-th submission is to be run on.
Output
Output the only integer — the number of interesting submissions.
Examples
Input
2 2
49 100
Output
1
Input
4 2
32 100 33 1
Output
2
Input
14 5
48 19 6 9 50 20 3 42 38 43 36 21 44 6
Output
5
Note
Consider the first example. At time moment \(0\) both solutions start testing. At time moment \(49\) the first solution is fully tested, so at time moment \(49.5\) the second solution is being tested on the test \(50\), and the caption says "System testing: \(50\)%" (because there is one fully tested solution out of two). So, the second solution is interesting.
Consider the second example. At time moment \(0\) the first and the second solutions start testing. At time moment \(32\) the first solution is fully tested, the third solution starts testing, the caption says "System testing: \(25\)%". At time moment \(32 + 24.5 = 56.5\) the third solutions is being tested on test \(25\), the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment \(32 + 33 = 65\), the fourth solution is fully tested at time moment \(65 + 1 = 66\). The captions becomes "System testing: \(75\)%", and at time moment \(74.5\) the second solution is being tested on test \(75\). So, this solution is also interesting. Overall, there are two interesting solutions.
題意
給定\(n\)個題目,每個題目有\(a[i]\)個測試點,有k臺評測機,每臺評測機在某一時刻只能測試一個題目,把所有測試點測完後測試隊列中的下一道題,測試的進度用\(d = round\left(100\cdot\frac{m}{n}\right)\)來表示,其中\(round(x) = \lfloor{x + 0.5}\rfloor\) ,m是完全測完的題目數量,對於每一個測試中的題,如果某一時刻正好測試到\(i_{th}\)測試點,而\(i_{th}=d\)則這個題目被稱為有趣的,問有多少有趣的題目
題解
我們可以用一個優先隊列很快的算出每個題測試的開始時間和結束時間,然後我們就可以知道每個時刻前有多少個題目已經測試完畢了,所以就能算出每個時刻的測試進度,以及每個進度對應的時間區間,然後我們對於每一個題,遍歷每一個時間進度覆蓋的區間,如果和當前題結束和開始的時間段有交集就判斷是否有趣。
至於如何判斷,如果當前進度覆蓋的區間剛好能包括進去當前題目的測試對應等於進度測試點的時間,那麽就是有趣的,這個時間就是這個題目開始的時間+這個區間對應的進度值,,這個時間要大於等於這個進度的左端點,同時要小於等於這個題目測試完的時間和這個區間右端點的較小值。
AC代碼
#include <bits/stdc++.h>
#define N 1050
using namespace std;
int a[N];
int min(int a, int b) {
return a < b ? a : b;
}
struct node {
int id, val;
node (int id = 0, int val = 0): id(id), val(val) {}
bool operator < (const node &b) const {
return val > b.val;
}
};
struct fin {
int l, r;
} finish[N];
int pre[150 * N];
int round1[150 * N];
struct pro{
int l; int r; int val;
} process[N];
priority_queue<node> q;
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
if (q.size() < k) q.push(node(i, a[i]));
else {
node now = q.top();
finish[now.id].l = now.val - a[now.id];
finish[now.id].r = now.val;
q.pop();
q.push(node(i, a[i] + now.val));
}
}
int last = 0;
while (!q.empty()) {
node now = q.top();
finish[now.id].l = now.val - a[now.id];
finish[now.id].r = now.val;
last = max(last, now.val);
q.pop();
}
// for (int i = 1; i <= n; i++) {
// cout << finish[i].l << " " << finish[i].r << endl;
// }
for (int i = 1; i <= n; i++) {
pre[finish[i].r]++;
}
for (int i = 1; i <= last; i++) {
pre[i] += pre[i - 1];
}
for (int i = 1; i <= last; i++) {
round1[i] = floor((double)pre[i] / (double)n * 100 + 0.5);
}
int cnt = 0;
int tmp = 0;
for (int i = 1; i <= last; i++) {
if (round1[i] != round1[i - 1]) {
process[++cnt].val = round1[i];
process[cnt].l = i + 1;
if (cnt != 0) {
process[cnt - 1].r = i;
}
}
}
process[cnt].r = last + 1;
// for (int i = 1; i <= cnt; i++) {
// cout << process[i].l << " " << process[i].r << " " << process[i].val << endl;
// }
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= cnt; j++) {
if (process[j].l <= finish[i].r && process[j].r >= finish[i].l) {
if (process[j].l <= finish[i].l + process[j].val && min(finish[i].r, process[j].r) >= finish[i].l + process[j].val) {
// cout << "ans: " << i << endl;
ans++;
break;
}
}
}
}
printf("%d\n", ans);
return 0;
}
CodeForces-1121C System Testing