Codeforces Round #624 (Div. 3) A—C

A - Add Odd or Subtract Even (奇偶)

将x变成y
两种操作
1,x可以加一个任意的奇数
2,x可以减一个任意的偶数

判断x与y的奇偶,以及他的大小关系即可

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
typedef long long ll;
#define Pll make_pair
#define PB push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int MAXN=1e5+50;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int x,y;
        cin>>x>>y;
        if(x==y){
            cout<<0<<endl;
            continue;
        }
        if(x%2==1&&y%2==1&&x<y) {
            cout << 2 << endl;
            continue;
        }
        if(x%2==1&&y%2==1&&x>y) {
            cout << 1 << endl;
            continue;
        }
        if(x%2==1&&y%2==0&&x<y) {
            cout << 1 << endl;
            continue;
        }
        if(x%2==1&&y%2==0&&x>y) {
            cout << 2 << endl;
            continue;
        }
        if(x%2==0&&y%2==1&&x<y) {
            cout << 1 << endl;
            continue;
        }
        if(x%2==0&&y%2==1&&x>y) {
            cout << 2 << endl;
            continue;
        }
        if(x%2==0&&y%2==0&&x<y) {
            cout << 2 << endl;
            continue;
        }
        if(x%2==0&&y%2==0&&x>y) {
            cout << 1 << endl;
            continue;
        }
    }
    return 0;
}

B - WeirdSort(冒泡排序)

给一个长度为n的数组a
一个长度为m的数组b,该数组每个值x的意义是可以把a[x]与a[x+1]交换
问通过b数组里面元素的操作能否把a变成非递减的序列

数据比较小,冒泡排序,判断该交换的位置能否交换即可,不能输出NO

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
typedef long long ll;
#define Pll make_pair
#define PB push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int MAXN=1e5+50;
int b[150];
int a[150];
int main()
{
    int t;
    cin>>t;
    while(t--){
        map<int,int>mp;
        int n,m;
        cin>>n>>m;
        rep(i,1,n)cin>>a[i];
        int k;
        rep(i,1,m)cin>>k,mp[k]=1;
        int flag=1;
        for(int i=1;i<n;i++){
            for(int j=1;j<=n-i;j++){
                if(a[j]>a[j+1]){
                    if(mp[j]!=1) {
                        flag = 0;
                        break;
                    }
                    else swap(a[j],a[j+1]);
                }
            }
            if(flag==0)break;
        }
        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

C - Perform the Combo (前缀和)

一个长度为n的字符串(由小写字母组成)
一个长度为m的数组,数组的每个数x的意思就是统计一下在字符串中1到x中,各个字母的个数,最后m个统计完之后输出统计结果

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
typedef long long ll;
#define Pll make_pair
#define PB push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int MAXN=2e5+50;
char ch[MAXN];
int p[MAXN][30];
ll ans[30];
int n,m,_;
void solve(){
    memset(ans,0,sizeof(ans));
    rep(i,0,25)p[0][i]=0;//只有把第一组的置为0即可,如果memset的话,会超时
    //不写上面那一行也可以,本来定义的全局变量,里面的值就为0,而且整个过程它的值没发生改变
    scanf("%d%d",&n,&m);
    scanf("%s",ch+1);
    rep(i,1,n){
        rep(j,0,25)p[i][j]=p[i-1][j];
        p[i][ch[i]-'a']++;
    }
    int x;
    rep(i,1,m){
        scanf("%d",&x);
        rep(j,0,25){
            ans[j]+=p[x][j];
        }
    }
    rep(j,0,25){
            ans[j]+=p[n][j];
        }
    rep(j,0,25)printf("%lld%c",ans[j],j==25?'\n':' ');
}
int main()
{ 
    for(scanf("%d",&_);_;_--)solve();
    return 0;
}
发布了142 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44091178/article/details/104495755