Codeforces Round #696 (Div. 2)

Codeforces Round #696 (Div. 2)

A. Puzzle From the Future

贪心

#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;

void solve() {
    int n;
    string b;
    cin>>n>>b;
    int now;
    if(b[0]=='0')
        now=1;
    else now=2;
    cout<<1;
    for (int i = 1; i <n; ++i) {
        if(b[i]=='1'){
            if(now!=2){
                cout<<1,now=2;
            } else cout<<0,now=1;
        } else{
            if(now==1){
                cout<<0,now=0;
            } else cout<<1,now=1;
        }
    }
    cout<<"\n";
}

signed main() {
//    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

B. Different Divisors

素数筛,1,a,b,a*b

#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;

#define MAXN 1008611
char prime[MAXN];

void getPrime()
{
    memset(prime,1,sizeof(prime)); //默认全为素数
    prime[0]=prime[1]=0; //0和1不是素数
    for(int i=2;i<MAXN;i++)
    {
        if(prime[i]==1) //如果i是素数
        {
            for(int j=2;j*i<MAXN;j++)
            {
                prime[i*j]=0; //将所有i的倍数标记为非素数
            }
        }
    }
}

vector<int > v;

void solve() {
    int d;
    cin>>d;
    int i=lower_bound(v.begin(),v.end(),1+d)-v.begin();
    int j=lower_bound(v.begin(),v.end(),v[i]+d)-v.begin();
    cout<<v[i]*v[j]<<'\n';
}

signed main() {
//    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    getPrime();
    for (int i = 0; i < 1008611; ++i) {
        if(prime[i])
            v.push_back(i);
    }
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

//4   1 5 9 6 12 16

C. Array Destruction

枚举谁和最大数一起删除

#include <bits/stdc++.h>

#pragma GCC optimize(2)
//#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;

int a[maxn];

vector<int > check(int n,int sum){
    multiset<int> me(a,a+2*n);
    vector<int >res;
    for (int i = 0; i < n ; ++i) {
        auto it1=--me.end();
        int y=sum-*it1;
        res.push_back(*it1);
        me.erase(it1);
        auto it2=me.find(y);
        if (it2==me.end()) return {};
        res.push_back(*it2);
        me.erase(it2);
        sum-=y;
    }
    return res;
}

void solve() {
    int n;
    cin>>n;
    n*=2;
    for (int i = 0; i < n; ++i) cin>>a[i];
    sort(a,a+n);
    for (int i = 0; i < n - 1; ++i) {
        int sum=a[i]+a[n-1];
        vector<int > res=check(n/2,sum);
        if (res.size()){
            cout<<"YES\n"<<sum<<"\n";
            for (int j = 0; j < n; j+=2)
                cout<<res[j]<<" "<<res[j+1]<<"\n";
            return;
        }
    }
    cout<<"NO\n";
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

D. Cleaning

根据题意从俩头都来一遍遍历消除,检查是否有相等的时候,或者考虑互换i和i+1,检查是否满足

#include <bits/stdc++.h>

#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int inf=1e18;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;

int a[maxn];
int b[maxn],c[maxn];

void solve() {
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    int n;
    cin>>n;
    for (int i = 1; i <=n; ++i)
        cin>>a[i];
    for (int i = 1; i <=n; ++i)
        if (a[i]>=b[i-1]) b[i]=a[i]-b[i-1];
        else b[i]=inf+1;
    for (int i = n; i >0; --i)
        if (a[i]>=c[i+1]) c[i]=a[i]-c[i+1];
        else c[i]=inf-1;
    int f1=0;
    for (int i = 1; i <n; ++i) {
        if (b[i]==c[i+1]){
            f1=1;
            break;
        }else{
            if (a[i]>=c[i+2]&&a[i+1]>=b[i-1]){
                int t1=a[i]-c[i+2];
                int t2=a[i+1]-b[i-1];
                if (t1==t2) {
                    f1=1;
                    break;
                }
            }
        }
    }
    cout<<(f1?"YES":"NO")<<"\n";
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页