제출 #116340

#제출 시각아이디문제언어결과실행 시간메모리
116340shayan_pArranging Tickets (JOI17_arranging_tickets)C++14
100 / 100
2781 ms38800 KiB
// High above the clouds there is a rainbow...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,ll> pil;

const int maxn=2e5+10,SQ=50;
const ll inf=1e18;

int a[maxn],b[maxn],_a[maxn],_b[maxn];
ll c[maxn],arr[maxn],arr2[maxn];

int pp[maxn];

vector<pil>v[maxn];
priority_queue<pil> pq;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);

    srand(time(0));
    
    int n,m; cin>>n>>m;
    for(int i=0;i<m;i++){
	cin>>_a[i]>>_b[i]>>c[i], --_a[i],--_b[i];
    }
    for(int i=0;i<n;i++){
	pp[i]=i;
    }
    random_shuffle(pp,pp+n);

    ll ans=inf;
    
    for(int w=0;w<min(SQ,n);w++){
	
	memset(arr,0,sizeof arr);
	memset(arr2,0,sizeof arr2);
	for(int i=0;i<n;i++){
	    v[i].clear();
	}
	while(sz(pq)){
	    pq.pop();
	}
	
	for(int i=0;i<m;i++){
	    a[i]=(_a[i]+pp[w])%n, b[i]=(_b[i]+pp[w])%n;
	    if(a[i]>b[i]) swap(a[i],b[i]);
	    v[a[i]].PB({b[i],c[i]});
	    arr[a[i]]+=c[i], arr[b[i]]-=c[i];
	}
	for(int i=1;i<n;i++){
	    arr[i]+=arr[i-1];
	}
	for(int i=0;i<n;i++){
	    arr[i]=(1+arr[i])/2;
	}
	
	ll cnt=0,nw=0;
	for(int i=0;i<n;i++){
	    for(auto x:v[i]){
		pq.push(x);	
	    }
	    nw+=arr2[i];
	    
	    while(arr[i]-nw>0){
		pil x=pq.top();
		pq.pop();
		
		ll num= min(arr[i]-nw,x.S);
		x.S-=num, nw+=num, arr2[x.F]-=num, cnt+=num;
		if(x.S>0) pq.push(x);
	    }
	}

	ans=min(ans,cnt);
    }
    return cout<<ans<<endl,0;
}
// Deathly mistakes:
//  * Read the problem curfully.
//  * Check maxn.
//  * Overflows.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...