Skip to content

Commit

Permalink
Binary Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Jan 23, 2018
1 parent 26e420f commit 89779c8
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 87 deletions.
31 changes: 31 additions & 0 deletions CP/Codeforces Round #457 (Div. 2)/prob1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
n = int(input())
a = map(str,raw_input().split(' '))

a[0] = int(a[0])
a[1] = int(a[1])

a1 = a[0]//10
a2 = a[0]%10
b1 = a[1]//10
b2 = a[1]%10

if(a1 == 7 or a2 == 7 or b1 == 7 or b2 == 7 ):
print "0"
else:
count = 0
while(not(a1 == 7 or a2 == 7 or b1 == 7 or b2 == 7)):
a[1]-=n
count+=1
#print(count)
if(a[1]<0):
a[1]+=60
a[0]-=1
if(a[0]<0):
a[0]+=24
a1 = a[0]//10
a2 = a[0]%10
b1 = a[1]//10
b2 = a[1]%10
continue
#print(count,str(a[0])+str(a[1]))
print(count)
33 changes: 33 additions & 0 deletions CP/Codeforces Round #457 (Div. 2)/prob2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from collections import deque
n, k = map(int, raw_input().split())
a = [i for i in xrange(64) if n >> i & 1]
if k < len(a):
print 'No'
quit()
s = a[-1]
c = 0
while len(a) + c < k:
if a and s == a[-1]:
c += 1
a.pop()
c = c * 2
s -= 1
t = len(a) + c - k
a.reverse()
b = 0
if t:
while t:
c -= 2
t -= 1
b += 1
if not a:
c -= 1
a.append(s)
while c > 1:
c -= 2
b += 1
a.extend([a.pop() - 1] * 2)
a = [s + 1] * b + [s] * c + a
print 'Yes'
for x in a:
print x,
166 changes: 79 additions & 87 deletions CP/JanuaryCookOff2018/prob3.cpp
Original file line number Diff line number Diff line change
@@ -1,91 +1,83 @@
#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 A[100009];
int D1[100009];
int D2[100009];
bool ok(int i)
{
if (i % 2 == 1)
return A[i] < A[i + 1];
else
return A[i] > A[i + 1];
}

int main() {
int ct;
scanf("%d", &ct);

while (ct--) solve();
int N;
int ans()
{
int i = (N + 1) / 2;
int j = (N + 2) / 2;
int id = 0;
D1[id] = N + 2;
D2[id] = N + 2;
if (i == j)
{
D1[id] = 0;
D2[id] = 0;
}
else
{
if (ok(i))
D1[id] = 0;
swap(A[i], A[j]);
if (ok(i))
D2[id] = 1;
swap(A[i], A[j]);
}
while (true)
{
i--;
j++;
if (i < 1)
break;
id++;
D1[id] = N + 2;
D2[id] = N + 2;
{
if (ok(i) && ok(j - 1))
D1[id] = min(D1[id], D1[id - 1]);
swap(A[i + 1], A[j - 1]);
if (ok(i) && ok(j - 1))
D1[id] = min(D1[id], D2[id - 1]);
swap(A[i + 1], A[j - 1]);
}
swap(A[i], A[j]);
{
if (ok(i) && ok(j - 1))
D2[id] = min(D2[id], 1 + D1[id - 1]);
swap(A[i + 1], A[j - 1]);
if (ok(i) && ok(j - 1))
D2[id] = min(D2[id], 1 + D2[id - 1]);
swap(A[i + 1], A[j - 1]);
}
swap(A[i], A[j]);
}
return min(D1[id], D2[id]);
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> N;
for (int i = 1; i <= N; i++)
cin >> A[i];
int out = N + 2;
out = min(out, ans());
for (int i = 1; i <= N; i++)
A[i] *= -1;
out = min(out, ans());
if (out > N)
out = -1;
cout << out << '\n';
}
}
Loading

0 comments on commit 89779c8

Please sign in to comment.