#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<int>> a(m, vector<int>(3));
for(int i = 0; i < m; ++i){
cin >> a[i][0] >> a[i][1] >> a[i][2];
--a[i][0], --a[i][1];
int len = (a[i][1] + n - a[i][0]) % n;
if(len * 2 > n){
swap(a[i][0], a[i][1]);
}
}
sort(a.begin(), a.end(), [&](vector<int>&x, vector<int>&y)->bool{
if(x[0] < y[0]) return 1;
if(x[0] > y[0]) return 0;
int xx = x[1], yy = y[1];
if(xx < x[0]) xx += n;
if(yy < y[0]) yy += n;
return xx < yy;
});
int ans = (int)1e18;
for(int i = 0; i < m; ++i){
int j = i;
while(true){
vector<int> sum(n);
for(int k = i; ; k = (k + 1) % m){
if(a[k][0] < a[k][1]){
++sum[a[k][0]];
--sum[a[k][1]];
}
else{
++sum[a[k][0]];
++sum[0];
--sum[a[k][1]];
}
if(k == j) break;
}
for(int k = (j + 1) % m; k != i; k = (k + 1) % m){
swap(a[k][0], a[k][1]);
if(a[k][0] < a[k][1]){
++sum[a[k][0]];
--sum[a[k][1]];
}
else{
++sum[a[k][0]];
++sum[0];
--sum[a[k][1]];
}
swap(a[k][0], a[k][1]);
}
int mx = 0;
for(int k = 0; k < n; ++k){
if(k) sum[k] += sum[k - 1];
mx = max(mx, sum[k]);
}
ans = min(ans, mx);
j = (j + 1) % m;
if(i == j) break;
}
}
vector<int> sum(n);
for(int k = 0; k != m; ++k){
swap(a[k][0], a[k][1]);
if(a[k][0] < a[k][1]){
++sum[a[k][0]];
--sum[a[k][1]];
}
else{
++sum[a[k][0]];
++sum[0];
--sum[a[k][1]];
}
swap(a[k][0], a[k][1]);
}
int mx = 0;
for(int k = 0; k < n; ++k){
if(k) sum[k] += sum[k - 1];
mx = max(mx, sum[k]);
}
ans = min(ans, mx);
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |