Codeforces Round 966 (Div. 3) 题解(C++)A-F

比赛地址 : 

Dashboard - Codeforces Round 966 (Div. 3) - Codeforces

直接模拟 ,没有什么好说的 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}


inline void solve(){
    int x ; cin >> x ;
    int y = to_string(x).size() ;
    if(y<3){
    	cout << "NO" << endl ; 
    	return ;
	}
    int t = qmi(10,y-2);
    int xx = x / t ;
    int yy = x % t ;
    if(xx!=10 || yy==0 || to_string(yy).size()!=y-2 || yy==1) {
        cout << "NO" << endl ;
    }else{
        cout << "YES" << endl ;
    }
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

模拟 ,每次判断相邻位置是否有乘客 , 如果都没有 ,那么直接返回false , 否则置a[x] = 1 , 继续下一个 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}


inline void solve(){
    int n ; cin >> n ;
    vector<int> a(n+2,0) ; 
    bool tag = true ;
    for(int i=1;i<=n;i++){
        int x ; cin >> x ;
        if(i==1){
            a[x] = 1 ;
            continue ;
        }else{
            if(a[x-1]==1||a[x+1]==1){
                a[x] = 1 ;
                continue ;
            }else{
                tag = false ;
            }
        }
    }
    if(tag) cout << "YES" << endl ;
    else cout << "NO" << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

C

字符串匹配 : 

1 . 先将每一个相同的a[i]值的下标放在一个vector中  这里用map来实现 ,key为a[i] ; 

2 . 对于每一个字符串 , 根据s[i]-'a'分成26个下标集合 ;

3 . 判断两个下标集合是否一一对应 ,输出yes / No ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}


inline void solve(){
    int n ; cin >> n ;
    vector<int> a(n,0) ;
    map<int,vector<int>> mp ;
    for(int i=0;i<n;i++) {
    	cin >> a[i] ;
    	mp[a[i]].push_back(i) ;
	}
    int m ; cin >> m ;
    while(m--){
        string s ; cin >> s ;// 只有小写
        if(s.size()!=n){
            cout << "NO" << endl ;
            continue ; 
        }
		vector<vector<int>> ax(26) ;
		for(int i=0;i<n;i++){
			ax[s[i]-'a'].pb(i) ; 
		}
		bool tag = true ;
		for(int i=0;i<26;i++){
			auto vec = ax[i] ;
			if(!vec.empty()){
				int ft = a[vec[0]];
				if(mp[ft]!=vec){
					tag = false ;
					break ;
				}
			}
		}
		if(tag) cout << "YES" << endl ;
		else cout << "NO" << endl ;
    }
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

D

贪心 ; 

从左到右遍历 , 遇到每一个'L' , 选取最远尚且没用过的'R'与之进行配对 , 求和的过程采用前缀和优化 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}


inline void solve(){
    int n ; cin >> n ;
    vector<int> a(n+1) , ps(n+1,0);
    for(int i=1;i<=n;i++) cin >> a[i] , ps[i] = ps[i-1] + a[i] ;
    vector<int> r ;
    string s ; cin >> s ;
    s = ' ' + s ;
    for(int i=1;i<=n;i++){
        if(s[i]=='R') r.pb(i) ;
    }
    LL ans =  0 ;
    int p = 1 ;
    for(int i=1;i<=n;i++){
        if(s[i]=='L'){
            if(r.empty()) break ;
            int x = r.back() ; r.pop_back() ;
            if(x<i) break ;
            ans += 1LL * p * (ps[x]-ps[i-1]) ;
            p++ ;
        }
    }
    cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

E

贪心 : 

先根据n,m,k求出矩阵每一个位置会被加上多少次  , 放入vector<int> b中 ;

贪心地将大猩猩数量多地放在被加次数多地格子中 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

int n , m , k ;

int f(int l , int x){
    if(l>2*k){
        if(x<=k) return x ;
        else if(x>k && x<l-k+1) return k ;
        else return l-x+1 ; 
    }else if(l==2*k){
        if(x<=k) return x ;
        else return l-x+1 ;
    }else if(l<2*k&&l>k) {
        if(x<=k) return min(x , l-k+1) ;
        else return l-x+1 ;
    }else if(l==k){
        return 1 ;
    }
}



inline void solve(){
    cin >> n >> m >> k ;
    int t ; cin >> t ;
    vector<int> a(t) ;
    for(int& x : a) cin >> x ;
    int ans = 0 ;
    sort(a.begin(),a.end()) ;
    vector<LL> b ;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            LL x = 1LL * f(n,i) * f(m,j) ;
            b.pb(x) ;
        }
    }
    sort(b.begin(),b.end()) ;
    int r = n * m - 1 ;
    for(int i=t-1;i>=0;i--){
        int x = a[i] ;
        ans += 1LL * x * b[r--] ;
    }
    cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

dp : 

dp[i]代表获取i分所需要的最少次数 ;

对于每一个矩阵 , 获取得到分数与相应最少操作次数存放在me数组中 ;

先枚举j  , 再枚举这个矩阵的得分p , 然后更新 ;

动态转移方程 :  dp[j+p] = min(dp[j+p] , dp[j] + me[p]) ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int INF = 1e9 ;

inline void solve(){
    int n , k ; cin >> n >> k ;
    vector<int> dp(k+1,INF) ;// 获得k分需要的最少次数
    dp[0] = 0 ;
    for(int i=0;i<n;i++){
        int a , b ; cin >> a >> b ;
        vector<int> me(k+1,INF) ;// 这个矩阵得分对应的操作次数
        me[0] = 0 ;
        int cnt = 0 , cost = 0 ;
        while(cnt<k && (a>0||b>0)){
            if(a<b) swap(a,b) ;
            cnt += 1 ;
            cost += b ;
            a -= 1 ;
            me[cnt] = cost ;
        }
        for(int j=k-1;j>=0;j--){// 枚举k
            for(int p=1;p<=k-j;p++){//枚举这个矩阵得分
                dp[j+p] = min(dp[j+p],dp[j]+me[p]) ;
            }
        }
    }
    cout << (dp[k]==INF ? -1 : dp[k]) << endl; 
    return ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

### Codeforces Round 1013 Div. 3 部分题目解析 #### A. Good Numbers 此题的核心在于统计输入数据中特定数字的频率并验证其是否满足条件。具体实现方法如下: 程序通过 `map` 数据结构存储每个数字出现的次数,并逐一检查这些计数值是否达到指定的要求。 ```cpp void solve() { int n, m, k; cin >> n; map<int, int> mp; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { mp[a[i]]++; if (mp[2] >= 2 && mp[0] >= 3 && mp[1] >= 1 && mp[3] >= 1 && mp[5] >= 1) { cout << i << endl; return; } } cout << 0 << endl; } ``` 上述代码逻辑清晰地展示了如何利用哈希表来解决该问题[^1]。 --- #### B. Team Training 对于本题,目标是从给定字符串中删除某些字符以形成子串 `"2020"`。解决方案采用枚举的方式尝试不同的分割位置组合,从而快速判断是否存在合法方案。 以下是完整的 C++ 实现代码片段: ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define endl &#39;\n&#39; void solve() { int n; string a; cin >> n >> a; for (int i = 0; i <= 4; i++) { if (a.substr(0, i) + a.substr(n - (4 - i), 4 - i) == "2020") { puts("YES"); return; } } puts("NO"); } signed main() { int _ = 1; cin >> _; while (_--) solve(); } ``` 这段代码实现了对所有可能情况的有效覆盖,并提供了高效的解答方式。 --- #### 更多题目概述 虽然未提供其他题目的详细描述,但可以根据比赛惯例推测其余部分涉及的内容范围广泛,涵盖了动态规划、贪心算法以及图论等领域知识点。有兴趣者可访问官方平台获取完整版题解资料[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值