Problem set: http://codeforces.com/contest/265
Solution tutorial: http://codeforces.com/blog/entry/6478
Problem A
#include <iostream>
#include <string>
using namespace std;
int main() {
string s, t;
cin >> s >> t;
int ans = 0;
for (int i = 0; i < t.length(); ++i)
if (t[i] == s[ans])
++ans;
cout << (ans + 1) << endl;
return 0;
}
Problem B
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int ans = n + n - 1, curh = 0, h;
for (int i = 0; i < n; ++i) {
cin >> h;
ans += abs(h - curh);
curh = h;
}
cout << ans << endl;
return 0;
}
Problem C
Attention: using std::endl (rather than "\n") is slow (because of flushing stream), and may cause TLE!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 1000000;
int ans[MAX];
int main() {
string s;
cin >> s;
int left = 0, right = s.length() - 1;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == 'l') {
ans[right] = i + 1;
--right;
} else {
ans[left] = i + 1;
++left;
}
}
for (int i = 0; i < s.length(); ++i)
cout << ans[i] << endl;
return 0;
}
Problem D (TLE #21, DP)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 100000;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a%b);
}
int main() {
int n, a[MAX], cnt[MAX] = {1};
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 1; i < n; ++i) {
cnt[i] = 1;
for (int j = i - 1; j >= 0; --j) {
if (cnt[i] > cnt[j] + 1)
continue;
if (gcd(a[i], a[j]) > 1)
cnt[i] = cnt[j] + 1;
}
}
int ans = 0;
for (int i = 0; i < n; ++i)
ans = max(ans, cnt[i]);
cout << ans << endl;
return 0;
}
Problem D
Problem E (TLE pretest #8, DP)
#include <iostream>
#include <cstdint>
#include <algorithm>
using namespace std;
const int MAX = 100000;
int c[MAX], v[MAX];
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; ++i)
cin >> v[i];
for (int i = 0; i < n; ++i)
cin >> c[i];
while (q--) {
int64_t a, b;
cin >> a >> b;
int64_t dp[MAX];
for (int i = 0; i < n; ++i) {
dp[i] = b * v[i];
for (int j = 0; j < i; ++j) {
if (c[i] == c[j] && dp[i] < dp[j] + a * v[i])
dp[i] = dp[j] + a * v[i];
else if(c[i] != c[j] && dp[i] < dp[j] + b * v[i])
dp[i] = dp[j] + b * v[i];
}
}
int64_t ans = 0;
for (int i = 0; i < n; ++i) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
return 0;
}
Problem E