This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 5;
priority_queue<int> heap;
int start[Nmax], finish[Nmax], add[Nmax], dp[Nmax];
vector<int> v[Nmax];
int n, m, i;
vector< pair<int,int> > edges;
int mod(int x)
{
if(x <= 0) return x+n;
if(x>n) return x-n;
return x;
}
bool check_center(int where, int L)
{
int i, need = 0, nr;
for(i=1; i<=n; ++i) v[i].clear();
for(auto e : edges)
{
int x, y;
x = mod(e.first - where + 1);
y = mod(e.second - where + 1);
if(x > y) swap(x, y);
v[x].push_back(y);
}
while(heap.size()) heap.pop();
for(i=1; i<=n; ++i) finish[i] = 0;
for(i=1; i<=n; ++i)
{
start[i] = v[i].size(); add[i] = 0;
for(auto it : v[i]) finish[it] ++;
}
dp[0] = L;
for(i=1; i<n; ++i)
{
for(auto it : v[i]) heap.push(it);
dp[i] = dp[i-1] + start[i] - finish[i] + add[i];
while(dp[i] > L && need <= L)
{
assert(heap.size());
dp[i] -= 2;
nr = heap.top();
heap.pop();
add[nr] += 2;
++need;
}
if(need > L) return 0;
}
return (need <= L);
}
bool check(int L)
{
int i;
for(i=1; i<=n; ++i)
if(check_center(i, L)) return 1;
return 0;
}
int main()
{
// freopen("input", "r", stdin);
cin >> n >> m;
int i, x, y, C;
for(i=1; i<=m; ++i)
{
cin >> x >> y >> C;
if(x > y) swap(x, y);
edges.push_back({x, y});
}
for(i=1; i<=n; ++i) sort(v[i].begin(), v[i].end());
int st = 0, dr = m, mid;
while(st <= dr)
{
mid = (st+dr) / 2;
if(check(mid)) dr = mid - 1;
else st = mid + 1;
}
cout << st << '\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... |