Skip to content

Commit 14670a0

Browse files
committed
codeforces stuff
1 parent 4f0f126 commit 14670a0

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed

codeforces/20/c.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <cstdio>
2+
#include <queue>
3+
4+
using namespace std;
5+
6+
int N, M;
7+
//int edges[100010][3];
8+
vector<pair<int, int> > edges[100010];
9+
int previous[100010];
10+
bool seen[100010];
11+
12+
struct node {
13+
int n, prev;
14+
long long dist;
15+
node(int n, long long dist, int prev) : n(n), dist(dist), prev(prev) {};
16+
};
17+
18+
bool operator<(node n1, node n2) {
19+
return n1.dist > n2.dist;
20+
}
21+
22+
int main() {
23+
scanf("%d %d", &N, &M);
24+
int a, b, w;
25+
for(int i=0;i<M;i++) {
26+
scanf("%d %d %d", &a, &b, &w);
27+
edges[a - 1].push_back(make_pair(b - 1, w));
28+
edges[b - 1].push_back(make_pair(a - 1, w));
29+
previous[a - 1] = -1;
30+
previous[b - 1] = -1;
31+
}
32+
33+
priority_queue<node> pq;
34+
pq.push(node(0, 0LL, -1));
35+
while(!pq.empty()) {
36+
node cur = pq.top();
37+
pq.pop();
38+
if(seen[cur.n]) {
39+
continue;
40+
}
41+
42+
seen[cur.n] = true;
43+
previous[cur.n] = cur.prev;
44+
45+
if(cur.n == N - 1) {
46+
break;
47+
}
48+
49+
for(auto it = edges[cur.n].begin(); it != edges[cur.n].end(); it++) {
50+
pq.push(node(it->first, it->second + cur.dist, cur.n));
51+
}
52+
}
53+
vector<int> ans;
54+
if(!seen[N - 1]) {
55+
printf("-1\n");
56+
} else {
57+
int cur = N - 1;
58+
while(cur > 0) {
59+
ans.push_back(cur + 1);
60+
cur = previous[cur];
61+
}
62+
printf("1");
63+
for(vector<int>::reverse_iterator it = ans.rbegin(); it != ans.rend(); it++) {
64+
printf(" %d", *it);
65+
}
66+
printf("\n");
67+
}
68+
return 0;
69+
}

codeforces/465/a.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
n = int(raw_input())
2+
s = raw_input()
3+
carry = 1
4+
ans = 0
5+
for i, e in enumerate(s):
6+
if carry == 0:
7+
break
8+
carry = 1 if (e == '1') else 0
9+
ans = i
10+
print ans+1

codeforces/465/b.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
n = int(raw_input())
2+
a = map(int, raw_input().split())
3+
letters = [i for i, e in enumerate(a) if e == 1]
4+
5+
ans = 1 if len(letters) > 0 else 0
6+
for i, e in enumerate(letters):
7+
if i == 0:
8+
continue
9+
if e - 1 == letters[i-1]:
10+
ans += 1
11+
else:
12+
ans += 2
13+
14+
print ans

codeforces/465/c.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
int N, P;
6+
int a[1001];
7+
string s;
8+
int main() {
9+
cin >> N >> P;
10+
cin >> s;
11+
for(int i=0;i<N;i++) {
12+
a[i] = s[i] - 'a';
13+
}
14+
bool tolerable = false;;
15+
while(!tolerable) {
16+
int i = N - 1;
17+
a[i]++;
18+
while(a[i] >= P) {
19+
a[i] = 0;
20+
i--;
21+
if(i < 0) {
22+
cout << "NO\n";
23+
return 0;
24+
}
25+
a[i]++;
26+
}
27+
tolerable = true;
28+
//check for doubles and triples
29+
for(int mid=1;mid<N-1;mid++) {
30+
if(a[mid-1] == a[mid+1] || a[mid-1] == a[mid] || a[mid] == a[mid+1]) tolerable = false;
31+
}
32+
if(N >= 2 && a[0] == a[1]) tolerable = false;
33+
}
34+
for(int i=0;i<N;i++) {
35+
cout << ((char)(a[i] + 'a'));
36+
}
37+
cout << '\n';
38+
return 0;
39+
}

codeforces/465/d.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <iostream>
2+
#define P 8
3+
#define C 3
4+
using namespace std;
5+
long long coords[8][3];
6+
int permutation[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
7+
int current[] = {0, 0, 0, 0, 0, 0, 0, 0};
8+
9+
void printans() {
10+
cout << "YES\n";
11+
for(int i=0;i<P; i++) {
12+
cout << coords[i][permutation[current[i]][0]] << " "
13+
<< coords[i][permutation[current[i]][1]] << " "
14+
<< coords[i][permutation[current[i]][2]] << '\n';
15+
}
16+
}
17+
18+
long long square(long long x) {
19+
return 1LL * x * x;
20+
}
21+
22+
bool isgood() {
23+
//calculate all the squared differences and see if they make sense
24+
long long dists[] = {0, 0, 0};
25+
long long counts[] = {0, 0, 0};
26+
for(int i=0;i<P;i++) {
27+
for(int j=i+1;j<P;j++) {
28+
long long dist =
29+
square(coords[i][permutation[current[i]][0]] - coords[j][permutation[current[j]][0]]) +
30+
square(coords[i][permutation[current[i]][1]] - coords[j][permutation[current[j]][1]]) +
31+
square(coords[i][permutation[current[i]][2]] - coords[j][permutation[current[j]][2]]);
32+
bool inserted = false;
33+
for(int k=0;k<3;k++) {
34+
if(dists[k] == dist) {
35+
counts[k]++;
36+
inserted = true;
37+
break;
38+
} else if(dists[k] == 0) {
39+
dists[k] = dist;
40+
counts[k] = 1;
41+
inserted = true;
42+
break;
43+
}
44+
}
45+
if(!inserted) return false;
46+
}
47+
}
48+
if(counts[0] + counts[1] + counts[2] != 28) return false;
49+
for(int i=0;i<6;i++) {
50+
if(dists[permutation[i][0]] * 2 == dists[permutation[i][1]]
51+
&& dists[permutation[i][0]] * 3 == dists[permutation[i][2]]
52+
&& counts[permutation[i][0]] == 12 && counts[permutation[i][1]] == 12 && counts[permutation[i][2]] == 4) {
53+
return true;
54+
}
55+
}
56+
return false;
57+
}
58+
59+
bool increment() {
60+
int i = P-1;
61+
current[i]++;
62+
while(current[i] == 6) {
63+
current[i] = 0;
64+
i--;
65+
if(i < 0) {
66+
return false;
67+
}
68+
current[i]++;
69+
}
70+
return true;
71+
}
72+
73+
int main() {
74+
for(int i=0;i<P;i++) {
75+
for(int j=0;j<C;j++) {
76+
cin >> coords[i][j];
77+
}
78+
}
79+
if(isgood()) {
80+
printans();
81+
return 0;
82+
}
83+
bool done = false;
84+
while(increment()) {
85+
if(isgood()) {
86+
done = true;
87+
printans();
88+
break;
89+
}
90+
}
91+
if(!done) {
92+
cout << "NO" << '\n';
93+
}
94+
return 0;
95+
}

codeforces/465/e.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
s = raw_input()
2+
n = int(raw_input())
3+
for i in xrange(n):
4+
inn, out = raw_input().split('->')
5+
s = s.replace(inn, out)
6+
7+
if len(s) == 0:
8+
s = '0'
9+
print int(s, 10) % 1000000007

0 commit comments

Comments
 (0)