forked from harrypotter0/algorithms-in-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
570591f
commit 856b42b
Showing
7 changed files
with
226 additions
and
76 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 60 additions & 75 deletions
135
CP/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob4.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,74 @@ | ||
// Still unsolved What the fuck ! | ||
|
||
#include <bits/stdc++.h> | ||
#define F first | ||
#define S second | ||
using namespace std; | ||
|
||
typedef long long ll; | ||
typedef pair<int, int> pii; | ||
#define ll long long | ||
|
||
const int N = 2e3 + 5; | ||
|
||
ll y[4][2010], sz[4]; | ||
ll cnt[5], sum[5]; | ||
vector<int> a[5]; | ||
ll pre[N]; | ||
|
||
void solve() { | ||
|
||
for(int i = 0; i < 3; i++) { | ||
a[i].clear(); | ||
cnt[i] = 0; | ||
sum[i] = 0; | ||
} | ||
|
||
int n, x, y; | ||
|
||
scanf("%d", &n); | ||
for(int i = 1; i <= n; i++) { | ||
scanf("%d %d", &x, &y); | ||
x--, y--; | ||
a[x].push_back(y); | ||
sum[x] += y; | ||
cnt[x]++; | ||
} | ||
|
||
ll case1(int l, int r) { | ||
ll ans = 0; | ||
int i, j; | ||
for(i = 1; i <= sz[l]; ++i) { | ||
ll cur = 0, cnt = 0; | ||
for(j = 1; j <= sz[r]; ++j) { | ||
ll z = abs(r - l)*abs(y[r][j] - y[l][i]) + 2*abs(r - l)*min(y[r][j], y[l][i]); | ||
ans += cnt*z - cur; | ||
cur += z; | ||
cnt += 1; | ||
|
||
for(int i = 0; i < 3; i++) { | ||
sort(a[i].begin(), a[i].end()); | ||
} | ||
|
||
for(int i = 0; i < 3; i++) { | ||
for(int j = 0; j < 3; j++) { | ||
if(i == j) continue; | ||
for(int k = 0; k < a[i].size(); k++) { | ||
for(int p = k + 1; p < a[i].size(); p++) { | ||
ans += a[j].size() * 1LL * abs((a[i][p] - a[i][k]) * 1LL * (i - j)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pre[0] = 0; | ||
for(int i = 0; i < a[1].size(); i++) { | ||
pre[i + 1] = pre[i] + a[1][i]; | ||
} | ||
|
||
for(int i = 0; i < a[0].size(); i++) { | ||
int start = 0; | ||
for(int j = 0; j < a[2].size(); j++) { | ||
ll tot = a[0][i] + a[2][j]; | ||
while(start < cnt[1] && 2*a[1][start] < tot) start++; | ||
ll contri = ((tot * start) - (2LL * pre[start])) + ((2LL * (pre[cnt[1]] - pre[start])) - (tot * (cnt[1] - start))); | ||
ans += contri; | ||
} | ||
} | ||
return ans; | ||
double res = double(ans) / 2.0; | ||
cout<<setprecision(7)<<fixed<<res<<'\n'; | ||
} | ||
|
||
int main() { | ||
int t, i, j; | ||
// freopen("in.txt", "r", stdin); | ||
int t; | ||
scanf("%d", &t); | ||
while(t--) { | ||
int n, u, v; | ||
scanf("%d", &n); | ||
sz[1] = sz[2] = sz[3] = 0; | ||
for(i = 1; i <= n; ++i) { | ||
scanf("%d %d", &u, &v); | ||
sz[u]++; | ||
y[u][sz[u]] = v; | ||
} | ||
for(i = 1; i <= 3; ++i) { | ||
sort(y[i] + 1, y[i] + sz[i] + 1); | ||
} | ||
ll ans = 0; | ||
//Case 1 and 2 | ||
ans += case1(1, 2); | ||
//Case 2 and 1 | ||
ans += case1(2, 1); | ||
// Case 2 and 3 | ||
ans += case1(2, 3); | ||
// Case 3 and 2 | ||
ans += case1(3, 2); | ||
// Case 1 and 3 | ||
ans += case1(1, 3); | ||
// Case 3 and 1 | ||
ans += case1(3, 1); | ||
// cout << ans << endl; | ||
// Case 1, 2 and 3 | ||
for(i = 1; i <= sz[1]; ++i) { | ||
ll cur = 0, cnt = 0, ptr = 1; | ||
for(j = 1; j <= sz[2]; ++j) { | ||
ll z = abs(y[2][j] - y[1][i]) + 2*min(y[2][j], y[1][i]); | ||
while(ptr <= sz[3] and y[3][ptr] < y[2][j] + y[2][j] - y[1][i]) ptr++; | ||
ans += (ptr - 1)*z - (sz[3] - ptr + 1)*z; | ||
// cout << "in " << ptr << endl; | ||
} | ||
} | ||
// cout << ans << endl; | ||
for(i = 1; i <= sz[3]; ++i) { | ||
ll cur = 0, cnt = 0, ptr = 1; | ||
for(j = 1; j <= sz[2]; ++j) { | ||
ll z = abs(y[2][j] - y[3][i]) + 2*min(y[2][j], y[3][i]); | ||
while(ptr <= sz[1] and y[1][ptr] < y[2][j] + y[2][j] - y[3][i]) ptr++; | ||
ans += (ptr - 1)*z - (sz[1] - ptr + 1)*z; | ||
} | ||
} | ||
// cout << ans << endl; | ||
for(i = 1; i <= sz[1]; ++i) { | ||
ll cur = 0, cnt = 0, ptr = 1; | ||
for(j = 1; j <= sz[3]; ++j) { | ||
ll z = 2*abs(y[3][j] - y[1][i]) + 2*2*min(y[3][j], y[1][i]); | ||
while(ptr <= sz[2] and y[2][ptr] + y[2][ptr] <= y[1][i] + y[3][j]) ptr++; | ||
ans += (ptr - 1)*z - (sz[2] - ptr + 1)*z; | ||
} | ||
} | ||
// cout << ans << endl; | ||
long double fin = (long double)ans/2.0; | ||
printf("%.3Lf\n", fin); | ||
solve(); | ||
} | ||
return 0; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
CP/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob5.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
for iii in range(int(input())): | ||
a,b,c,d,e,f,l,r=(map(int,raw_input().strip().split(' '))) | ||
temp=max(l,r) | ||
tp=min(l,r) | ||
l=temp | ||
r=tp | ||
if(b>a): | ||
if(e>d): | ||
if(max(b,e)<=l and max(a,d)<=r): | ||
C=[0,0] | ||
A=[0,b] | ||
B=[a,0] | ||
F=[r,l] | ||
E=[r-d,l] | ||
D=[r,l-e] | ||
arr=[A,B,C,D,E,F] | ||
for i in range(6): | ||
p=arr[i] | ||
print p[0],p[1] | ||
else: | ||
print(-1) | ||
else: | ||
if(max(b,d)<=l and max(a,e)<=r): | ||
C=[0,0] | ||
A=[0,b] | ||
B=[a,0] | ||
D=[r-e,l] | ||
E=[r,l-d] | ||
F=[r,l] | ||
arr=[A,B,C,D,E,F] | ||
for i in range(6): | ||
p=arr[i] | ||
print p[0],p[1] | ||
|
||
else: | ||
print(-1) | ||
else: | ||
if(e>d): | ||
if(max(a,e)<=l and max(b,d)<=r): | ||
C=[0,0] | ||
A=[b,0] | ||
B=[0,a] | ||
F=[r,l] | ||
E=[r-d,l] | ||
D=[r,l-e] | ||
arr=[A,B,C,D,E,F] | ||
for i in range(6): | ||
p=arr[i] | ||
print p[0],p[1] | ||
else: | ||
print(-1) | ||
else: | ||
if(max(a,d)<=l and max(b,e)<=r): | ||
C=[0,0] | ||
A=[b,0] | ||
B=[0,a] | ||
D=[r-e,l] | ||
E=[r,l-d] | ||
F=[r,l] | ||
arr=[A,B,C,D,E,F] | ||
for i in range(6): | ||
p=arr[i] | ||
print p[0],p[1] | ||
else: | ||
print(-1) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define ll long long | ||
|
||
const int N = 2e3 + 5; | ||
|
||
ll cnt[5], sum[5]; | ||
vector<int> a[5]; | ||
ll pre[N]; | ||
|
||
void solve() { | ||
|
||
for(int i = 0; i < 3; i++) { | ||
a[i].clear(); | ||
cnt[i] = 0; | ||
sum[i] = 0; | ||
} | ||
|
||
int n, x, y; | ||
|
||
scanf("%d", &n); | ||
for(int i = 1; i <= n; i++) { | ||
scanf("%d %d", &x, &y); | ||
x--, y--; | ||
a[x].push_back(y); | ||
sum[x] += y; | ||
cnt[x]++; | ||
} | ||
|
||
ll ans = 0; | ||
|
||
for(int i = 0; i < 3; i++) { | ||
sort(a[i].begin(), a[i].end()); | ||
} | ||
|
||
for(int i = 0; i < 3; i++) { | ||
for(int j = 0; j < 3; j++) { | ||
if(i == j) continue; | ||
for(int k = 0; k < a[i].size(); k++) { | ||
for(int p = k + 1; p < a[i].size(); p++) { | ||
ans += a[j].size() * 1LL * abs((a[i][p] - a[i][k]) * 1LL * (i - j)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pre[0] = 0; | ||
for(int i = 0; i < a[1].size(); i++) { | ||
pre[i + 1] = pre[i] + a[1][i]; | ||
} | ||
|
||
for(int i = 0; i < a[0].size(); i++) { | ||
int start = 0; | ||
for(int j = 0; j < a[2].size(); j++) { | ||
ll tot = a[0][i] + a[2][j]; | ||
while(start < cnt[1] && 2*a[1][start] < tot) start++; | ||
ll contri = ((tot * start) - (2LL * pre[start])) + ((2LL * (pre[cnt[1]] - pre[start])) - (tot * (cnt[1] - start))); | ||
ans += contri; | ||
} | ||
} | ||
double res = double(ans) / 2.0; | ||
cout<<setprecision(7)<<fixed<<res<<'\n'; | ||
} | ||
|
||
int main() { | ||
int t; | ||
scanf("%d", &t); | ||
while(t--) { | ||
solve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
t=input() | ||
for i in range(t): | ||
n=input() | ||
arr=map(int,raw_input().split()) | ||
s=sum(arr) | ||
if s%4!=0: | ||
print -1 | ||
else: | ||
a,b,c=[0,0,0] | ||
for i in arr: | ||
if i%4==1: | ||
a+=1 | ||
elif i%4==2: | ||
b+=1 | ||
elif i%4==3: | ||
c+=1 | ||
#print a,b,c | ||
#a,b,c=[2,5,8] | ||
ans=0 | ||
ans+=min(a,c) | ||
ans+=b/2 | ||
oth=max(a,c)-min(a,c) | ||
#print a,b,c | ||
if b%2==0: | ||
ans+=3*(oth/4) | ||
else: | ||
ans+=2+3*((oth-2)/4) | ||
print ans |