/* 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){
for(int j = l; j < r; ++j) fw.add(j, x);
}
int get(int l, int r){
int mx = 0;
for(int j = l; j < r; ++j) mx= max(fw.get(j) - fw.get(j-1), 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';
// }
vector<int> rem;
for(int i = 1; i <= m; ++i){
rem.pb(i);
}
int ans = m;
// for(int i = 1; i <= n; ++i){
// cerr << fw.get(i) - fw.get(i-1) << '\n';
// }
for(int k = 0; k <= m; ++k){
ans = min(get(1, n+1), ans);
if(k==m) break;
// cerr << "crap\n";
int best = 0;
int val = MOD;
for(int i = 0; i < rem.size(); ++i){
int l = a[rem[i]][0];
int r = a[rem[i]][1];
int vall = max(max(get(r, n + 1), get(1, l)) + 1, get(l, r) - 1);
if(val > vall) val = vall, best = i;
}
// cerr << rem[best] << " wtf\n";
add(a[rem[best]][0], a[rem[best]][1], -1);
add(1, a[rem[best]][0], 1);
add(a[rem[best]][1], n + 1, 1);
rem.erase(rem.begin() + best);
// for(int i = 1; i <= n; ++i){
// cerr << fw.get(i) - fw.get(i-1) << ' ';
// }
// en;
}
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... |