Submission #55009

#TimeUsernameProblemLanguageResultExecution timeMemory
55009ksun48Arranging Tickets (JOI17_arranging_tickets)C++14
0 / 100
4001 ms772 KiB

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

LL brute(vector<vector<LL> > z, LL n){
	LL best = z.size();
	for(LL r = 0; r < (1 << z.size()); r++){
		vector<LL> freq(n,0);
		for(int j = 0; j < z.size(); j++){
			LL c = z[j][0];
			LL d = z[j][1];
			if(r & (1 << j)){
				swap(c,d);
			}
			while(c != d){
				freq[c]++;
				c++;
				c %= n;
			}
		}
		LL maxused = 0;
		for(int j = 0; j < n; j++){
			maxused = max(maxused, freq[j]);
		}
		best = min(best, maxused);
	}
	return best;	
}

LL two(vector<vector<LL> > z, LL n){
	LL bound = 0;
	vector<int> a(2);
	for(a[0] = 0; a[0] < n; a[0]++){
		for(a[1] = 0; a[1] < n; a[1]++){
			LL cross = 0;
			for(int j = 0; j < z.size(); j++){
				int parity = 0;
				LL c = z[j][0];
				LL d = z[j][1];
				while(c != d){
					if(c == a[0] || c == a[1]) parity ^= 1;
					c++;
					c %= n;
				}
				cross += parity;
			}
			bound = max(bound, (cross+1)/2);
		}
	}
	return bound;	
}

int main(){
	cin.sync_with_stdio(0); cin.tie(0);
	LL n, m;
	cin >> n >> m;
	// stations 1 to n
	vector<vector<LL> > z;
	for(int i = 0; i < m; i++){
		LL a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		for(int j = 0; j < c; j++){
			z.push_back(vector<LL>({a,b}));
		}
	}
	LL bound = two(z, n);
	LL ans = brute(z, n);
	assert(ans <= bound);
	assert(bound - 1 <= ans);
	cout << ans << '\n';
}
#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...