Skip to content

Commit

Permalink
ICPC training
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da committed Jun 29, 2016
1 parent 5e7d316 commit 03531dc
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 0 deletions.
Binary file added doc/RuSSIR_2011_04.pdf
Binary file not shown.
Binary file added doc/TC103_2013.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions solved/URI/1034 Ice Statues Festival/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2
6 100
1 5 10 15 25 50
2 103
1 5
28 changes: 28 additions & 0 deletions solved/URI/1034 Ice Statues Festival/solution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
#define debug(x) cout << #x " = " << (x) << endl
#define endl '\n'

using namespace std;

int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
int tc; cin >> tc;
while (tc--) {
int n, m; cin >> n >> m;
vector<int> a(n);
vector<int> dp(m + 1);
for (auto &i : a) {
cin >> i;
dp[i] = 1;
}
for (int i = 2; i <= m; ++i) {
if (dp[i] == 0) dp[i] = i;
for (auto &j : a) {
if (i - j > 0)
dp[i] = min(dp[i], dp[i - j] + 1);
}
}
cout << dp[m] << endl;
}
return 0;
}
11 changes: 11 additions & 0 deletions solved/URI/1084 Erasing and Winning/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
4 2
3759
6 3
123123
7 4
1000000
7 3
1001234
6 2
103759
0 0
15 changes: 15 additions & 0 deletions solved/URI/1084 Erasing and Winning/input.extra
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
10 1
1234567890
10 2
1234567890
10 3
1234567890
10 4
1234567890
10 5
1234567890
10 6
1234567890
7 2
9909099
0 0
39 changes: 39 additions & 0 deletions solved/URI/1084 Erasing and Winning/solution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
#define debug(x) cout << #x " = " << (x) << endl
#define endl '\n'

using namespace std;

int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n, d;
while (cin >> n >> d && (n + d)) {
string line; cin >> line;
vector<vector<int>> last(n + 1, vector<int>(11));
for (int i = 0; i < 11; ++i) last[n][i] = n;
for (int i = n - 1; i >= 0; --i) {
int cur = line[i] - '0';
for (int j = 0; j < 11; ++j)
last[i][j] = n;

last[i][cur] = i;
for (int j = 0; j < 11; ++j)
last[i][j] = min(last[i][j], last[i + 1][j]);
}

int q = n - d;
for (int i = 0; i < n; ++i) {
int cur = line[i] - '0', best = n;
for (int j = cur + 1; j < 11; ++j)
best = min(best, last[i + 1][j]);

if (n - best < q) {
cout << line[i];
q--;
}
}
assert(q == 0);
cout << endl;
}
return 0;
}
5 changes: 5 additions & 0 deletions solved/URI/1110 Throwing Cards Away/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
7
19
10
6
0
28 changes: 28 additions & 0 deletions solved/URI/1110 Throwing Cards Away/solution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
#define debug(x) cout << #x " = " << (x) << endl
#define endl '\n'

using namespace std;

int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n;
while (cin >> n && n) {
deque<int> q;
for (int i = 1; i <= n; ++i)
q.push_back(i);

cout << "Discarded cards: ";
int first = true;
while (q.size() > 1) {
if (!first) cout << ", "; first = false;
cout << q.front();
q.pop_front();
q.push_back(q.front());
q.pop_front();
}
cout << endl << "Remaining card: " << q.front() << endl;
}

return 0;
}
14 changes: 14 additions & 0 deletions solved/URI/1310 Profit/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
6
20
18
35
6
80
15
21
4
40
30
20
10
38
24 changes: 24 additions & 0 deletions solved/URI/1310 Profit/solution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
#define debug(x) cout << #x " = " << (x) << endl
#define endl '\n'

using namespace std;

int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n, c;
while (cin >> n >> c) {
vector<int> a(n);
for (auto &i : a) {
cin >> i;
i -= c;
}
vector<int> dp(n);
dp[0] = max(0, a[0]);
for (int i = 1; i < n; ++i)
dp[i] = max(dp[i - 1] + a[i], max(0, a[i]));
cout << *max_element(dp.begin(), dp.end()) << endl;
}

return 0;
}
13 changes: 13 additions & 0 deletions solved/URI/1322 Kids' Wishes/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
4 3
2 3
1 3
2 1
1000000000 0
3 6
3 2
2 1
1 2
1 3
2 3
3 1
0 0
14 changes: 14 additions & 0 deletions solved/URI/1322 Kids' Wishes/input.extra
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
3 3
1 2
2 3
1 3

6 6
1 2
2 1
2 3
4 5
5 6
6 4

0 0
84 changes: 84 additions & 0 deletions solved/URI/1322 Kids' Wishes/solution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <bits/stdc++.h>
#define debug(x) cout << #x " = " << (x) << endl
#define endl '\n'

using namespace std;

typedef unordered_map<int, int> umap;

const int MN = (100000 + 100) << 1;

int p[MN];
int c[MN];

int get_id(umap &s, int id) {
if (s.count(id))
return s[id];
int ans = s.size();
return s[id] = ans;
}

int fs(int x) {
return x == p[x] ? x : p[x] = fs(p[x]);
}

void js(int x, int y) {
if (fs(x) == fs(y)) {
c[fs(x)]++;
} else {
c[fs(y)] += c[fs(x)];
p[fs(x)] = fs(y);
}
}

int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n, m;
while (cin >> n >> m && (n + m)) {
unordered_map<int, int> seen(m << 1);
set<pair<int, int>> q;

for (int i = 0; i < MN; ++i) p[i] = i, c[i] = 0;

vector<set<int>> g(m << 1);
int u, v;
for (int i = 0; i < m; ++i) {
cin >> u >> v;
u = get_id(seen, u);
v = get_id(seen, v);
g[u].insert(v);
g[v].insert(u);
if (u > v) swap(u, v);
if (q.count(make_pair(u, v))) continue;
q.insert(make_pair(u, v));
js(u, v);
}

int ok = true;
for (auto &i : g) {
if (i.size() > 2) ok = false;
}
if (!ok) {
cout << "N" << endl;
continue;
}

set<int> comp;
for (int i = 0; i < seen.size(); ++i)
comp.insert(fs(i));

int cycles = 0;
for (auto &i: comp) {
cycles += c[i];
}

if (cycles == 0 ||
(cycles == 1 && seen.size() == n && comp.size() == 1)) {
cout << "Y" << endl;
continue;
}
cout << "N" << endl;

}
return 0;
}
24 changes: 24 additions & 0 deletions solved/URI/1470 Folding Machine/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
7
5 6 23 8 19 7 10
4
5 16 30 27
7
1 2 3 4 5 6 7
5
7 6 5 5 5
4
1 2 3 4
1
10
6
19 23 3 51 2 0
2
34 64
6
1 2 3 4 5 6
6
1 2 3 4 5 6
6
1 2 3 4 5 6
6
6 5 4 3 2 1
4 changes: 4 additions & 0 deletions solved/URI/1470 Folding Machine/input2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
7
1 2 3 4 5 6 7
5
7 6 5 5 5
77 changes: 77 additions & 0 deletions solved/URI/1470 Folding Machine/solution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <bits/stdc++.h>
#define debug(x) cout << #x " = " << (x) << endl
#define endl '\n'

using namespace std;

void D(vector<int> a) {
for (auto &i : a) cout << i << ' ';
cout << endl;
}

vector<int> gen_next(const vector<int> &a, int i) {
int l = i + 1;
int r = a.size() - l;
vector<int> next(max(r, l));
if (l >= r) {
int k = i;
for (int j = i + 1; j < a.size(); ++j, --k) {
next[k] += a[k];
next[k] += a[j];
}
while (k >= 0) {
next[k] += a[k];
k--;
}
} else {
int k = 0;
for (int j = i; j >= 0; --j, ++k) {
next[i - j] += a[j];
next[k] += a[i + k + 1];
}
while (k + i + 1 < a.size()) {
next[k] += a[i + k + 1];
k++;
}
reverse(next.begin(), next.end());
}
return next;
}


set<vector<int>> seen;
bool bf(vector<int> a, vector<int> &b) {
if (seen.count(a)) return false;
if (a.size() < b.size()) return false;
if (a == b) return true;

seen.insert(a);
for (int i = 0; i + 1 < a.size(); ++i) {
vector<int> next = gen_next(a, i);
if (bf(next, b))
return true;
}
reverse(a.begin(), a.end());
if (bf(a, b))
return true;
return false;
}

int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);

int n;
while (cin >> n) {
vector<int> a(n);
for (auto &i : a) cin >> i;
int m; cin >> m;
vector<int> b(m);
for (auto &i : b) cin >> i;

if (bf(a, b))
cout << "S" << endl;
else
cout << "N" << endl;
}
return 0;
}
Loading

0 comments on commit 03531dc

Please sign in to comment.