Skip to content

Commit

Permalink
656
Browse files Browse the repository at this point in the history
  • Loading branch information
ldct committed Jul 18, 2020
1 parent 8e54eb6 commit 29aa779
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 0 deletions.
6 changes: 6 additions & 0 deletions codeforces/656/A/A.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5
3 2 3
100 100 100
50 49 49
10 30 20
1 1000000000 1000000000
40 changes: 40 additions & 0 deletions codeforces/656/A/A.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

from itertools import permutations

def ans(x, y, z):
old_xyz = [x, y, z]

if len(set([x, y, z])) == 1:
print("YES")
print(x, x, x)
return
if len(set([x, y, z])) == 3:
print("NO")
return
# two are the same
l = sorted([x, y, z])
[x, y, z] = l

if x == y:
print("NO")
return

assert(y == z)

for a, b, c in permutations([x, z, x]):
if [max(a,b), max(a,c), max(b,c)] == old_xyz:
print("YES")
print(a, b, c)
return

assert(False)

T = int(input())

for _ in range(T):
x, y, z = input().split(' ')
x = int(x)
y = int(y)
z = int(z)
ans(x, y, z)
11 changes: 11 additions & 0 deletions codeforces/656/B/B.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
18 changes: 18 additions & 0 deletions codeforces/656/B/B.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env pypy3

T = int(input())

for _ in range(T):
input()
A = input().split(' ')
A = list(map(int, A))

seen = set()

P = []
for a in A:
if a not in seen:
P += [a]
seen.add(a)

print(*P)
11 changes: 11 additions & 0 deletions codeforces/656/C/C.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
27 changes: 27 additions & 0 deletions codeforces/656/C/C.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env pypy3

from sys import stdin, stdout

def input():
return stdin.readline().strip()

T = int(input())

def ans(A):
A += [float("-inf")]
i = len(A)-1

while i > 0 and A[i-1] >= A[i]:
i -= 1

while i > 0 and A[i-1] <= A[i]:
i -= 1

return i

for _ in range(T):
input()
A = input().split(' ')
A = list(map(int, A))

print(ans(A))
13 changes: 13 additions & 0 deletions codeforces/656/D/D.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac
44 changes: 44 additions & 0 deletions codeforces/656/D/D.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env pypy3

from sys import stdin, stdout

def input():
return stdin.readline().strip()

T = int(input())

def count_diff(c, S):
c = chr(c + 97)
ret = 0
for s in S:
if c != s:
ret += 1
return ret

memo = None

def ans(c, S):
global memo

if len(S) == 1:
if S == chr(c + 97): return 0
else: return 1

if (c, S) in memo: return memo[(c, S)]

mid = len(S) // 2
left = S[0:mid]
right = S[mid:]

ret = min(
count_diff(c, left) + ans(c+1, right),
ans(c+1, left) + count_diff(c, right)
)
memo[(c, S)] = ret
return ret

for _ in range(T):
N = int(input())
S = input()
memo = dict()
print(ans(0, S))
104 changes: 104 additions & 0 deletions codeforces/656/E/E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <bits/stdc++.h>
using namespace std;

template<class T1, class T2> ostream& operator << (ostream& os, const pair<T1,T2>& v) { return os << "(" << v.first << ", " << v.second << ")"; }
template<class T> ostream& operator << (ostream& os, const vector<T>& v) { os << "["; for (int i=0; i<v.size(); i++) { os << v[i]; if (i < v.size() - 1) os << ", "; } return os << "]"; }
template<class T> ostream& operator << (ostream& os, const set<T>& v) { os << "{"; for (const T& e : v) os << e << ", "; os << "}"; }

constexpr size_t MAX_N = 2000009;

using iii = int;

#define int long long

int N,M,T;

vector<pair<int,int>> all_edges;
vector<int> neighbours[MAX_N];
vector<int> toposort_ans;
int visited[MAX_N];
bool has_cycle;

void dfs(int daddy, int v) {
visited[v] = 0;
for (int u : neighbours[v]) {
if (visited[u] == 0) has_cycle = true;
if (visited[u] == -1) dfs(daddy, u);
}
visited[v] = daddy;
toposort_ans.push_back(v);
}

void topological_sort() {
for (int i = 1; i <= N; ++i) {
if (visited[i] == -1) dfs(i, i);
}
reverse(toposort_ans.begin(), toposort_ans.end());
}

iii main() {

cin >> T;

while (T --> 0) {
all_edges.clear();
toposort_ans.clear();
has_cycle = false;
cin >> N >> M;
for (int i=1; i<=N; i++) {
neighbours[i].clear();
visited[i] = -1;
}
for (int i=0; i<M; i++) {
int t,x,y;
cin >> t >> x >> y;
all_edges.push_back(make_pair(x, y));
if (t == 1) {
neighbours[x].push_back(y);
} else {
assert(t == 0);
}
}
// toposort
topological_sort();

if (has_cycle) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;

// cout << "sort=" << toposort_ans << endl;

auto level_of = vector<int>(N+1, -1);

assert(toposort_ans.size() == N);

for (int i=0; i<toposort_ans.size(); i++) {
level_of[toposort_ans[i]] = i;
}

assert(all_edges.size() == M);
for (const auto& p : all_edges) {
auto x = p.first;
auto y = p.second;

assert(1 <= x && x <= N);
assert(1 <= y && y <= N);
assert(level_of[x] != -1);
assert(level_of[y] != -1);
assert(level_of[x] != level_of[y]);

if (level_of[x] > level_of[y]) {
cout << y << " " << x << endl;
} else {
cout << x << " " << y << endl;
}
}
sort(toposort_ans.begin(), toposort_ans.end());
for (int i=0; i<toposort_ans.size(); i++) {
assert(toposort_ans[i] == i+1);
}
}
}
return 0;
}
5 changes: 5 additions & 0 deletions codeforces/656/E/E.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1
10 3
1 1 2
1 2 3
1 1 3

0 comments on commit 29aa779

Please sign in to comment.