/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
struct Fenwick{
vector<int> t;
Fenwick(int n){
t.resize(n + 1);
}
void add(int v, int x){
while(v < t.size()){
t[v]+=x;
v += v & -v;
}
}
int get(int v){
int res=0;
while(v > 0){
res+=t[v];
v -= v&-v;
}
return res;
}
} fw(N);
int n, m;
array<int, 3> a[N];
void add(int l, int r, int x){
if(l < r){
for(int j = l; j < r; ++j) fw.add(j, x);
}else{
for(int j = r; j <= n; ++j) fw.add(j, x);
for(int j = 1; j < l; ++j) fw.add(j, x);
}
}
int get(int l, int r){
int mx = 0;
for(int j = l; j < r; ++j) mx=mx + fw.get(j) - fw.get(j-1);
mx = mx - fw.get(n) + mx;
return mx;
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= m; ++i){
cin >> a[i][0] >> a[i][1] >> a[i][2];
if(a[i][1] < a[i][0]) swap(a[i][0], a[i][1]);
add(a[i][0], a[i][1], 1);
}
sort(a+1, a+1+m, [&](const array<int, 3> &x, const array<int, 3> &y){
return get(x[0], x[1]) < get(y[0], y[1]);
});
// for(int i = 1; i <= m; ++i){
// cerr << get(a[i][0], a[i][1]) << ' ';
// // cerr << a[i][0] << ' ' << a[i][1] << '\n';
// }
int ans = m;
for(int k = 0; k <= m; ++k){
vector<int> cnt(n + 1);
for(int i = 1; i <= m; ++i){
if(i <= k){
for(int j = a[i][0]; j < a[i][1]; ++j) cnt[j]++;
}else{
for(int j = a[i][1]; j <= n; ++j) cnt[j]++;
for(int j = 1; j < a[i][0]; ++j) cnt[j]++;
}
}
ans = min(ans, *max_element(all(cnt)));
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |