acwing第46周赛

T1

第一次写的时候没有看清楚题目还以为先取完的获胜(测试样例的时候没有看清楚答案是不是正确的),结果wa了一次,呜呜呜呜~,我是撒币。

看清楚题目后发现每个人每次只用取一颗,实际上就是比较石子的大小。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n1, n2, k1, k2;

int main()
{
    cin >> n1 >> n2 >> k1 >> k2;
    
    if(n1 > n2)    puts("First");
    else    puts("Second");
    return 0;
}

T2

啊。。。这题看错了数据范围有wa了一次,我是真的憨。

 如果a[i]小于等于b[i]就加上a[i],统计a[i]小于b[i]的个数cnt,如果个数大于等于k,直接加上a[i]>b[i]是的b[i],否则找到a[i]-b[i]前k - cnt个最小的a[i]加上,最后加上其余的b[i];

我的代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;

const int N = 2e5 + 10;
typedef pair<int, int> PII;

int a[N], b[N], s[N];
bool stu[N];

int main()
{
    int n, k;
    cin >> n >> k;
    memset(stu, false, sizeof stu);
    for(int i = 1; i <= n; i ++)    cin >> a[i];
    for(int i = 1; i <= n; i ++)    cin >> b[i];
    for(int i = 1; i <= n; i ++)    s[i] = a[i] - b[i];
    
    int res = 0, cnt = 0;
    vector<PII> ans;
    for(int i = 1; i <= n; i ++){
        if(s[i] <= 0){
            res += a[i];
            stu[i] = true;
            cnt ++;
        }
        else{
            ans.push_back({s[i], i});
        }
    }
    
    if(cnt >= k){
        for(int i = 1; i <= n; i ++)
            if(!stu[i]) res += b[i];
    }
    else{
        sort(ans.begin(), ans.end());
        for(auto i : ans){
            if(cnt < k) res += a[i.second];
            else    res += b[i.second];
            cnt ++;
        }
    }
    cout << res;
    return 0;
}

看了y总的代码后,我发现我是真的菜

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 2e5 + 10;

int a[N], b[N], s[N];
int n, k;
int main()
{
    cin >> n >> k;
    int sum = 0;
    for(int i = 1; i <= n; i ++)    cin >> a[i], sum += a[i];
    for(int i = 1; i <= n; i ++)    cin >> b[i], s[i] = b[i] - a[i];
    
    sort(s + 1, s + 1 + n);
    
    for(int i = 1; i <= n - k; i ++)
        if(s[i] > 0)    break;
        else    sum += s[i];
        
    cout << sum;
    return 0;
}

T3

 

 这题没写出来超时了,我是直接在询问里进行查找,看了y总的讲解后发现应该先预处理。因为输入的字符串的长度都很小(最大为8),可以直接先列举子串。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int main(){
    int n;
    cin >> n;
    unordered_map<string, int> mp; //子串的个数
    unordered_map<string, string> m;    //子串对应的字符串
    
    while(n --){
        string str;
        cin >> str;
        unordered_set<string> s;    //因为每个字符串中可能有多个相同的子串,但是每个子串只能算一次
        for(int i = 0; i < str.size(); i ++)
            for(int j = i; j < str.size(); j ++)
                s.insert(str.substr(i, j - i + 1)); //求字符串中的子串
        for(auto i : s) mp[i] ++, m[i] = str;
    }
    int q;
    cin >> q;
    while( q --){
        string st;
        cin >> st;
        if(!mp[st]) puts("0 -");
        else    cout << mp[st] << " " << m[st] << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值