Codeforces Round #162 (Div. 2)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值