Skip to content

Commit 856b42b

Browse files
committed
Problem 6 ACM ICPC Regionals
1 parent 570591f commit 856b42b

File tree

7 files changed

+226
-76
lines changed

7 files changed

+226
-76
lines changed
Binary file not shown.

CP/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
for i in xrange(3):
1010
b[i]+=abs(m)
1111
[x1,x2,x3]=sorted(b)
12-
if (k >= (x3-x1)/2):
12+
if (k >= (x3-x1)/2): # k is the move we can take
1313
print float(a*a)
1414
else:
1515
if x3-x1-2*k>a: print float(0)
Lines changed: 60 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,74 @@
1+
// Still unsolved What the fuck !
2+
13
#include <bits/stdc++.h>
2-
#define F first
3-
#define S second
44
using namespace std;
55

6-
typedef long long ll;
7-
typedef pair<int, int> pii;
6+
#define ll long long
7+
8+
const int N = 2e3 + 5;
89

9-
ll y[4][2010], sz[4];
10+
ll cnt[5], sum[5];
11+
vector<int> a[5];
12+
ll pre[N];
13+
14+
void solve() {
15+
16+
for(int i = 0; i < 3; i++) {
17+
a[i].clear();
18+
cnt[i] = 0;
19+
sum[i] = 0;
20+
}
21+
22+
int n, x, y;
23+
24+
scanf("%d", &n);
25+
for(int i = 1; i <= n; i++) {
26+
scanf("%d %d", &x, &y);
27+
x--, y--;
28+
a[x].push_back(y);
29+
sum[x] += y;
30+
cnt[x]++;
31+
}
1032

11-
ll case1(int l, int r) {
1233
ll ans = 0;
13-
int i, j;
14-
for(i = 1; i <= sz[l]; ++i) {
15-
ll cur = 0, cnt = 0;
16-
for(j = 1; j <= sz[r]; ++j) {
17-
ll z = abs(r - l)*abs(y[r][j] - y[l][i]) + 2*abs(r - l)*min(y[r][j], y[l][i]);
18-
ans += cnt*z - cur;
19-
cur += z;
20-
cnt += 1;
34+
35+
for(int i = 0; i < 3; i++) {
36+
sort(a[i].begin(), a[i].end());
37+
}
38+
39+
for(int i = 0; i < 3; i++) {
40+
for(int j = 0; j < 3; j++) {
41+
if(i == j) continue;
42+
for(int k = 0; k < a[i].size(); k++) {
43+
for(int p = k + 1; p < a[i].size(); p++) {
44+
ans += a[j].size() * 1LL * abs((a[i][p] - a[i][k]) * 1LL * (i - j));
45+
}
46+
}
47+
}
48+
}
49+
50+
pre[0] = 0;
51+
for(int i = 0; i < a[1].size(); i++) {
52+
pre[i + 1] = pre[i] + a[1][i];
53+
}
54+
55+
for(int i = 0; i < a[0].size(); i++) {
56+
int start = 0;
57+
for(int j = 0; j < a[2].size(); j++) {
58+
ll tot = a[0][i] + a[2][j];
59+
while(start < cnt[1] && 2*a[1][start] < tot) start++;
60+
ll contri = ((tot * start) - (2LL * pre[start])) + ((2LL * (pre[cnt[1]] - pre[start])) - (tot * (cnt[1] - start)));
61+
ans += contri;
2162
}
2263
}
23-
return ans;
64+
double res = double(ans) / 2.0;
65+
cout<<setprecision(7)<<fixed<<res<<'\n';
2466
}
2567

2668
int main() {
27-
int t, i, j;
28-
// freopen("in.txt", "r", stdin);
69+
int t;
2970
scanf("%d", &t);
3071
while(t--) {
31-
int n, u, v;
32-
scanf("%d", &n);
33-
sz[1] = sz[2] = sz[3] = 0;
34-
for(i = 1; i <= n; ++i) {
35-
scanf("%d %d", &u, &v);
36-
sz[u]++;
37-
y[u][sz[u]] = v;
38-
}
39-
for(i = 1; i <= 3; ++i) {
40-
sort(y[i] + 1, y[i] + sz[i] + 1);
41-
}
42-
ll ans = 0;
43-
//Case 1 and 2
44-
ans += case1(1, 2);
45-
//Case 2 and 1
46-
ans += case1(2, 1);
47-
// Case 2 and 3
48-
ans += case1(2, 3);
49-
// Case 3 and 2
50-
ans += case1(3, 2);
51-
// Case 1 and 3
52-
ans += case1(1, 3);
53-
// Case 3 and 1
54-
ans += case1(3, 1);
55-
// cout << ans << endl;
56-
// Case 1, 2 and 3
57-
for(i = 1; i <= sz[1]; ++i) {
58-
ll cur = 0, cnt = 0, ptr = 1;
59-
for(j = 1; j <= sz[2]; ++j) {
60-
ll z = abs(y[2][j] - y[1][i]) + 2*min(y[2][j], y[1][i]);
61-
while(ptr <= sz[3] and y[3][ptr] < y[2][j] + y[2][j] - y[1][i]) ptr++;
62-
ans += (ptr - 1)*z - (sz[3] - ptr + 1)*z;
63-
// cout << "in " << ptr << endl;
64-
}
65-
}
66-
// cout << ans << endl;
67-
for(i = 1; i <= sz[3]; ++i) {
68-
ll cur = 0, cnt = 0, ptr = 1;
69-
for(j = 1; j <= sz[2]; ++j) {
70-
ll z = abs(y[2][j] - y[3][i]) + 2*min(y[2][j], y[3][i]);
71-
while(ptr <= sz[1] and y[1][ptr] < y[2][j] + y[2][j] - y[3][i]) ptr++;
72-
ans += (ptr - 1)*z - (sz[1] - ptr + 1)*z;
73-
}
74-
}
75-
// cout << ans << endl;
76-
for(i = 1; i <= sz[1]; ++i) {
77-
ll cur = 0, cnt = 0, ptr = 1;
78-
for(j = 1; j <= sz[3]; ++j) {
79-
ll z = 2*abs(y[3][j] - y[1][i]) + 2*2*min(y[3][j], y[1][i]);
80-
while(ptr <= sz[2] and y[2][ptr] + y[2][ptr] <= y[1][i] + y[3][j]) ptr++;
81-
ans += (ptr - 1)*z - (sz[2] - ptr + 1)*z;
82-
}
83-
}
84-
// cout << ans << endl;
85-
long double fin = (long double)ans/2.0;
86-
printf("%.3Lf\n", fin);
72+
solve();
8773
}
88-
return 0;
89-
}
74+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
for iii in range(int(input())):
2+
a,b,c,d,e,f,l,r=(map(int,raw_input().strip().split(' ')))
3+
temp=max(l,r)
4+
tp=min(l,r)
5+
l=temp
6+
r=tp
7+
if(b>a):
8+
if(e>d):
9+
if(max(b,e)<=l and max(a,d)<=r):
10+
C=[0,0]
11+
A=[0,b]
12+
B=[a,0]
13+
F=[r,l]
14+
E=[r-d,l]
15+
D=[r,l-e]
16+
arr=[A,B,C,D,E,F]
17+
for i in range(6):
18+
p=arr[i]
19+
print p[0],p[1]
20+
else:
21+
print(-1)
22+
else:
23+
if(max(b,d)<=l and max(a,e)<=r):
24+
C=[0,0]
25+
A=[0,b]
26+
B=[a,0]
27+
D=[r-e,l]
28+
E=[r,l-d]
29+
F=[r,l]
30+
arr=[A,B,C,D,E,F]
31+
for i in range(6):
32+
p=arr[i]
33+
print p[0],p[1]
34+
35+
else:
36+
print(-1)
37+
else:
38+
if(e>d):
39+
if(max(a,e)<=l and max(b,d)<=r):
40+
C=[0,0]
41+
A=[b,0]
42+
B=[0,a]
43+
F=[r,l]
44+
E=[r-d,l]
45+
D=[r,l-e]
46+
arr=[A,B,C,D,E,F]
47+
for i in range(6):
48+
p=arr[i]
49+
print p[0],p[1]
50+
else:
51+
print(-1)
52+
else:
53+
if(max(a,d)<=l and max(b,e)<=r):
54+
C=[0,0]
55+
A=[b,0]
56+
B=[0,a]
57+
D=[r-e,l]
58+
E=[r,l-d]
59+
F=[r,l]
60+
arr=[A,B,C,D,E,F]
61+
for i in range(6):
62+
p=arr[i]
63+
print p[0],p[1]
64+
else:
65+
print(-1)

CP/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob6.py

Whitespace-only changes.

CP/templates/temp.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define ll long long
5+
6+
const int N = 2e3 + 5;
7+
8+
ll cnt[5], sum[5];
9+
vector<int> a[5];
10+
ll pre[N];
11+
12+
void solve() {
13+
14+
for(int i = 0; i < 3; i++) {
15+
a[i].clear();
16+
cnt[i] = 0;
17+
sum[i] = 0;
18+
}
19+
20+
int n, x, y;
21+
22+
scanf("%d", &n);
23+
for(int i = 1; i <= n; i++) {
24+
scanf("%d %d", &x, &y);
25+
x--, y--;
26+
a[x].push_back(y);
27+
sum[x] += y;
28+
cnt[x]++;
29+
}
30+
31+
ll ans = 0;
32+
33+
for(int i = 0; i < 3; i++) {
34+
sort(a[i].begin(), a[i].end());
35+
}
36+
37+
for(int i = 0; i < 3; i++) {
38+
for(int j = 0; j < 3; j++) {
39+
if(i == j) continue;
40+
for(int k = 0; k < a[i].size(); k++) {
41+
for(int p = k + 1; p < a[i].size(); p++) {
42+
ans += a[j].size() * 1LL * abs((a[i][p] - a[i][k]) * 1LL * (i - j));
43+
}
44+
}
45+
}
46+
}
47+
48+
pre[0] = 0;
49+
for(int i = 0; i < a[1].size(); i++) {
50+
pre[i + 1] = pre[i] + a[1][i];
51+
}
52+
53+
for(int i = 0; i < a[0].size(); i++) {
54+
int start = 0;
55+
for(int j = 0; j < a[2].size(); j++) {
56+
ll tot = a[0][i] + a[2][j];
57+
while(start < cnt[1] && 2*a[1][start] < tot) start++;
58+
ll contri = ((tot * start) - (2LL * pre[start])) + ((2LL * (pre[cnt[1]] - pre[start])) - (tot * (cnt[1] - start)));
59+
ans += contri;
60+
}
61+
}
62+
double res = double(ans) / 2.0;
63+
cout<<setprecision(7)<<fixed<<res<<'\n';
64+
}
65+
66+
int main() {
67+
int t;
68+
scanf("%d", &t);
69+
while(t--) {
70+
solve();
71+
}
72+
}

CP/templates/temp.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
t=input()
2+
for i in range(t):
3+
n=input()
4+
arr=map(int,raw_input().split())
5+
s=sum(arr)
6+
if s%4!=0:
7+
print -1
8+
else:
9+
a,b,c=[0,0,0]
10+
for i in arr:
11+
if i%4==1:
12+
a+=1
13+
elif i%4==2:
14+
b+=1
15+
elif i%4==3:
16+
c+=1
17+
#print a,b,c
18+
#a,b,c=[2,5,8]
19+
ans=0
20+
ans+=min(a,c)
21+
ans+=b/2
22+
oth=max(a,c)-min(a,c)
23+
#print a,b,c
24+
if b%2==0:
25+
ans+=3*(oth/4)
26+
else:
27+
ans+=2+3*((oth-2)/4)
28+
print ans

0 commit comments

Comments
 (0)