Codeforces Round #401 (Div. 2) (solve 4 of 5)

A - Shell Game【暴力】

题意:

1.有三个坑,小球在其中一个坑。
2.执行 n 次交换后,告诉你最后小球的坑位 x
3.奇数次 (1.3.5...) 交换前两个坑,偶数次 (2.4.6...) 交换后两个坑。
4.数据范围: 1n2×109 0x2

思路:

1.周期为 6 ,所以对 n 做优化, n=n%6
2.暴力枚举坑的位置 012 ,每次模拟 n 次交换。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int n, x;
bool judge(int i) {
    int a[3];
    n %= 30;
    memset(a, 0, sizeof(a));
    a[i] = 1;
    for(int i = 1; i <= n; i++) {
        if(i % 2 == 0) swap(a[1], a[2]);
        else swap(a[0], a[1]);
    }

    if(a[x] == 1) return true;
    return false;
}
int main() {
    scanf("%d%d", &n, &x);
    for(int i = 0; i < 3; i++) {
        if(judge(i)) {
            cout << i << endl;
            return 0;
        }
    }
    return 0;
}

B.Game of Credit Cards【贪心】

题意:

1.这题的作者估计也是希望夏洛克和莫里亚蒂搞基的。
2.给你两串长度为 n 的数字,第一串是夏洛克的,第二串是莫里亚蒂的。
3.比对每一位,数字小的要被弹脑壳。
4.调整数字顺序。求莫里亚蒂最少被弹次数,和夏洛克最多被弹次数。
(PS.因为小莫比较受,所以应该少被弹)

思路:

1.问题一的贪心策略:从小到大sort两人的数字。每一次攻夏拿出一个当前最小数字,小莫应该拿出尽可能小的不被弹的数字。
2.问题二的贪心策略:从大到小sort两人的数字。每一次小莫拿出一个当前最大数字,攻夏应该拿出尽可能大的被弹的数字。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int a[1010], b[1010];
bool cmp(const int &a, const int &b) {
    return a > b;
}
int main() {
    int n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    for(int i = 0; i < n; ++ i) a[i] = s1[i] - '0';
    for(int i = 0; i < n; ++ i) b[i] = s2[i] - '0';
    sort(a, a + n);
    sort(b, b + n);
    int idx1 = 0, idx2 = 0;
    while(idx1 < n && idx2 < n) {
        if(a[idx1] > b[idx2]) idx2++;
        else idx1++, idx2++;
    }
    cout << n - idx1 << endl;
    idx1 = 0, idx2 = 0;
    sort(b, b + n, cmp);
    sort(a, a + n, cmp);
    int maxx = b[0];
    while(idx1 < n && a[idx1] >= maxx) idx1++;
    while(idx1 < n && idx2 < n) {
        if(b[idx2] > a[idx1]) idx1++, idx2++;
        else idx1++;
    }
    cout << idx2 << endl;
}

C - Alyona and Spreadsheet【DP】

戳上面链接直达题解BLOG

D - Cloud of Hashtags【贪心】

戳上面链接直达题解BLOG

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值