Codeforces Round #336 (Div. 2)

目前只写了前四题,第五题读了半个小时,还没有读懂,哈哈。。。

A. Saitama Destroys Hotel
题意:电梯载人,一个简单模拟,可能代码写的有点乱,不喜勿喷!
解题思路:略。
Show me the code!

#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
int main() {
    int N, M;
    cin >> N >> M;
    vector<pii> vec(N);
    for (int a = 0; a < N; ++a) cin >> vec[a].x >> vec[a].y;
    sort(vec.begin(), vec.end());
    int res = 0, cf = M;
    for (int a = N - 1; a >= 0; --a) {
        if (vec[a].x < cf) {
            res += cf - vec[a].x;
            cf = vec[a].x;
        }
        if (vec[a].y > res) {
            res = vec[a].y;
        }
    }
    if (cf) res += cf;
    cout << res << endl;
    return 0;
}

B. Hamming Distance Sum
题意:先定义好了两个长度均为n的串s 和t 的Hamming distance = sum{abs(s[i] - t[i]), 0 <= i <= n}。给出a, b两个01串(|a| ≤ |b|≤200000),求b得所有长度为|a|的子串和a串的距离之和。
解题思路:显然分别去求a串和b的所有子串的Hamming distance,再求和,肯定是不可取的。其实我们可以把这道题目考虑成贡献统计,对应每一个a[i],求出一个贡献,记作c[i],最后的结果就是sum{c[i], 0 <= i <= |a|}。怎样去求c[i]?c[i] = sum{abs(a[i] - b[j]), i <= j <= |b| - (|a| - i - )}。所以。。。
Show me the code!

#include <iostream>
using namespace std;
const int maxn = 200001;
int bit[maxn];
void updata(int x, int val) {
    while (x < maxn) {
        bit[x] += val;
        x += x & -x;
    }
}
int query(int x) {
    int res = 0;
    while (x) {
        res += bit[x];
        x -= x & -x;
    }
    return res;
}
int main() {
    string stra, strb;
    cin >> stra >> strb;
    int N = stra.size(), M = strb.size();
    for (int a = 0; a < M; ++a) {
        if (strb[a] == '1') updata(a + 1, 1);
    }
    long long res = 0;
    for (int a = 0; a < N; ++a) {
        int t = query(M - (N - a - 1)) - query(a);
        int all = M - N + 1;
        if (stra[a] == '0') res += t;
        else res += all - t;
    }
    cout << res << endl;
    return 0;
}

C. Chain Reaction
题目大意:真的有点难得叙述,就这样水过去吧。。。。。。
解题思路:比赛的时候没有写出来,一躺上床,思路就来了。这是道简单DP,第i个灯塔存在时,去找到没有被摧毁的第P个灯塔,dp[i] = dp[p] + 1。
Show me the code!

#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
int main() {
    int N;
    cin >> N;
    vector<pii> vec(N);
    for (int a = 0; a < N; ++a) cin >> vec[a].x >> vec[a].y;
    sort(vec.begin(), vec.end());
    vector<int> dp(N);
    dp[0] = 1;
    for (int a = 1; a < N; ++a) {
        int p = lower_bound(vec.begin(), vec.end(),
                pii(vec[a].x - vec[a].y, 0)) - vec.begin();
        dp[a] = p > 0 ? dp[p - 1] + 1 : 1;
    }
    int res = N;
    for (int a = 0; a < N; ++a) {
        res = min(res, N - dp[a]);
    }
    cout << res << endl;
    return 0;
}

D. Zuma
题目大意:给出一个由数字组成的串,每次删掉一个回文串,求最少多少次能全部删除完。
解题思路:这道题目也是DP,不过像我这种菜鸟还是适合用记忆化搜索。定义dp[i][j]等于 i 到 j 的最小的删除次数,如果arr[i] != arr[j],则dp[i][j] = min{dp[i][k] + dp[k + 1][j], i <= k < j},否则,还要在此基础上取一次小,dp[i][j] = min(dp[i][j], dp[i + 1][j - 1])。
Show me the code!

#include <iostream>
using namespace std;
const int maxn = 500;
int arr[maxn], dp[maxn][maxn];
int solve(int l, int r) {
    if (dp[l][r]) return dp[l][r];
    dp[l][r] = maxn;
    for (int a = l; a < r; ++a) {
        dp[l][r] = min(dp[l][r], solve(l, a) + solve(a + 1, r));
    }
    if (arr[l] == arr[r]) dp[l][r] = min(dp[l][r], solve(l + 1, r - 1));
    return dp[l][r];
}
int main() {
    int N;
    cin >> N;
    for (int a = 0; a < N; ++a) cin >> arr[a];
    for (int a = 0; a < N; ++a) {
        fill(dp[a], dp[a] + N, 0);
        dp[a][a] = 1;
        if (a + 1 < N && arr[a] == arr[a + 1]) dp[a][a + 1] = 1; 
    }
    cout << solve(0, N - 1) << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值