Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)

A题:

题解:
让第一行去平摊第二行没有的代价就可以达到最大最小了。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int a[N],b[N];
int main()
{
    int n ; cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int res=0,num=0,flag=0;
    for(int i=1;i<=n;i++){
        if(a[i]==1&&b[i]==0) num++;
        if(a[i]==0&&b[i]==1) res++;
        if((a[i]&&b[i])||(!a[i]&&!b[i])) flag++;
    }
    if(flag==n||(num==0&&res>0)) cout<<-1<<endl;
    else cout<<ceil(res/num)+1<<endl;
}

B题:

题解:
移项我们可以发现,每个数的 c i − b i c_i-b_i cibi的值是一样的,所以利用桶来储存最大值,因为相减可能为负数,所以加一个偏移量。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+7;
const int base=4e5;
int a[N],b[N];
signed main()
{
    int n,res=0; cin>>n;
    for(int i=1;i<=n;i++){
        int tmp; scanf("%lld",&tmp);
        a[tmp-i+base]+=tmp;
        res=max(res,a[tmp-i+base]);
    }
    cout<<res<<endl;
}

C题:

题解:
从后往前扫一遍,直到不能更新这个答案为止。

#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=2e5+7;
int main()
{
    int n; scanf("%d",&n);
    string s; cin>>s;
    int res=0;
    for(char i='z';i>='a';i--){
        while(1) {
            int flag=0;
            for (int j = 0; j < n; j++) {
                if (s[j] == i) {
                    if (s[j - 1] + 1 == s[j] || s[j] == s[j + 1] + 1) {
                        res++;
                        s.erase(s.begin() + j);
                        n--;
                        flag=1;
                        break;
                    }
                }
            }
            if(!flag) break;
        }
    }
    cout<<res<<endl;
}

D题:

题解:
因为我们是在向终点的过程中看是否发生了变向,所以我们反向建图,预处理出所有的点到终点的距离。然后看看每条路是否在最短路上或者距离是否和最短路相同。

#include <bits/stdc++.h>
using namespace std;
int n,m,K,a[200005],dis[200005];
vector<int> g[200005],g2[200005];
void BFS(int s){
    queue<int> q;
    memset(dis,0x3f,sizeof(dis));
    dis[s]=0;
    q.push(s);
    while(!q.empty()){
        int now=q.front();
        q.pop();
        for(int i=0;i<g2[now].size();i++){
            int y=g2[now][i];
            if(dis[y]==0x3f3f3f3f){
                dis[y]=dis[now]+1;
                q.push(y);
            }
        }
    }
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1,x,y;i<=m;i++){
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g2[y].push_back(x);
    }
    scanf("%d",&K);
    for(int i=1;i<=K;i++)scanf("%d",&a[i]);
    BFS(a[K]);
    int minn=0,maxx=0;
    for(int i=1;i<K;i++){
        if(dis[a[i+1]]+1!=dis[a[i]])minn++;
        for(int j=0;j<g[a[i]].size();j++){
            int y=g[a[i]][j];
            if(y!=a[i+1]&&dis[y]+1==dis[a[i]]){
                maxx++;
                break;
            }
        }
    }
    cout<<minn<<' '<<maxx<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值