# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
292129 |
2020-09-06T12:20:12 Z |
anonymous |
Rope (JOI17_rope) |
C++14 |
|
31 ms |
47352 KB |
#include <iostream>
#include <vector>
#define MAXN 1000005
using namespace std;
int N, M, C[MAXN], Val[MAXN][2], Ans[MAXN];
vector <int> Pairs[MAXN][2];
class segtree {
int ST[MAXN * 4];
public:
void upd(int ind, int val, int l, int r, int cur) {
if (ind < l || ind > r) {return;}
if (l == r) {
ST[cur] += val;
} else {
int mid = (l + r) >> 1;
upd(ind, val, l, mid, 2*cur);
upd(ind, val, mid+1, r, 2*cur+1);
ST[cur] = max(ST[2*cur], ST[2*cur+1]);
}
}
int get() {return(ST[1]);}
} ST1, ST2;
int main() {
freopen("ropein.txt","r",stdin);
scanf("%d %d",&N,&M);
for (int i=1; i<=M; i++) {
Ans[i] = N;
}
for (int i=1; i<=N; i++) {
scanf("%d",&C[i]);
Ans[C[i]] -= 1;
}
for (int i=1; i<N; i+=2) {
Pairs[C[i]][0].push_back(C[i+1]);
Pairs[C[i+1]][0].push_back(C[i]);
Val[C[i]][0]++, Val[C[i+1]][0]++;
}
for (int i=2; i<N; i+=2) {
Pairs[C[i]][1].push_back(C[i+1]);
Pairs[C[i+1]][1].push_back(C[i]);
Val[C[i]][1]++, Val[C[i+1]][1]++;
}
if (N%2) {
Val[C[N]][0]++, Val[C[1]][1]++;
} else {
Val[C[1]][1]++, Val[C[N]][1]++;
}
for (int i=1; i<=M; i++) {
ST1.upd(i, Val[i][0], 1, M, 1);
ST2.upd(i, Val[i][1], 1, M, 1);
printf("%d %d %d\n",i,Val[i][0], Val[i][1]);
}
for (int i=1; i<=M; i++) {
//solve case 1
for (int v: Pairs[i][0]) {
ST1.upd(v, -1, 1, M, 1);
}
ST1.upd(i, -1<<20, 1, M, 1);
Ans[i] = min(Ans[i], N - ST1.get() - Val[i][0]);
for (int v: Pairs[i][0]) {
ST1.upd(v, 1, 1, M, 1);
}
ST1.upd(i, 1<<20, 1, M, 1);
//solve case 2:
for (int v: Pairs[i][1]) {
ST2.upd(v, -1, 1, M, 1);
}
ST2.upd(i, -1<<20, 1, M, 1);
Ans[i] = min(Ans[i], N - ST2.get() - Val[i][1]);
for (int v: Pairs[i][1]) {
ST2.upd(v, 1, 1, M, 1);
}
ST2.upd(i, 1<<20, 1, M, 1);
}
for (int i=1; i<=M; i++) {
printf("%d\n",Ans[i]);
}
}
Compilation message
rope.cpp: In function 'int main()':
rope.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
26 | freopen("ropein.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rope.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
27 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
rope.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
32 | scanf("%d",&C[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |