Acwing第 46 场周赛

第2题:AcWing 4397. 卡牌

卡住的点:不用具体实现翻牌、记录编号的操作,只需求d[i]=b[i]-a[i],之后将d排序,当d[i]>0时,不选,反之则选。

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

using namespace std;

const int N = 200010;

int n, m;
int a[N], b[N], d[N];

int main()
{
    cin >> n >> m;
    
    int sum = 0;
    for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]), sum += a[i];
    for (int i = 1; i <= n; i ++ ) scanf("%d", &b[i]), d[i] = b[i] - a[i];
    
    sort(d + 1, d + n + 1);
    
    for(int i = 1; i <= n - m; i ++ )  //最多能翻n-m次
        if(d[i] >= 0) break;
        else sum += d[i];
    
    printf("%d\n", sum);
    
    return 0;
}

第3题:AcWing 4398. 查询字符串

哈希即可

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

using namespace std;

int main()
{
    unordered_map<string, int> cnt;
    unordered_map<string, string> hash;
    
    int n, m;
    cin >> n;
    while (n -- )
    {
        unordered_set<string> S;
        string str;
        cin >> str;
        for(int i = 0; i < str.size(); i ++ )
        {
            string s;
            for(int j = i; j < str.size(); j ++ )
            {
                s += str[j];
                S.insert(s);
            }
        }
        
        for(auto& word: S)
        {
            cnt[word] ++;
            hash[word] = str;
        }
    }
    
    cin >> m;
    while (m -- )
    {
        string str;
        cin >> str;
        cout << cnt[str] << " ";
        if(!cnt[str]) puts("-");
        else cout << hash[str] << endl;
    }
    return 0;
}
posted @   esico  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示