Submission #412283

# Submission time Handle Problem Language Result Execution time Memory
412283 2021-05-26T16:40:02 Z amoo_safar Arranging Tickets (JOI17_arranging_tickets) C++17
100 / 100
202 ms 24104 KB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;

const ll Mod = 1000000007LL;
const int N = 4e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

vector<pii> G[N];
int n, m;

void Add_Edge(int u, int v, int c){
	G[u].pb({v, c});
}

ll ps[N];

ll Solve(int l, int r, int dif){
	memset(ps, 0, sizeof ps);
	multiset<pii> ms;

	ll res = 0;
	for(int i = l; i < r - 1; i++){
		for(auto [x, c] : G[i]){
			if(x >= r) continue;
			ps[i] += c;
			ps[x] -= c;
			ms.insert({x, c});
		}
		ps[i] += ps[i - 1];
		while(ps[i] > dif){
			auto [fr, cnt] = *ms.rbegin();
			ms.erase(ms.find({fr, cnt}));
			if(fr <= i)
				continue;

			int del = min((ll) cnt, (ps[i] - dif + 1) / 2);
			
			ps[i] -= 2 * del;
			ps[fr] += 2 * del;
			res += del;
			if(cnt > del)
				ms.insert({fr, cnt - del});
		}
	}
	return res + dif;
}

ll df[N];

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	int a, b, c;
	for(int i = 0; i < m; i++){
		cin >> a >> b >> c;
		// assert(c == 1);
		if(a > b) swap(a, b);
		df[a] += c;
		df[b] -= c;
		
		Add_Edge(a, b, c);
		Add_Edge(b, n + a, c);
		Add_Edge(n + a, n + b, c);
	}
	for(int i = 1; i <= n; i++) df[i] += df[i - 1];
	int id_mx = max_element(df + 1, df + n) - df;
	id_mx ++;
	ll ans = min(Solve(id_mx, id_mx + n, 0), Solve(id_mx, id_mx + n, 1));

	cout << ans << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12748 KB Output is correct
2 Correct 8 ms 12748 KB Output is correct
3 Correct 9 ms 12844 KB Output is correct
4 Correct 8 ms 12748 KB Output is correct
5 Correct 8 ms 12808 KB Output is correct
6 Correct 7 ms 12748 KB Output is correct
7 Correct 9 ms 12748 KB Output is correct
8 Correct 8 ms 12748 KB Output is correct
9 Correct 8 ms 12748 KB Output is correct
10 Correct 8 ms 12748 KB Output is correct
11 Correct 9 ms 12748 KB Output is correct
12 Correct 8 ms 12748 KB Output is correct
13 Correct 9 ms 12720 KB Output is correct
14 Correct 8 ms 12748 KB Output is correct
15 Correct 8 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12748 KB Output is correct
2 Correct 8 ms 12748 KB Output is correct
3 Correct 9 ms 12844 KB Output is correct
4 Correct 8 ms 12748 KB Output is correct
5 Correct 8 ms 12808 KB Output is correct
6 Correct 7 ms 12748 KB Output is correct
7 Correct 9 ms 12748 KB Output is correct
8 Correct 8 ms 12748 KB Output is correct
9 Correct 8 ms 12748 KB Output is correct
10 Correct 8 ms 12748 KB Output is correct
11 Correct 9 ms 12748 KB Output is correct
12 Correct 8 ms 12748 KB Output is correct
13 Correct 9 ms 12720 KB Output is correct
14 Correct 8 ms 12748 KB Output is correct
15 Correct 8 ms 12748 KB Output is correct
16 Correct 9 ms 12748 KB Output is correct
17 Correct 10 ms 12748 KB Output is correct
18 Correct 9 ms 12748 KB Output is correct
19 Correct 8 ms 12756 KB Output is correct
20 Correct 8 ms 12748 KB Output is correct
21 Correct 8 ms 12748 KB Output is correct
22 Correct 8 ms 12812 KB Output is correct
23 Correct 8 ms 12820 KB Output is correct
24 Correct 8 ms 12788 KB Output is correct
25 Correct 10 ms 12748 KB Output is correct
26 Correct 8 ms 12864 KB Output is correct
27 Correct 9 ms 12876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12748 KB Output is correct
2 Correct 8 ms 12748 KB Output is correct
3 Correct 9 ms 12844 KB Output is correct
4 Correct 8 ms 12748 KB Output is correct
5 Correct 8 ms 12808 KB Output is correct
6 Correct 7 ms 12748 KB Output is correct
7 Correct 9 ms 12748 KB Output is correct
8 Correct 8 ms 12748 KB Output is correct
9 Correct 8 ms 12748 KB Output is correct
10 Correct 8 ms 12748 KB Output is correct
11 Correct 9 ms 12748 KB Output is correct
12 Correct 8 ms 12748 KB Output is correct
13 Correct 9 ms 12720 KB Output is correct
14 Correct 8 ms 12748 KB Output is correct
15 Correct 8 ms 12748 KB Output is correct
16 Correct 9 ms 12748 KB Output is correct
17 Correct 10 ms 12748 KB Output is correct
18 Correct 9 ms 12748 KB Output is correct
19 Correct 8 ms 12756 KB Output is correct
20 Correct 8 ms 12748 KB Output is correct
21 Correct 8 ms 12748 KB Output is correct
22 Correct 8 ms 12812 KB Output is correct
23 Correct 8 ms 12820 KB Output is correct
24 Correct 8 ms 12788 KB Output is correct
25 Correct 10 ms 12748 KB Output is correct
26 Correct 8 ms 12864 KB Output is correct
27 Correct 9 ms 12876 KB Output is correct
28 Correct 10 ms 13004 KB Output is correct
29 Correct 12 ms 13132 KB Output is correct
30 Correct 11 ms 13108 KB Output is correct
31 Correct 10 ms 12996 KB Output is correct
32 Correct 10 ms 13004 KB Output is correct
33 Correct 10 ms 13016 KB Output is correct
34 Correct 12 ms 13044 KB Output is correct
35 Correct 11 ms 13004 KB Output is correct
36 Correct 11 ms 13020 KB Output is correct
37 Correct 10 ms 13092 KB Output is correct
38 Correct 11 ms 13132 KB Output is correct
39 Correct 10 ms 13132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12748 KB Output is correct
2 Correct 8 ms 12748 KB Output is correct
3 Correct 9 ms 12844 KB Output is correct
4 Correct 8 ms 12748 KB Output is correct
5 Correct 8 ms 12808 KB Output is correct
6 Correct 7 ms 12748 KB Output is correct
7 Correct 9 ms 12748 KB Output is correct
8 Correct 8 ms 12748 KB Output is correct
9 Correct 8 ms 12748 KB Output is correct
10 Correct 8 ms 12748 KB Output is correct
11 Correct 9 ms 12748 KB Output is correct
12 Correct 8 ms 12748 KB Output is correct
13 Correct 9 ms 12720 KB Output is correct
14 Correct 8 ms 12748 KB Output is correct
15 Correct 8 ms 12748 KB Output is correct
16 Correct 9 ms 12748 KB Output is correct
17 Correct 10 ms 12748 KB Output is correct
18 Correct 9 ms 12748 KB Output is correct
19 Correct 8 ms 12756 KB Output is correct
20 Correct 8 ms 12748 KB Output is correct
21 Correct 8 ms 12748 KB Output is correct
22 Correct 8 ms 12812 KB Output is correct
23 Correct 8 ms 12820 KB Output is correct
24 Correct 8 ms 12788 KB Output is correct
25 Correct 10 ms 12748 KB Output is correct
26 Correct 8 ms 12864 KB Output is correct
27 Correct 9 ms 12876 KB Output is correct
28 Correct 10 ms 13004 KB Output is correct
29 Correct 12 ms 13132 KB Output is correct
30 Correct 11 ms 13108 KB Output is correct
31 Correct 10 ms 12996 KB Output is correct
32 Correct 10 ms 13004 KB Output is correct
33 Correct 10 ms 13016 KB Output is correct
34 Correct 12 ms 13044 KB Output is correct
35 Correct 11 ms 13004 KB Output is correct
36 Correct 11 ms 13020 KB Output is correct
37 Correct 10 ms 13092 KB Output is correct
38 Correct 11 ms 13132 KB Output is correct
39 Correct 10 ms 13132 KB Output is correct
40 Correct 180 ms 23592 KB Output is correct
41 Correct 186 ms 23604 KB Output is correct
42 Correct 180 ms 23236 KB Output is correct
43 Correct 172 ms 22992 KB Output is correct
44 Correct 171 ms 23108 KB Output is correct
45 Correct 150 ms 22668 KB Output is correct
46 Correct 153 ms 22644 KB Output is correct
47 Correct 201 ms 22676 KB Output is correct
48 Correct 172 ms 22552 KB Output is correct
49 Correct 141 ms 21860 KB Output is correct
50 Correct 141 ms 24104 KB Output is correct
51 Correct 134 ms 22212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12748 KB Output is correct
2 Correct 8 ms 12748 KB Output is correct
3 Correct 9 ms 12844 KB Output is correct
4 Correct 8 ms 12748 KB Output is correct
5 Correct 8 ms 12808 KB Output is correct
6 Correct 7 ms 12748 KB Output is correct
7 Correct 9 ms 12748 KB Output is correct
8 Correct 8 ms 12748 KB Output is correct
9 Correct 8 ms 12748 KB Output is correct
10 Correct 8 ms 12748 KB Output is correct
11 Correct 9 ms 12748 KB Output is correct
12 Correct 8 ms 12748 KB Output is correct
13 Correct 9 ms 12720 KB Output is correct
14 Correct 8 ms 12748 KB Output is correct
15 Correct 8 ms 12748 KB Output is correct
16 Correct 9 ms 12748 KB Output is correct
17 Correct 10 ms 12748 KB Output is correct
18 Correct 9 ms 12748 KB Output is correct
19 Correct 8 ms 12756 KB Output is correct
20 Correct 8 ms 12748 KB Output is correct
21 Correct 8 ms 12748 KB Output is correct
22 Correct 8 ms 12812 KB Output is correct
23 Correct 8 ms 12820 KB Output is correct
24 Correct 8 ms 12788 KB Output is correct
25 Correct 10 ms 12748 KB Output is correct
26 Correct 8 ms 12864 KB Output is correct
27 Correct 9 ms 12876 KB Output is correct
28 Correct 10 ms 13004 KB Output is correct
29 Correct 12 ms 13132 KB Output is correct
30 Correct 11 ms 13108 KB Output is correct
31 Correct 10 ms 12996 KB Output is correct
32 Correct 10 ms 13004 KB Output is correct
33 Correct 10 ms 13016 KB Output is correct
34 Correct 12 ms 13044 KB Output is correct
35 Correct 11 ms 13004 KB Output is correct
36 Correct 11 ms 13020 KB Output is correct
37 Correct 10 ms 13092 KB Output is correct
38 Correct 11 ms 13132 KB Output is correct
39 Correct 10 ms 13132 KB Output is correct
40 Correct 180 ms 23592 KB Output is correct
41 Correct 186 ms 23604 KB Output is correct
42 Correct 180 ms 23236 KB Output is correct
43 Correct 172 ms 22992 KB Output is correct
44 Correct 171 ms 23108 KB Output is correct
45 Correct 150 ms 22668 KB Output is correct
46 Correct 153 ms 22644 KB Output is correct
47 Correct 201 ms 22676 KB Output is correct
48 Correct 172 ms 22552 KB Output is correct
49 Correct 141 ms 21860 KB Output is correct
50 Correct 141 ms 24104 KB Output is correct
51 Correct 134 ms 22212 KB Output is correct
52 Correct 195 ms 23704 KB Output is correct
53 Correct 193 ms 23160 KB Output is correct
54 Correct 188 ms 23512 KB Output is correct
55 Correct 194 ms 22860 KB Output is correct
56 Correct 202 ms 22960 KB Output is correct
57 Correct 175 ms 22780 KB Output is correct
58 Correct 185 ms 22912 KB Output is correct
59 Correct 142 ms 23024 KB Output is correct