Submission #25194

# Submission time Handle Problem Language Result Execution time Memory
25194 2017-06-20T17:02:40 Z minimario Tug of War (BOI15_tug) C++14
100 / 100
296 ms 27440 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int MAX = 30005;
 
set<int> left1[MAX];
set<int> right1[MAX];
int l[2*MAX], r[2*MAX], s[2*MAX]; // the people
 
int leftpts = 0;
int rightpts = 0;
int ropel[MAX], roper[MAX];
 
void nosolution() {
	printf("NO\n"); exit(0);
}
int main() {
	//freopen("a.in", "r", stdin);
	//freopen("a.out", "w", stdout);
	int n, k; scanf("%d %d", &n, &k);
	set<int> students;
	for (int i=1; i<=2*n; i++) {
		scanf("%d %d %d", &l[i], &r[i], &s[i]);
		right1[r[i]].insert(i);
		left1[l[i]].insert(i);
		students.insert(i);
	}
	set<pair<int, int>> left_data, right_data;
 
	for (int i=1; i<=n; i++) {
		if (left1[i].empty() || right1[i].empty()) {
			nosolution();
		}
		left_data.insert({left1[i].size(), i});
		right_data.insert({right1[i].size(), i});
	}
 
	bool repeat = true;
	while (repeat) {
		repeat = false;
		if (!left_data.empty() && left_data.begin()->first <= 0) {
			nosolution();
		}
		if (!left_data.empty() && left_data.begin()->first == 1) {
			pair<int, int> fixed = *left_data.begin();
			int pos = fixed.second;
			int student = *(left1[pos].begin());
			leftpts += s[student];
			left_data.erase(fixed);
			left1[pos].erase(student);
			students.erase(student);
			ropel[pos] = student; // assign the student to the position on the rope
			int rightpos = r[student];
			right_data.erase({right1[rightpos].size(), rightpos});
			right1[rightpos].erase(student);
			right_data.insert({right1[rightpos].size(), rightpos});
			repeat = true;
		}
		if (!right_data.empty() && right_data.begin()->first <= 0) {
			nosolution();
		}
		if (!right_data.empty() && right_data.begin()->first == 1) {
			pair<int, int> fixed = *right_data.begin();
			int pos = fixed.second;
			int student = *right1[pos].begin();
			rightpts += s[student];
			right_data.erase(fixed);
			right1[pos].erase(student);
			students.erase(student);
			roper[pos] = student; // assign the student to the position on the rope
			int leftpos = l[student];
			left_data.erase({left1[leftpos].size(), leftpos});
			left1[leftpos].erase(student);
			left_data.insert({left1[leftpos].size(), leftpos});
			repeat = true;
		}
	}
	// for (int i : students) { cout << i << " "; } 
	set<int> lefts; // the collection of left values
	set<int> rights;
	vector<pair<int, int> > matches[MAX]; // the 2 pairs (right, strength) of a given left
	vector<int> inverse[MAX]; // the left values with a right value of i
	vector<pair<int, pair<int, int> > > g[MAX]; // [dest, [w1, w2]]
	bool v[MAX];
 
	for (int i : students) {
		lefts.insert(l[i]);
		rights.insert(r[i]);
		matches[l[i]].push_back({r[i], s[i]});
		inverse[r[i]].push_back(l[i]);
	}
 
	for (int i : lefts) { 
		pair<int, int> e1 = matches[i][0];
		pair<int, int> e2 = matches[i][1];
		//cout << "left value: " << i << endl;
		//cout << "two rights: " << e1.first << " " << e2.first << endl;
		//cout << "strengths: " << e1.second << " " << e2.second << endl;
 
		g[e1.first].push_back({e2.first, {e1.second, e2.second}});
		g[e2.first].push_back({e1.first, {e2.second, e1.second}});
	}
 
	vector<int> choices;
	for (int i : rights) {
		if (v[i]) { continue; }
		// cout << "cycling from... " << i << endl;
		v[i] = true;
		int start = i;
		if (g[start][0].first == g[start][1].first && g[start][0].first == start) {
			choices.push_back(abs(g[start][0].second.first-g[start][0].second.second));
			// cout << "choices" << endl;
			// for (int i : choices) { cout << i << " "; } cout << endl;	
			continue;
		}
		else {
			// cout << "next level: " << i << endl;
			int sum1 = g[start][0].second.first;
			int sum2 = g[start][0].second.second;
			int last = start;
			int next = g[start][0].first;
			int its = 0;
			
			while (next != start) {
 
				v[next] = true;
				if (g[next][0].first != last) {
					sum1 += g[next][0].second.first;
					sum2 += g[next][0].second.second;
					last = next;
					next = g[next][0].first;
				}
				else if (g[next][1].first != last) {
					sum1 += g[next][1].second.first;
					sum2 += g[next][1].second.second;
					last = next;
					next = g[next][1].first;
				}
				else { 
					//cout << start << endl;
					//cout << g[start][0].second.first << " " << g[start][0].second.second << "." << endl;
					//cout << g[start][1].second.first << " " << g[start][1].second.second << "." << endl;
					sum1 = g[start][0].second.first + g[start][1].second.second;
					sum2 = g[start][1].second.first + g[start][0].second.second;
					//cout << sum1 << " " << sum2 << endl;
					break;
				}
			}
			choices.push_back(abs(sum1-sum2));
		}
	}
	int ct[600005];
	for (int i=0; i<600005; i++) { ct[i] = 0; }
 
	int t = 0;
	for (int i : choices) { ct[i]++; t += i; }
	for (int i=1; i<=600000; i++) {
		if (!ct[i]) { continue; }
		if (ct[i]%2) { ct[2*i] += ct[i]/2; ct[i] = 1; }
		else { ct[2*i] += ct[i]/2-1; ct[i] = 2; }
	}
	bitset<600005> dp;
	dp[0] = 1;
	for (int i=1; i<=600005; i++) {
		if (!ct[i]) { continue; }
		for (int j=0; j<ct[i]; j++) {
			dp |= (dp << i); 
		}
	}
	// cout << leftpts - rightpts << endl;
	for (int i=0; i<=600000; i++) {
		if (!dp[i]) { continue; }
		int score = (leftpts - rightpts) + i;
		int diff = abs(score - (t - i));
		// cout << i << " " << diff << endl;
		if (-k <= diff && diff <= k) {
			printf("YES\n"); return 0;
		}
	}
	printf("NO\n"); return 0;	
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:123:8: warning: unused variable 'its' [-Wunused-variable]
    int its = 0;
        ^
tug.cpp:21:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, k; scanf("%d %d", &n, &k);
                                  ^
tug.cpp:24:41: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &l[i], &r[i], &s[i]);
                                         ^
tug.cpp:166:12: warning: iteration 600004u invokes undefined behavior [-Waggressive-loop-optimizations]
   if (!ct[i]) { continue; }
            ^
tug.cpp:165:17: note: containing loop
  for (int i=1; i<=600005; i++) {
                 ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10360 KB Output is correct
2 Correct 3 ms 10356 KB Output is correct
3 Correct 6 ms 10356 KB Output is correct
4 Correct 3 ms 10352 KB Output is correct
5 Correct 6 ms 10360 KB Output is correct
6 Correct 6 ms 10352 KB Output is correct
7 Correct 6 ms 10356 KB Output is correct
8 Correct 6 ms 10356 KB Output is correct
9 Correct 3 ms 10356 KB Output is correct
10 Correct 6 ms 10352 KB Output is correct
11 Correct 3 ms 10360 KB Output is correct
12 Correct 6 ms 10356 KB Output is correct
13 Correct 0 ms 10356 KB Output is correct
14 Correct 6 ms 10356 KB Output is correct
15 Correct 0 ms 10352 KB Output is correct
16 Correct 3 ms 10360 KB Output is correct
17 Correct 3 ms 10360 KB Output is correct
18 Correct 3 ms 10356 KB Output is correct
19 Correct 3 ms 10356 KB Output is correct
20 Correct 3 ms 10360 KB Output is correct
21 Correct 0 ms 10356 KB Output is correct
22 Correct 6 ms 10356 KB Output is correct
23 Correct 3 ms 10356 KB Output is correct
24 Correct 3 ms 10356 KB Output is correct
25 Correct 3 ms 10356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10360 KB Output is correct
2 Correct 3 ms 10356 KB Output is correct
3 Correct 6 ms 10356 KB Output is correct
4 Correct 3 ms 10352 KB Output is correct
5 Correct 6 ms 10360 KB Output is correct
6 Correct 6 ms 10352 KB Output is correct
7 Correct 6 ms 10356 KB Output is correct
8 Correct 6 ms 10356 KB Output is correct
9 Correct 3 ms 10356 KB Output is correct
10 Correct 6 ms 10352 KB Output is correct
11 Correct 3 ms 10360 KB Output is correct
12 Correct 6 ms 10356 KB Output is correct
13 Correct 0 ms 10356 KB Output is correct
14 Correct 6 ms 10356 KB Output is correct
15 Correct 0 ms 10352 KB Output is correct
16 Correct 3 ms 10360 KB Output is correct
17 Correct 3 ms 10360 KB Output is correct
18 Correct 3 ms 10356 KB Output is correct
19 Correct 3 ms 10356 KB Output is correct
20 Correct 3 ms 10360 KB Output is correct
21 Correct 0 ms 10356 KB Output is correct
22 Correct 6 ms 10356 KB Output is correct
23 Correct 3 ms 10356 KB Output is correct
24 Correct 3 ms 10356 KB Output is correct
25 Correct 3 ms 10356 KB Output is correct
26 Correct 16 ms 11548 KB Output is correct
27 Correct 23 ms 11548 KB Output is correct
28 Correct 13 ms 11548 KB Output is correct
29 Correct 16 ms 11544 KB Output is correct
30 Correct 16 ms 11548 KB Output is correct
31 Correct 23 ms 11540 KB Output is correct
32 Correct 9 ms 11544 KB Output is correct
33 Correct 23 ms 11548 KB Output is correct
34 Correct 9 ms 11408 KB Output is correct
35 Correct 16 ms 11544 KB Output is correct
36 Correct 13 ms 11544 KB Output is correct
37 Correct 23 ms 11548 KB Output is correct
38 Correct 6 ms 11548 KB Output is correct
39 Correct 33 ms 11540 KB Output is correct
40 Correct 13 ms 11540 KB Output is correct
41 Correct 23 ms 11540 KB Output is correct
42 Correct 19 ms 11548 KB Output is correct
43 Correct 33 ms 11544 KB Output is correct
44 Correct 19 ms 11540 KB Output is correct
45 Correct 23 ms 11548 KB Output is correct
46 Correct 13 ms 11548 KB Output is correct
47 Correct 6 ms 10880 KB Output is correct
48 Correct 9 ms 11276 KB Output is correct
49 Correct 16 ms 11280 KB Output is correct
50 Correct 13 ms 11540 KB Output is correct
51 Correct 13 ms 11540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 16180 KB Output is correct
2 Correct 23 ms 13128 KB Output is correct
3 Correct 53 ms 16180 KB Output is correct
4 Correct 29 ms 13132 KB Output is correct
5 Correct 59 ms 16184 KB Output is correct
6 Correct 26 ms 13128 KB Output is correct
7 Correct 63 ms 16188 KB Output is correct
8 Correct 23 ms 13132 KB Output is correct
9 Correct 53 ms 16184 KB Output is correct
10 Correct 19 ms 13128 KB Output is correct
11 Correct 49 ms 16184 KB Output is correct
12 Correct 26 ms 13132 KB Output is correct
13 Correct 49 ms 16184 KB Output is correct
14 Correct 63 ms 16180 KB Output is correct
15 Correct 19 ms 13128 KB Output is correct
16 Correct 53 ms 16188 KB Output is correct
17 Correct 23 ms 13132 KB Output is correct
18 Correct 49 ms 16184 KB Output is correct
19 Correct 33 ms 13124 KB Output is correct
20 Correct 49 ms 16184 KB Output is correct
21 Correct 26 ms 13124 KB Output is correct
22 Correct 59 ms 14316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10360 KB Output is correct
2 Correct 3 ms 10356 KB Output is correct
3 Correct 6 ms 10356 KB Output is correct
4 Correct 3 ms 10352 KB Output is correct
5 Correct 6 ms 10360 KB Output is correct
6 Correct 6 ms 10352 KB Output is correct
7 Correct 6 ms 10356 KB Output is correct
8 Correct 6 ms 10356 KB Output is correct
9 Correct 3 ms 10356 KB Output is correct
10 Correct 6 ms 10352 KB Output is correct
11 Correct 3 ms 10360 KB Output is correct
12 Correct 6 ms 10356 KB Output is correct
13 Correct 0 ms 10356 KB Output is correct
14 Correct 6 ms 10356 KB Output is correct
15 Correct 0 ms 10352 KB Output is correct
16 Correct 3 ms 10360 KB Output is correct
17 Correct 3 ms 10360 KB Output is correct
18 Correct 3 ms 10356 KB Output is correct
19 Correct 3 ms 10356 KB Output is correct
20 Correct 3 ms 10360 KB Output is correct
21 Correct 0 ms 10356 KB Output is correct
22 Correct 6 ms 10356 KB Output is correct
23 Correct 3 ms 10356 KB Output is correct
24 Correct 3 ms 10356 KB Output is correct
25 Correct 3 ms 10356 KB Output is correct
26 Correct 16 ms 11548 KB Output is correct
27 Correct 23 ms 11548 KB Output is correct
28 Correct 13 ms 11548 KB Output is correct
29 Correct 16 ms 11544 KB Output is correct
30 Correct 16 ms 11548 KB Output is correct
31 Correct 23 ms 11540 KB Output is correct
32 Correct 9 ms 11544 KB Output is correct
33 Correct 23 ms 11548 KB Output is correct
34 Correct 9 ms 11408 KB Output is correct
35 Correct 16 ms 11544 KB Output is correct
36 Correct 13 ms 11544 KB Output is correct
37 Correct 23 ms 11548 KB Output is correct
38 Correct 6 ms 11548 KB Output is correct
39 Correct 33 ms 11540 KB Output is correct
40 Correct 13 ms 11540 KB Output is correct
41 Correct 23 ms 11540 KB Output is correct
42 Correct 19 ms 11548 KB Output is correct
43 Correct 33 ms 11544 KB Output is correct
44 Correct 19 ms 11540 KB Output is correct
45 Correct 23 ms 11548 KB Output is correct
46 Correct 13 ms 11548 KB Output is correct
47 Correct 6 ms 10880 KB Output is correct
48 Correct 9 ms 11276 KB Output is correct
49 Correct 16 ms 11280 KB Output is correct
50 Correct 13 ms 11540 KB Output is correct
51 Correct 13 ms 11540 KB Output is correct
52 Correct 56 ms 16180 KB Output is correct
53 Correct 23 ms 13128 KB Output is correct
54 Correct 53 ms 16180 KB Output is correct
55 Correct 29 ms 13132 KB Output is correct
56 Correct 59 ms 16184 KB Output is correct
57 Correct 26 ms 13128 KB Output is correct
58 Correct 63 ms 16188 KB Output is correct
59 Correct 23 ms 13132 KB Output is correct
60 Correct 53 ms 16184 KB Output is correct
61 Correct 19 ms 13128 KB Output is correct
62 Correct 49 ms 16184 KB Output is correct
63 Correct 26 ms 13132 KB Output is correct
64 Correct 49 ms 16184 KB Output is correct
65 Correct 63 ms 16180 KB Output is correct
66 Correct 19 ms 13128 KB Output is correct
67 Correct 53 ms 16188 KB Output is correct
68 Correct 23 ms 13132 KB Output is correct
69 Correct 49 ms 16184 KB Output is correct
70 Correct 33 ms 13124 KB Output is correct
71 Correct 49 ms 16184 KB Output is correct
72 Correct 26 ms 13124 KB Output is correct
73 Correct 59 ms 14316 KB Output is correct
74 Correct 229 ms 27440 KB Output is correct
75 Correct 243 ms 27256 KB Output is correct
76 Correct 203 ms 27440 KB Output is correct
77 Correct 256 ms 27256 KB Output is correct
78 Correct 189 ms 27436 KB Output is correct
79 Correct 226 ms 27436 KB Output is correct
80 Correct 246 ms 27248 KB Output is correct
81 Correct 206 ms 27120 KB Output is correct
82 Correct 116 ms 27440 KB Output is correct
83 Correct 226 ms 27440 KB Output is correct
84 Correct 269 ms 27252 KB Output is correct
85 Correct 163 ms 27440 KB Output is correct
86 Correct 296 ms 27256 KB Output is correct
87 Correct 183 ms 27436 KB Output is correct
88 Correct 249 ms 27252 KB Output is correct
89 Correct 206 ms 27436 KB Output is correct
90 Correct 256 ms 27252 KB Output is correct
91 Correct 246 ms 27436 KB Output is correct
92 Correct 266 ms 27256 KB Output is correct
93 Correct 243 ms 27436 KB Output is correct
94 Correct 83 ms 18804 KB Output is correct
95 Correct 286 ms 21708 KB Output is correct
96 Correct 279 ms 21704 KB Output is correct
97 Correct 236 ms 27436 KB Output is correct
98 Correct 183 ms 27436 KB Output is correct