Codeforces Round #478 (Div. 2) ABC

比赛链接

A题 解题思路:


给予 n 个字符串,让求有多少个不同的词根(set存储)。

首先我们将每个都set去重,然后使用map数组存储当前词根,然后最后进行统计。


代码:


#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const long long M = 1e9 + 7;

const int N = 300;

typedef long long ll ;

map <string,int> ans;

int res[30];

int main(){
    int n;
    char st[3000];
    scanf("%d",&n);
    for (int i = 0; i < n; i++){
        int m = 0;
        memset(res,0,sizeof res);
        scanf("%s",st);
        for (int j = 0; j < strlen(st);j++){
            res[st[j] - 'a' ] = 1;
        }
        string s="";
        for (int j = 0; j <= 25; j++){
            if (res[j] != 0) s += 'a' + j;
        }
        ans[s] = 1;
    }
    printf("%d\n",ans.size());
    return 0;
}


B题 解题思路:


B题主要是先读懂题意,他说的是拿有偶数的石头,题目中 n 的的数目是14,所以暴力枚举就OK,但是其中要注意超时,就是一个一个分配可能会超时 我们之间将拿的总数ans[i] / 14,即可,然后对每个进行分配,% 14 剩下的再另行分配。


代码:


#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const long long M = 1e9 + 7;

const int N = 300005;

typedef long long ll ;

ll ans[15];
ll fun[15];

int main(){
    for (int i = 0; i < 14; i++){
        scanf("%lld",&ans[i]);
        fun[i] = ans[i];
    }
    ll mx = -1;
    for (int i = 0; i < 14; i ++){
        fun[i] = 0;
        ll j = i + 1;
        ll result = 0;
        ll res = ans[i];
        ll l = ans[i] / 14;  // 注意不要一次一次分配,容易超时
        res = res % 14;
        for (int k = 0; k < 14; k++){
            fun[k] += l;
        }
        while(res--){  // 对剩下的进行分配
            j = j % 14;
            fun[j] += 1;
            j ++;
        }
        for (int k = 0; k < 14; k++){
            if (fun[k] % 2 == 0) result += fun[k];
        }
        if (result > mx){
            mx = result;
        }
        memcpy(fun,ans,sizeof ans);  // 最后还原数组
    }
    printf("%lld",mx);
    return 0;
}


C题 解题思路:


利用前缀和+二分(反正我想不到,还是做题少),对于当前输入的值 t ,我们判断当消费 t 时,我们还存活几个士兵,当消费全部时,那么全部复活。
因此我们保证原数组不变,我们利用动态更换 a 来判断剩余的士兵。我们用 a 存储之前花费的,如果超过ans[ n ] , 那么就使 a 为 0 ,否则判断完后加上当前的值。


代码:


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

using namespace std;

const long long M = 1e9 + 7;

const int N = 200010;

typedef long long ll ;

ll a = 0;
ll ans[N];

int bsearch(int l , int r ,ll x){  // 二分查找
    while(l < r){
        int mid = l + r >> 1;
        if (ans[mid] > x + a) r = mid;
        else l = mid + 1;
    }
    return l;
}

int main(){
    int n , q;
    scanf("%d%d",&n,&q);
    for (int i = 1; i <= n; i++){  // 计算前缀和
        scanf("%lld",&ans[i]);
        ans[i] += ans[i - 1];
    }
    for (int i = 1; i <= q; i++){
        ll t;
        scanf("%lld",&t);
        if (t + a >= ans[n]){  // 如果当前值会让所有的士兵全部阵亡,那么全部复活
            printf("%d\n",n);
            a = 0;  // 全部复活后让 a 为 0
        }
        else{
            int l = bsearch(1,n,t);  // 二分查找当前的位置
            a += t;  // 将当前的值加到 a
            printf("%d\n",n - l + 1);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值