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
ad4eb51
commit fbf0031
Showing
6 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
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 |
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,15 @@ | ||
import math | ||
for kohli in range(input()): | ||
n,k,s = map(int,raw_input().split()) | ||
if(n<k or (6*n<7*k and s>=7)): | ||
print("-1") | ||
continue | ||
c = int(math.ceil(s*1.0/(n*1.0/k*1.0))) | ||
print(c) | ||
|
||
|
||
''' | ||
2 | ||
16 2 10 | ||
50 48 7 | ||
''' |
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,46 @@ | ||
from __future__ import division | ||
from math import ceil | ||
from bisect import bisect_right as b_r | ||
from bisect import bisect_left as b_l | ||
for __ in range(int(input())): | ||
a = map(int,raw_input().split()) | ||
s = (a[1]+a[2])%10 | ||
lis = [a[1],a[2]] | ||
c=2 | ||
if(s%2==1): | ||
lis.append(s) | ||
c+=1 | ||
s = (s*2)%10 | ||
#print(s) | ||
rem = (a[0]-c)%4 | ||
loop = (a[0]-c)//4 | ||
#print(rem,loop) | ||
summ = sum(lis) | ||
summ += (loop*20)%3 | ||
tt = [4,8,6,2,4,8,6,2] | ||
st = '4862' | ||
i = st.find(str(s)) | ||
for j in range(0,rem): | ||
summ+=tt[i+j] | ||
summ%=3 | ||
if(a[0]==2): | ||
if((a[1]+a[2])%3==0): | ||
print 'YES' | ||
else: | ||
print 'NO' | ||
elif(s==0): | ||
print 'NO' | ||
elif(summ%3==0): | ||
print 'YES' | ||
else: | ||
print 'NO' | ||
|
||
|
||
|
||
|
||
''' | ||
2 | ||
5 3 4 | ||
13 8 1 | ||
''' |
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,91 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
const int oo = 1000000000; | ||
|
||
const int N = 100005; | ||
|
||
int a[N], val[N][2], f[N][2], g[N][2]; | ||
|
||
void solve() { | ||
int n; | ||
scanf("%d", &n); | ||
|
||
for (int i = 1; i <= n; i++) { | ||
scanf("%d", a+i); | ||
} | ||
|
||
if (n == 1) { | ||
printf("0\n"); | ||
return; | ||
} | ||
|
||
for (int i = 1; i <= n; i++) { | ||
val[i][0] = a[i]; | ||
val[i][1] = a[n+1-i]; | ||
} | ||
|
||
int m = n/2; | ||
for (int i = 1; i <= m; i++) | ||
for (int k = 0; k < 2; k++) | ||
f[i][k] = g[i][k] = oo; | ||
f[1][0] = g[1][0] = 0; | ||
f[1][1] = g[1][1] = 1; | ||
for (int i = 1; i < m; i++) { | ||
for (int j = 0; j < 2; j++) { | ||
for (int k = 0; k < 2; k++) { | ||
int c1 = val[i][j]; | ||
int c2 = val[i+1][k]; | ||
if (((i & 1) && (c1 <= c2)) || (((i & 1) == 0) && c1 >= c2)) continue; | ||
int t = n+1-i; | ||
c1 = val[t][j]; | ||
c2 = val[t-1][k]; | ||
if (((t & 1) && (c1 <= c2)) || (((t & 1) == 0) && c1 >= c2)) continue; | ||
f[i+1][k] = min(f[i+1][k], f[i][j] + k); | ||
} | ||
for (int k = 0; k < 2; k++) { | ||
int c1 = val[i][j]; | ||
int c2 = val[i+1][k]; | ||
if (((i & 1) && (c1 >= c2)) || (((i & 1) == 0) && c1 <= c2)) continue; | ||
int t = n+1-i; | ||
c1 = val[t][j]; | ||
c2 = val[t-1][k]; | ||
if (((t & 1) && (c1 >= c2)) || (((t & 1) == 0) && c1 <= c2)) continue; | ||
g[i+1][k] = min(g[i+1][k], g[i][j] + k); | ||
} | ||
} | ||
} | ||
|
||
int ans = oo; | ||
|
||
if (n % 2 == 0) { | ||
for (int j = 0; j < 2; j++) { | ||
int c1 = val[m][j]; | ||
int c2 = val[m+1][j]; | ||
if (((m&1) && (c1 > c2)) || ((m&1) == 0 && (c1 < c2))) | ||
ans = min(ans, f[m][j]); | ||
if (((m&1) && (c1 < c2)) || ((m&1) == 0 && (c1 > c2))) | ||
ans = min(ans, g[m][j]); | ||
} | ||
} | ||
else { | ||
int c1 = a[m]; | ||
int c2 = a[m+2]; | ||
int b = a[m+1]; | ||
if ((((m+1)&1) && b > c1 && b > c2) || (((m+1)&1) == 0 && b < c1 && b < c2)) | ||
ans = min(ans, min(f[m][0], f[m][1])); | ||
if ((((m+1)&1) && b < c1 && b < c2) || (((m+1)&1) == 0 && b > c1 && b > c2)) | ||
ans = min(ans, min(g[m][0], g[m][1])); | ||
} | ||
|
||
if (ans >= oo) ans = -1; | ||
printf("%d\n", ans); | ||
} | ||
|
||
int main() { | ||
int ct; | ||
scanf("%d", &ct); | ||
|
||
while (ct--) solve(); | ||
} |
Empty file.
Empty file.