Codeforces Round #626 Div. 2

Codeforces Round #626 Div. 2补题

A

在这里插入图片描述
思路:水题

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#include<bits/stdc++.h>
#define TEST freopen("C:\\Users\\hp\\Desktop\\ACM\\in.txt","r",stdin);
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;

const int inf=0x3f3f3f3f;
const ll INF=0x7fffffffffffffff;
const int mod=1e9+7;
const int maxn = 1e6+5;
const double eps=1e-8;

template<typename T> void read(T &x) {
    x = 0;
    char ch = getchar();
    ll f = 1;
    while(!isdigit(ch)) {
        if(ch == '-')f*=-1;
        ch=getchar();
    }
    while(isdigit(ch)) {
        x = x*10+ch-48;
        ch=getchar();
    }
    x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
    read(first);
    read(args...);
}
int sgn(double a) {
    return a<-eps?-1:a<eps?0:1;
}

inline int add(int x,int y) {
    return x+y>=mod?x+y-mod:x+y;
}
inline int sub(int x,int y) {
    return x-y<0?x-y+mod:x-y;
}
inline int mul(int x,int y) {
    return 1ll*x*y%mod;
}
inline int sq(int x) {
    return 1ll*x*x%mod;
}
int pow(int a,int b) {
    return b == 0?1:( b&1?mul(a,sq(pow(a,b/2))):sq(pow(a,b/2)));
}

int main() {
    int T;
    read(T);
    while(T--) {
        int n,sum=0,pos=0;
        read(n);
        for(int i=1; i<=n; i++) {
            int x;
            read(x);
            if(x&1) pos=i;
            sum+=x;
        }
        if(sum%2==0) {
            cout<<n<<"\n";
            for(int i=1; i<=n; i++) {
                cout<<i<<" ";
            }
        } else {
            if(n==1)
                cout<<"-1";
            else {
                cout<<n-1<<"\n";
                for(int i=1; i<=n; i++) {
                    if(i!=pos)
                        cout<<i<<" ";
                }
            }
        }
        cout<<"\n";
    }
}

B

在这里插入图片描述
思路:前缀和+约数枚举

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#include<bits/stdc++.h>
#define TEST freopen("C:\\Users\\hp\\Desktop\\ACM\\in.txt","r",stdin);
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int inf=0x3f3f3f3f;
const ll INF=0x7fffffffffffffff;
const int mod=1e9+7;
const int maxn = 1e6+5;
const double eps=1e-8;

template<typename T> void read(T &x) {
    x = 0;
    char ch = getchar();
    ll f = 1;
    while(!isdigit(ch)) {
        if(ch == '-')f*=-1;
        ch=getchar();
    }
    while(isdigit(ch)) {
        x = x*10+ch-48;
        ch=getchar();
    }
    x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
    read(first);
    read(args...);
}
int sgn(double a) {
    return a<-eps?-1:a<eps?0:1;
}

inline int add(int x,int y) {
    return x+y>=mod?x+y-mod:x+y;
}
inline int sub(int x,int y) {
    return x-y<0?x-y+mod:x-y;
}
inline int mul(int x,int y) {
    return 1ll*x*y%mod;
}
inline int sq(int x) {
    return 1ll*x*x%mod;
}
int pow(int a,int b) {
    return b == 0?1:( b&1?mul(a,sq(pow(a,b/2))):sq(pow(a,b/2)));
}
vector<pii>v;
void solve(int n){
    for(int i=1;i*i<=n;i++){
        if(n%i==0){
            v.push_back({i,n/i});
            if(i*i!=n)
            v.push_back({n/i,i});
        }
    }
}
int n,m,k;
int a[maxn],b[maxn];
int sum[maxn][2];
int main() {
    read(n,m,k);
    for(int i=1;i<=n;i++){
        read(a[i]);
        sum[i][0]+=sum[i-1][0]+a[i];
    }
    for(int i=1;i<=m;i++){
        read(b[i]);
        sum[i][1]+=sum[i-1][1]+b[i];
    }
    solve(k);
    ll ans=0;
    for(auto it:v){
        int x=it.first,y=it.second;
        int sum1=0,sum2=0;
        for(int i=1;i<=n-x+1;i++){
            sum1+=(sum[i+x-1][0]-sum[i-1][0]==x);
        }
        for(int i=1;i<=m-y+1;i++){
            sum2+=(sum[i+y-1][1]-sum[i-1][1]==y);
        }
        ans+=1ll*sum1*sum2;
    }
    cout<<ans<<"\n";
}

在这里插入图片描述
括号序列性质:左括号减1右括号加1。方法序列的情况需要满足下面的2个条件
1:前缀和为0时
2:前缀最小值为0时为合法序列
知道这些就好做了

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#include<bits/stdc++.h>
#define TEST freopen("C:\\Users\\hp\\Desktop\\ACM\\in.txt","r",stdin);
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
 
const int inf=0x3f3f3f3f;
const ll INF=0x7fffffffffffffff;
const int mod=1e9+7;
const int maxn = 1e6+5;
const double eps=1e-8;
 
template<typename T> void read(T &x) {
    x = 0;
    char ch = getchar();
    ll f = 1;
    while(!isdigit(ch)) {
        if(ch == '-')f*=-1;
        ch=getchar();
    }
    while(isdigit(ch)) {
        x = x*10+ch-48;
        ch=getchar();
    }
    x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
    read(first);
    read(args...);
}
int sgn(double a) {
    return a<-eps?-1:a<eps?0:1;
}
 
inline int add(int x,int y) {
    return x+y>=mod?x+y-mod:x+y;
}
inline int sub(int x,int y) {
    return x-y<0?x-y+mod:x-y;
}
inline int mul(int x,int y) {
    return 1ll*x*y%mod;
}
inline int sq(int x) {
    return 1ll*x*x%mod;
}
int pow(int a,int b) {
    return b == 0?1:( b&1?mul(a,sq(pow(a,b/2))):sq(pow(a,b/2)));
}
char s[maxn];
int cnt[2];
stack<char>st;
int main(){
    int n;
    cin>>n;
    cin>>s+1;
    for(int i=1;i<=n;i++){
        if(s[i]=='(') cnt[0]++;
        else cnt[1]++;
    }
    if(cnt[0]!=cnt[1]){
        cout<<"-1\n";
        return 0;
    }
    cnt[0]=cnt[1]=0;
    int sum=0,pre=0;
    int ans=0;
    for(int i=1;i<=n;i++){
        if(s[i]=='('){
            sum++;
            cnt[0]++;
        }
        else{
            sum--;
            cnt[1]++;
        }
        pre=min(sum,pre);
        if(sum==0&&pre==0&&cnt[0]==cnt[1]){
            cnt[0]=cnt[1]=0;
        }
        if(pre<0&&cnt[0]==cnt[1]&&sum==0){
            ans+=cnt[0]*2;
            cnt[0]=cnt[1]=pre=0;
        }
    }
    cout<<ans<<"\n";
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值