Educational Codeforces Round 153 (Rated for Div. 2)

Educational Codeforces Round 153 (Rated for Div. 2)

A. Not a Substring

思路:构造题
我们发现,最终合法的式子中一定含有"()“,那么当s为”()“时一定输出 ,然后分类讨论,当s中存在”)(“,那么我们就用括号套括号,就是”(())“来表示,显然这中间不会有”)(“子序列存在,如果不存在”)(“,那我们就用并列括号来表示,即”()()"。

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
const int N=3e6+10,M=2e5;
typedef double db;
typedef pair<int,int>pii;
int n,m,k,Q,cnt,t,a1,a2;
vector<int>del;
int a[N],b[N];
int prime[N];
bool st[N];
map<int,int>mp;
int lowbit(int x) {
	return (-x)&x;
}
void solve(){
    string s;std::cin>>s;
    if(s=="()"){
        std::cout<<"NO\n";
        return ;
    }
    else{
        cout<<"YES\n";
        int f=0;
        rep(i,1,s.size()-1){
            if(s[i]==s[i-1]){
                f=1;
                break;
            }
        }
        if(f){
            string c="()";
            rep(i,0,s.size()-1)std::cout<<c;
            std::cout<<"\n";
        }else{
            rep(i,0,s.size()-1)std::cout<<"(";
            rep(i,0,s.size()-1)std::cout<<")";
            std::cout<<"\n";
        }
    }
}
signed main(){
    std::cin>>t;
    while(t--)solve();
}
//-2 -2 -2 -2 0 0 0 0 0 3

B. Fancy Coins

思路:
首先我们预处理出最多需要多少个ak为x,在ak为x个的情况下a1有y个,然后当a2>=x,在判断a1和y的大小即可,当a2<x,首先将需要的y进行判断,如果a1<=y 答案就为x-a2+y-a1,如果a1>y在将a1-y后的值跟新x,即x-=(y/k),然后输出max(0ll,x-a2)即可

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
const int N=3e6+10,M=2e5;
typedef double db;
typedef pair<int,int>pii;
int n,m,k,Q,cnt,t,a1,a2;
vector<int>del;
int a[N],b[N];
int prime[N];
bool st[N];
map<int,int>mp;
int lowbit(int x) {
	return (-x)&x;
}
void solve(){
    std::cin>>m>>k>>a1>>a2;
    int res=0;
    int x=m/k;
    int y=m%k;
    if(a2>=x){
        if(a1>=y){
            std::cout<<0<<"\n";
        }else{
            std::cout<<y-a1<<"\n";
        }
    }else{
        if(a1>=y){
            a1-=y;
            x-=(a1/k);
            std::cout<<max(0ll,x-a2)<<"\n";
        }
        else {
            std::cout<<x-a2+y-a1<<"\n";
        }
    }
}
signed main(){
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    std::cin>>t;
    while(t--)solve();
}
//-2 -2 -2 -2 0 0 0 0 0 3

C. Game on Permutation

思路:
我们可以从左到右来考虑,假如Alice在点 i 下了棋,那么考虑 i 前面是否存在可选择的必胜状态,如果存在必胜状态或者前面没有能选择的状态,那么 i 就是必败状态,反之为必胜,计算必胜状态的数量即可。我们用 min 表示 i 前面的最小值, minlose 表示必胜状态的最小值,如果我们的 a[i] 大于 min ,说明前面有可选的状态,如果大于 minlose ,说明前面有可选的必胜状态。

#include <bits/stdc++.h>

using i64 = long long;

void solve() {
    int n;
    std::cin >> n;
    
    std::vector<int> p(n);
    for (int i = 0; i < n; i++) {
        std::cin >> p[i];
        p[i]--;
    }
    
    int minlose = n;
    int min = n;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int win = 0;
        if (p[i] < min) {
            min = p[i];
            win = 1;
        } else {
            win = (minlose < p[i]);
        }
        if (!win) {
            ans += 1;
            minlose = std::min(minlose, p[i]);
        }
    }
    
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

方法二 lowbit(x)实现

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
const int N=2e6+10,M=2e5;
typedef double db;
typedef pair<int,int>pii;
int n,m,k,Q,cnt,t;
vector<int>del;
int a[200010],b[200010];
int prime[N];
bool st[N];
map<int,int>mp;
int lowbit(int x){
    return x&(-x);
}
void solve(){
   std::cin>>n;
   int l=1;
   a[l++]=n;
   if(n&1){
       a[l++]=n-1;
       n--;
   }
   while(n>1){
       if(lowbit(n)==n){
           a[l++]=lowbit(n)/2;
           n-=(lowbit(n)/2);
       }
       else {
           a[l++]=n-lowbit(n);
           n-=lowbit(n);
       }
   }
   std::cout<<l-1<<endl;
   rep(i,1,l-1)std::cout<<a[i]<<" ";
   std::cout<<"\n";
}
signed main(){
    cin>>t;
    while(t--)solve();
}

D. Balanced String

思路:
在这里插入图片描述
在这里插入图片描述
代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 110, M = N * (N - 1) / 2;
int dp[N][M];
void solve()
{
	string op;
	cin >> op;
	int n = op.size();
	int c1 = count(op.begin(), op.end(), '1'), c0 = n - c1;
	int need = (n * (n - 1) / 2 - c0 * (c0 - 1) / 2 + c1 * (c1 - 1) / 2) / 2;
	memset(dp,0x3f,sizeof dp);
	dp[0][0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = c1; j >= 1; j--) {
			for (int k = i; k <= need; k++) {
				dp[j][k] = min(dp[j][k], dp[j - 1][k - i] + (op[i] == '0'));
			}
		}
	}
	cout << dp[c1][need] << endl;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	while (t--)
		solve();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linkk_bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值