codeforces round160 div 2

很久没有更新整场比赛的了哈,这场 v p vp vp的还行,半小时出了俩,后面 c c c做到 1 h 1h 1h没出就润了直接
比赛链接

A

思维题

题目大意

给定一个开头不为零的字符串,判断是否可以拆成两个没有前导零的整数,使得前面的整数小于后面的整数

思路

已知整数不能有前导零,所以从左往右找到第二个不为零的位置分开,此时一定为后面整数最大的情况,判断一下输出即可

ACcode

#include<bits/stdc++.h>
 
using namespace std;
 
void solve()
{
    string str;cin >> str;
    int n = str.size();
    int m = str[0] - '0';
    int x = 1;
    while (str[x] == '0') {
        m = m * 10 + str[x] - '0';
        x++;
    }
    int mm = 0;
    for (int i = x;i < n;i++) {
        mm = mm * 10 + str[i] - '0';
    }
    if (m >= mm) {
        cout << -1 << '\n';
    }
    else {
        cout << m << ' ' << mm << '\n';
    }
}
 
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

B

思维题

题目大意

给定 01 01 01 s s s,你有两种操作,删除任意一个字符花费 1 1 1金币,交换相邻的两个字符花费 0 0 0金币
定义字符串 t t t为好串: t [ i ] ! = s [ i ] , 0 < = i < = t . s i z e t[i]!=s[i] ,0<=i<=t.size t[i]!=s[i],0<=i<=t.size
你可以执行上述操作任意次,求得到好串 t t t的最小成本

思路

相邻任意交换:保持 0 0 0 1 1 1的数量不变,可以组合任意一个串,因为要求对应位的 0 , 1 0,1 0,1不同,而删除会影响对应位置,所以我们可以拟构造好串 t t t
统计 s s s 0 0 0 1 1 1的数量,从左到右遍历 s s s,逢 1 1 1 0 0 0,逢 0 0 0 1 1 1,知道某个数量小于零或正好两个都等于零,输出即可

ACcode

#include<bits/stdc++.h>

using namespace std;

void solve()
{
    string str;cin >> str;
    int n1 = 0;int n2 = 0;
    for (auto x : str) {
        if (x == '1')n1++;
        else n2++;
    }
    for (int i = 0;i < str.size();i++) {
        if (str[i] == '0')n1--;
        else n2--;
        if (n1 < 0 || n2 < 0) {
            cout << max(n1, n2) << '\n';
            return;
        }
    }
    cout << 0 << '\n';
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

C

思维题,结论题

题目大意

给定一个空的多集,给定 n n n行每行两个正整数 a , b a,b a,b,若 a = 1 a=1 a=1,则在多集中添加 2 b 2^b 2b,若 a = 2 a=2 a=2,则查询,在当前多集中,是否存在某几个集合的和等于 b b b。输出每个查询的结果( y e s / n o yes/no yes/no)

思路

有一个结论需要记住,处理二进制凑数、加减问题,从大往小凑。而且 2 2 2的指数范围很小只到 29 29 29。所以思路出来了,用 v e c t o r vector vector存,查询时从大到小遍历,若小于则减,判断是否能减为 0 0 0

ACcode

#include<bits/stdc++.h>
 
using namespace std;
 
vector<int>a(30);
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;cin >> t;
 
    while (t--) {
        int b, c;cin >> b >> c;
        if (b == 1)a[c]++;
        else if (b == 2) {
            for (int i = 29;i >= 0;i--) {
                int ta = min(c >> i, a[i]);
                c -= ta << i;
            }
            cout << (c == 0 ? "YES" : "NO") << '\n';
        }
    }
    return 0;
}
  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值