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

来源:互联网 发布:办公软件考试系统 编辑:程序博客网 时间:2024/06/15 14:40

A - Shell Game【暴力】

题意:

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

思路:

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

原创粉丝点击