1. 程式人生 > 實用技巧 >Codeforces Round #691題解

Codeforces Round #691題解

A題

貪心思路,注意到他們每個是每個排列進行比較,因此只要判斷兩個字串對應位置的大小,誰多就誰贏

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=5e5+10;
int main(){
    ios::sync_with_stdio(false);
    int i,j;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin
>>n; string s; string x; cin>>s>>x; int cnt1=0,cnt2=0; for(i=0;i<n;i++){ if(s[i]>x[i]){ cnt1++; } else if(s[i]<x[i]){ cnt2++; } } if(cnt1>cnt2){ cout
<<"RED"<<endl; } else if(cnt1==cnt2){ cout<<"EQUAL"<<endl; } else{ cout<<"BLUE"<<endl; } } return 0; }
View Code

B題

這道題總結出來其實就是發現我們有四個方向,最後只要有多少方案就是答案,因此就是給四個方向分配個數

只要用乘法原理就能解答

#include<bits/stdc++.h>
using
namespace std; typedef long long ll; typedef pair<int,int> pll; const int N=5e5+10; int main(){ ios::sync_with_stdio(false); int n; cin>>n; if(n%2==0){ cout<<(n/2+1)*(n/2+1)<<endl; } else{ cout<<2*((n-1)/2+2)*((n-1)/2+1)<<endl; } return 0; }
View Code

C題

先排序之後兩兩取gcd

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=5e5+10;
ll n,m;
ll ans;
ll a[N],b[N];
ll gcd(ll x,ll y){
    return y?gcd(y,x%y):x;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    int i,j;
    for(i=1;i<=n;i++)
        cin>>a[i];
    for(i=1;i<=m;i++)
        cin>>b[i];
    sort(a+1,a+n+1);
    if(n>=2)
        ans=a[2]-a[1];
    for(i=3;i<=n;i++){
        ans=gcd(ans,a[i]-a[i-1]);
    }
    for(int i=1;i<=m;i++){
        cout<<gcd(ans,a[1]+b[i])<<" ";
    }
    return 0;
}
View Code