Codeforces Round #696 (Div. 2)

A.Puzzle From the Future

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        string s;
        cin >> s;
        string ans = "1";
        int flag = 1;
        char a = '1';
        for(int i = 1; i <= n - 1 ; i++){
 
            if( (int)s[i - 1] + (int)ans[i - 1]  == (int)s[i] + (int)a ){
                ans += '0';
            }
            else{
                ans += '1';
            }
            //cout << ans << endl;
        }
        cout << ans << endl;
    }
 
    return 0;
}

B.Different Divisors

#include <bits/stdc++.h>
using namespace std;
 
const int N = 7e6;
 
long long prime[N], cnt = 0;///下同
bool st[N] = {false};
void get_prime(int x) {
    for(int i = 2; i <= x; i++) {
        if(!st[i]) prime[cnt++] = i;
        for(int j = 0; prime[j] <= x / i; j++) {
 
            st[prime[j]*i] = true;///只用最小质因子筛,等于true就不是素数
            if(i % prime[j] == 0) break;
        }
    }
}
 
int main()
{
 
    int num = 1000000;
    get_prime(num);
    int t;
 
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        long long  p = lower_bound(prime,prime+cnt,n+1) - prime;
        long long  q = lower_bound(prime,prime+cnt,n + prime[p]) - prime;
        long long d = prime[p] * prime[q];
        cout << d <<endl;
    }
 
 
    return 0;
}

补题:

C.Array Destruction

~ 当时写了多久,最后发现有个点用错了就没写了、、、 ~

题意:

最开始选择最大的数和另一个数删除出序列,之后找到两个数,其和为上次删除的最大的数,
能找到输出,不能找到就“NO”

1.*prevz(s.end()),返回集合的最大值,不是返回迭代器!

2.在集合中查找,删除,时间复杂度是O( log(n) )的

3.如果在集合中直接erase( int x )值的话是会删除所有的这个值的元素(如果集合中有3个1,那么3个1都会被删除)

如果是erase( s.find() )find返回的是迭代器,那个就会只删除一个

#include <bits/stdc++.h>
using namespace std;


const int maxn=1e5;
typedef long long LL;
typedef pair<LL,LL>P;
multiset<LL>s;          ///存查询的集合
LL a[maxn];             ///原数组
LL temp;                ///是否有这么多个组合
P ans[maxn];            ///存最后的组合
int t,n;

bool f(int top)
{
    int las = top;

    while( !s.empty() ){
        top = *prev(s.end());               ///找到当前最大值
        s.erase( s.find(top) );             ///注意不能直接erase( top ) ,我比赛就是被这个坑了,
        auto oth = s.find( las - top );     ///他会erase所有值为top 的元素,如果有很多个相同的元素的话就会被wa
        if( oth != s.end() ){
            ans[temp++] = { top, las -top };
            s.erase( oth );
            s.erase( top );
            las = top;
        }
        else{
            return 0;
        }
    }

    if( temp == n ) return 1;
    else{
        return 0;
    }

}

void solve()
{
    cin >> t;

    while(t--){
        cin >> n;

        for(int i= 0 ; i < 2*n ; i++){
            cin >> a[i];
        }

        sort(a,a+2*n);

        int top = a[2*n-1];
        for(int i = 0 ; i < 2*n -1 ; i++){
            s.clear();                      ///初始化
            temp = 0;
            memset(ans,0,sizeof ans);
            for(int j = 0; j < 2*n-1 ; j++ ){
                s.insert(a[j]);
            }
            s.erase(s.find(a[i]));
            ans[temp++] = {top,a[i]};
            if( f(top) ){
                break;
            }

        }

        if( temp == n ){
            cout << "YES" <<endl;
            cout << ans[0].first + ans[0].second<<endl;
            for(int i = 0; i < temp ;i++){
                cout << ans[i].first << ' ' <<ans[i].second <<endl;
            }
        }
        else{
            cout <<"NO" <<endl;
        }

    }

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();

    return 0;
}
posted @ 2021-01-26 00:34  Hoppz  阅读(48)  评论(0编辑  收藏  举报