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;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值