CodeForces #624 Div3 部分题解

A. Add Odd or Subtract Even

 

题目大意:

直接根据两数字大小进行判断即可,代码稍长:

AC代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <stack>
#include <queue>
//#include <iomanip>
//#include <map>
//#include <time.h>
using namespace std;
typedef long long ll;
inline int read() {
    int x = 0;int f = 1; char c = getchar();
    while(c<'0' || c>'9') {if(c=='-') f = -f; c = getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}
inline void write(int x) {
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[36];int tot = 0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
//mt19937 rnd(time(NULL));
const int inf = 0x3f3f3f3f;
const int maxn = 40;
int a, b;
int t;
int main() {
    cin >> t;
    while(t--) {
        cin >> a >> b;
        if(a == b) {
            cout << 0 << endl;
        }
        else if(a > b) {
            if((a%2 == 0) && b %2 == 0) {
                cout<<1 << endl;
            }else if(a%2==1 && b%2 ==0){
                cout << 2 << endl;
            }else if(a%2 ==1 && b%2 == 1) {
                cout <<1<<endl;
            }else if(a%2==0&&b%2==1) {
                cout << 2 << endl;
            }
        }
        else if(a<b){
            if((a%2==0) && (b%2==0)) {
                cout << 2 << endl;
            }else if((a%2 ==0) &&(b%2==1)) {
                cout << 1 << endl;
            }else if((a%2==1)&&(b%2==0)) {
                cout << 1<<endl;
            }else if((a%2==1)&&(b%2==1)) {
                cout << 2 << endl;
            }
 
        }
    }
 
    return 0;
}
 

 

B. WeirdSort

 

题目大意:

给定一组数列,另给一组位置P[i],你可以互换P[i]和P[i]+1两处位置的数字。

问若干次操作后能否使得整个数列变为非减的。

 

思路:

1.纯模拟

思路是只要数组中还存在有逆序的地方且该处还可以进行交换就直接交换,知道不能进行以上操作为止。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <stack>
#include <queue>
//#include <iomanip>
//#include <map>
//#include <time.h>
using namespace std;
typedef long long ll;
inline int read() {
    int x = 0;int f = 1; char c = getchar();
    while(c<'0' || c>'9') {if(c=='-') f = -f; c = getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}
inline void write(int x) {
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[36];int tot = 0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
//mt19937 rnd(time(NULL));
const int inf = 0x3f3f3f3f;
const int maxn = 4e5 + 10;
int n;
int t;
int main() {
    cin >> t;
    while(t--) {
        int n, m;
        cin >> n >> m;
        vector<int> a(n);
        for(int i = 0; i < n; i++) cin >> a[i];
        vector<int> p(n);
        for(int i = 0; i < m; i++) {
            int num;
            cin >> num;
            p[num - 1] = 1;
        }
        while(1) {
            bool ok = false;
            for(int i = 0; i < n; i++) {
                if(p[i] && a[i] > a[i + 1]) {
                    swap(a[i],a[i+1]);
                    ok = true;
                }
            }
            if(!ok) break;
        }
        bool ok = true;
        for(int i = 0; i < n - 1; i++) {
            if(a[i] > a[i+1]) {
                ok = false;
                break;
            }
        }
        if(ok) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

 

2.思维

可以想到,整个数组串被分割成了若干个部分,只要是在可排序区间的数字,就一定能保证其区间内有序。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <stack>
#include <queue>
//#include <iomanip>
//#include <map>
//#include <time.h>
using namespace std;
typedef long long ll;
inline int read() {
    int x = 0;int f = 1; char c = getchar();
    while(c<'0' || c>'9') {if(c=='-') f = -f; c = getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}
inline void write(int x) {
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[36];int tot = 0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
//mt19937 rnd(time(NULL));
const int inf = 0x3f3f3f3f;
const int maxn = 4e5 + 10;
int n;
int t;
int main() {
    cin >> t;
    while(t--) {
        int n, m;
        cin >> n >> m;
        vector<int> a(n);
        for(int i = 0; i < n; i++) cin >> a[i];
        vector<int> p(n);
        for(int i = 0; i < m; i++) {
            int pos;
            cin >> pos;
            p[pos- 1] = 1;
        }
        for(int i = 0; i < n; i++) {
            if(p[i] == 0) continue;
            int j = i;
            while(j < n && p[j]) j++;
            sort(a.begin()+i,a.begin()+j+1);
            i = j;
        }
        bool ok = true;
        for(int i = 0; i < n - 1; i++) {
            if(a[i] > a[i + 1]) {
                ok = false;
                break;
            }
         }
        if(ok) cout << "YES" << endl;
        else cout <<"NO" << endl;
    }
    return 0;
}

 

C. Perform the Combo

 

题目大意:

给定一字符串和一个数组,字符串中从头到尾扫描,每次到数组中的元素位置时,就会退回开头,问该串中各个字符分别要出现多少次。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值