• Codeforces Round #721 (Div. 2) AB思维,B2博弈,C题map


    补题链接:Here

    1527A. And Then There Were K

    题目大意:

    给一个正整数n,求最大的k,使得 (n & (n−1) & (n−2) & (n−3) & … (k) = 0)

    思路:

    就假设 (n) 为 17,二进制为 10001,我们来模拟一下求解过程。

    17 10001
    
    16 10000
    
    15 01111
    

    因为按位与的特点就是,一位上只要有一个0,这一位最后就是0。

    我们竖着来看,17到15,每一位上就都出现过一次0了,所以15就是答案。然后举更多例子观察特点,发现,答案就是让n的二进制最左边的1置为0,后面全置为1。

    如:

    10001 11101 10111的答案都是 01111

    【AC代码】

    int qpow(int a, int b) {int ans = 1; while (b) {if (b & 1)ans = ans * a; b >>= 1; a = a * a;} return ans;}
    void solve() {
        int n;
        cin >> n;
        int ans = 0;
        while (n) {
            n >>= 1;
            ++ans;
        }
        cout << qpow(2, ans - 1) - 1 << "
    ";
    }
    

    转化思维,模拟上面的过程

    void solve() {
        ll n, k = 0;
        cin >> n;
        while (k * 2 + 1 < n)k = k * 2 + 1;
        cout << k << "
    ";
    }
    

    遇到这种位运算的题目,一般都是把数的二进制表示出来,然后根据运算的特点(比如&的特点就是,只要有一个0,最后就是0),找规律。

    1527B1. Palindrome Game (easy version)

    题目大意:

    给一个字符串(这题的字符串一开始一定是一个回文)。

    1.可以把一个0变为1,操作者的数字+1。

    2.或者翻转整个字符串(前提是该字符串不是回文且上一个人的操作不是翻转)。

    Alice先,最后字符串全为1时,谁的数字大谁输,相同则平局。

    思路:
    因为一开始就是回文,所以Alice只好进行1操作,如果可以在这个操作之后仍然让它时一个回文,那Alice就赢定了。

    比如:10001

    Alice进行1之后,10101

    Bob没法翻转,只好进行1操作,11101

    这时,Alice很聪明,不会傻傻地让自己的数字变大,选择2翻转字符串,10111

    Bob只好继续1操作,111111

    这时Bob都已经数字为2了,Alice为1,所以Alice胜

    比如:10101

    Alice 1操作之后并不是回文,这就让Bob有机可乘,最后是Bob赢了

    关键在于,Alice第一次操作之后它还是不是回文,而这取决于改字符串中间的字符是不是0,只有中间的字符是0,且字符串长为奇数(偶数的话连中间都没有)的时候Alice赢,否则Bob赢。

    还有一个特殊情况,就是只有一个0的时候,因为刚开始就是回文,Alice只好1操作,所以Alice必定+1,而Bob是0,所以Bob赢了。

    void solve() {
    	int n; string s;
    	cin >> n >> s;
    	int cnt0 = count(s.begin(), s.end(), '0');
    	if (cnt0 == 1)cout << "BOB
    ";//只有一个0
    	else if (n & 1 and s[n / 2] == '0')cout << "ALICE
    ";//是奇数个,中间为0
    	else cout << "BOB
    ";
    }
    

    压缩判断条件:奇数个0 并且不为 1 个,此时一定是 Alice 获胜了

    void solve() {
    	int n; string s;
    	cin >> n >> s;
    	int k = count(s.begin(), s.end(), '0');
    	if (k & 1 and k != 1)cout << "ALICE
    ";
    	else cout << "BOB
    ";
    }
    

    1527B2. Palindrome Game (hard version)

    题目大意:

    这题跟B1的区别就是,一开始的字符串可能不是回文。

    思路:

    一开始是回文时,用B1的结论。

    一开始不是回文时,Alice就可以一开始就翻转,让Bob喘不过气来,所以Bob不可能赢。

    但是有一种平局的情况,001,只有2个0,且中间是0。

    void solve() {
    	int n; string s;
    	cin >> n >> s;
    	int k = count(s.begin(), s.end(), '0');
    	string b = s;
    	reverse(b.begin(), b.end());
    	if (b == s) { // 用 1 的结论
    		if (k == 1 || !(n & 1))cout << "BOB
    ";
    		else if ((n & 1) and s[n / 2] == '0')cout << "ALICE
    ";
    		else cout << "BOB
    ";
    	} else {
    		if (k == 2 and (n & 1) and s[n / 2] == '0')
    			cout << "DRAW
    ";
    		else cout << "ALICE
    ";
    	}
    }
    

    赛后 DP 写法:很复杂,不推荐

    const int N = 1010;
    int dp[N][N][2][2];
    int vis[N][N][2][2];
    int DP(int a, int b, int c, int d) {
    	if (vis[a][b][c][d])return dp[a][b][c][d];
    	vis[a][b][c][d] = 1;
    	int &tmp = dp[a][b][c][d] = N;
    	if (a + b + c == 0) tmp = 0;
    	if (b and d) tmp = min(tmp, -DP(a, b, c, 0));
    	if (a) tmp = min(tmp, -DP(a - 1, b + 1, c, 1) + 1);
    	if (b) tmp = min(tmp, -DP(a, b - 1, c, 1) + 1);
    	if (c) tmp = min(tmp, -DP(a, b, c - 1, 1) + 1);
    	return tmp;
    }
    void solve() {
    	int n; string s;
    	cin >> n >> s;
    	int a = 0, b = 0, c = 0;
    	for (int i = 0; i * 2 + 1 < n; ++i) {
    		if (s[i] == '0' and s[n - i - 1] == '0')a++;
    		if (s[i] != s[n - 1 - i]) b++;
    	}
    	if (n & 1 and s[n / 2] == '0')c = 1;
    	int ans = DP(a, b, c, 1);
    	if (ans > 0)cout << "BOB
    ";
    	else if (ans == 0)cout << "DRAW
    ";
    	else cout << "ALICE
    ";
    }
    

    1527C. Sequence Pair Weight

    题目大意:

    给一个数组,求每个子序列的一对相同数字的数量之和。

    分析:

    暴力 (mathcal{O}(n^2)),肯定不行,所以肯定有什么 (mathcal{O}(n)) 的方法可以实现。

    首先,答案肯定跟这些数字出现的次数有关,其次,也跟数字所在的位置有关。

    所以很容易想到用 map 去存储位置并计数

    void solve() {
    	map<int, ll>mp;
    	ll ans = 0;
    	int n;
    	cin >> n;
    	for (int i = 1, a; i <= n; ++i) {
    		cin >> a;
    		ans += mp[a] * (n - i + 1);
    		mp[a] += i;
    	}
    	cout << ans << "
    ";
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    mysql 覆盖索引
    mysql 连接查询 和 子查询
    mysql varchar
    uchome 是如何将数据插入数据库的
    Tomcat5 在windows安装服务
    Linux中错误码及描述查看
    Longines浪琴手表型号解释
    perl 安装 Net::Telnet 模块
    php一些错误的显示问题
    firefox样式表cursor和兼容Ie firefox,解决文字溢出的问题(wordwrap:breakword;wordbreak:breakall)
  • 原文地址:https://www.cnblogs.com/RioTian/p/14798330.html
Copyright © 2020-2023  润新知