Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)【ABCD】(题解)

目录

  • 涵盖知识点:思维、树状数组。

    • 比赛链接:传送门
    • A - Even Subset Sum Problem
    • B - Count Subrectangles
    • C - Unusual Competitions
    • D - Present
    • E - Instant Noodles
    • F - Reality Show

涵盖知识点:思维、树状数组。

比赛链接:传送门

A - Even Subset Sum Problem

题意: 找一个子序列使得和为偶数
题解: 选一个偶数或者两个奇数。
Accept Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn=110;
int a[maxn];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int flag=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            if(!(a[i]&1))flag=i;
        }
        if(flag){
            puts("1");
            cout<<flag<<"\n";
        }
        else{
            if(n==1){
                puts("-1");
            }
            else{
                puts("2\n1 2");
            }
        }
    }
    return 0;
}

B - Count Subrectangles

题意: 给两个数组\(a,b\)。矩阵\(c\)满足\(c_{i,j}=a_ib_j\)。问\(c\)中有多少个子区域全为1且1的个数恰好为\(k\)。
题解: 子区域的长宽一定为\(k\)的因子,枚举一遍分别在\(a,b\)数组里面扫描一下可能性。
Accept Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn=4e4+10;
typedef long long ll;
int a[maxn],b[maxn],n,m,k;
int solve(int x){
    int cnt1=0,cnt2=0,cnt=0,y=k/x,res=0;
    for(int i=1;i<=n;i++){
        if(a[i])cnt1++;
        else cnt1=0;
        if(cnt1>=x)cnt++;
    }
    for(int i=1;i<=m;i++){
        if(b[i])cnt2++;
        else cnt2=0;
        if(cnt2>=y)res+=cnt;
    }
    return res;
}
int main(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=m;i++)cin>>b[i];
    ll ans=0;
    for(int i=1;i<=sqrt(k);i++){
        if(k%i==0){
            ans+=solve(i);
            if(k/i!=i)
                ans+=solve(k/i);
        }
    }
    cout<<ans<<"\n";
    return 0;
}

C - Unusual Competitions

题意: 给定一个左右圆括号序列。每次可以花费\(l\)以选择长度为\(l\)的字串进行排序。问最少花费多少使得括号匹配。
题解: 从左向右贪心,若右括号之前没有左括号匹配就像后扫描到第一个可以完全匹配所有右括号的点,记录区间长度。
Accept Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
typedef long long ll;
int a[maxn];
int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int cnt1=0,cnt2=0;
    for(char i:s){
        if(i=='(')cnt1++;
        else cnt2++;
    }
    if(cnt1!=cnt2){
        puts("-1");
        return 0;
    }
    int ans=0,cnt=0,l=0;
    for(int i=1;i<=s.length();i++){
        if(s[i-1]=='(')a[i]=a[i-1]-1;
        else a[i]=a[i-1]+1;
        if(a[i]==0){
            if(a[i-1]==1){
                ans+=i-l;
            }
            l=i;
        }
    }
    cout<<ans<<"\n";
    return 0;
}

D - Present

题意: 给定数组\(a\),求下列表达式:
\[(a_1 + a_2) \oplus (a_1 + a_3) \oplus \ldots \oplus (a_1 + a_n) \\ \oplus (a_2 + a_3) \oplus \ldots \oplus (a_2 + a_n) \\ \ldots \\ \oplus (a_{n-1} + a_n) \\\]
题解: 考虑最后答案的每一位。排除低位对高位的影响后,对于所有数字\(a\),对第\(k\)位的贡献等同于\(a\% 2^{k}\)对第k位的贡献。对于\(pos=a\% 2^{k}\)而言,只有位于区间\([2^{k}-1-pos,2^{k}-1]\)内的数才能改变第\(k\)位的状态。所以,我们可以对于每一位开一个树状数组维护区间状态。枚举最多25位即可。
Accept Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn=4e5+10,maxc=(1<<25)+10;
int a[maxn],n,v;
bool c[maxc];
inline int lowbit(int x){return x&(-x);}
void add(int x){
    while(x<v){
        c[x]^=1;
        x+=lowbit(x);
    }
}
bool query(int x){
    bool res=0;
    while(x){
        res^=c[x];
        x-=lowbit(x);
    }
    return res;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int ans=0;
    for(int p=0;p<25;p++){
        memset(c,0, sizeof c);
        v=1<<p;
        int res=0;
        for(int i=1;i<=n;i++){
            if(a[i]>>p&1)res++;
        }
        res=(res&1)&((n-res)&1);
        for(int i=1;i<=n;i++){
            int pos=a[i]%v;
            if(pos){
                res^=query(v-1)^query(v-pos-1);
                add(pos);
            }
        }
        if(res)ans=ans|(1<<p);
    }
    cout<<ans<<"\n";
    return 0;
}

E - Instant Noodles

F - Reality Show

原文地址:https://www.cnblogs.com/charles1999/p/12442219.html

时间: 2024-07-30 11:52:08

Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)【ABCD】(题解)的相关文章

Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)

A. Even Subset Sum Problem 题意:找和是偶数的子集,没什么好说的,直接上代码. #include <iostream> #include <algorithm> using namespace std; int n,x; int a[110]; int main() { int t; scanf("%d",&t); while(t--) { x=0; scanf("%d",&n); for(int i

Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)B(位运算,二分查找)

从低到高枚举当前位i,把所有数字对1<<(i+1)取模,因为比i位高的数字不会影响到低位,在这些数中,两两组成一对,每对的和如果在第i位上为1,++计数,如果计数为奇数,则答案上这一位为1.这个组对的过程通过排序后二分查找完成. 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[400007]; 5 int b[400007]; 6 int main(){

Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)(A-C)

题意:根据第一段,只是让你找到任意一组元素,和为偶数即可.还可以根据OUTPUT最后一句,多种答案,输出任意一种.    解析:随便找嘛,所以如果碰到单个偶数,直接输出,否则找出任意两个奇数输出即可.都没有,就输出-1. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<map> using namespace std; typedef l

Codeforces Round #500 (Div. 2) [based on EJOI]

Codeforces Round #500 (Div. 2) [based on EJOI] https://codeforces.com/contest/1013 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define IT set<node>::iterator 6 #def

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High [贪心 II][数据结构 I]

题目:http://codeforces.com/contest/867/problem/E 题意:模拟股票操作,每天只能买一只股票或者卖一只股票或者什么也不做,求最大利润. 题解:仔细想想是非常简单的一个贪心问题,理解为连续的多次贪心买入卖出可以合并为一次的买入卖出,且值为最优.只需要用一个最小堆每次寻找前面的最小且没有被标记为购买点的值即可.如果自己为最小值,continue.如果那个值没有被选过,为买入点,pop.如果那个值被选为了售出的点,那么取消售出的标记,把当前点标记为售出点,利润直

D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)

http://codeforces.com/contest/851/problem/D 分区间操作 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano]

http://codeforces.com/contest/1079/problem/C 题目大意:给出一个数列a[n],让构造一个满足下列条件的数列b[n]:如果a[i]>a[i-1]那么b[i]>b[i-1],如果a[i]<a[i-1]那么b[i]<b[i-1],如果a[i]==a[i-1],那么b[i]!=b[i-1].其中1<=b[i]<=5  1<=a[i]<=2*1e5. 题解:dp[i][j]表示在位置i处取j时是否成立,如果成立则dp[i][

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最长我们肯定是取最小x和最大的y连起来就完事. 但是要求长度最小又得覆盖,那么可以这样想,我们需要把最小的x不断右移到这条线段的y, 最大的左移到x,所以就是最大x-最小y完事 #include <bits/stdc++.h> using namespace std; #define ll long

【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B:数学+排序 C:字符串搜索 A // https://codeforces.com/contest/1277/problem/A /* 题意: 给出一个数,求不大于该数的完美整数的个数(完美整数指全是一个数组成的数字,如:111, 333333, 444444, 9, 8888 ...) 分析: