Codeforces Round 964 (Div. 4) C++题解(A-F)

比赛地址 : 

Dashboard - Codeforces Round 964 (Div. 4) - Codeforces

模拟即可

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

inline void solve(){
	int n ; cin >> n ;
	int res  = 0;
	while(n){
		res += n%10 ;
		n/=10 ;
	}	
	cout << res << endl; 
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

B

假设输入a ,  b , c , d ;

只有两种匹配方式(不考虑顺序) : 

1 . ac   ,bd

2 . ad , bc

只要满足至少平1胜1 , 那么就会赢下两局 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
#define pb push_back
const int N = 3e5+10;
#define endl '\n'

inline void solve(){
	int a , b , c, d ; cin >> a >> b >> c >> d ;
	int ans = 0 ;
	if(a>c&&b>d) ans +=2 ;
	if(a==c&&b>d) ans +=2 ;
	if(a>c&&b==d) ans +=2 ;
	if(a>d&&b>c) ans +=2 ;
	if(a==d&&b>c) ans += 2 ;
	if(a>d&&b==c) ans += 2 ;
	cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

C

模拟

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
#define int long long 
#define pb push_back
const int N = 2e5+10;
#define endl '\n'

int l[N]  , r[N] ;

inline void solve(){
	int n , s , m ; cin >> n >> s >> m ;
	for(int i=1;i<=n;i++) cin >> l[i] >> r[i] ;
	if(l[1]>=s){
		cout << "YES" << endl ;
		return  ;
	}
	for(int i=2;i<=n;i++){
		if(l[i]-r[i-1]>=s){
			cout << "YES" << endl ;
			return ;
		}
	}
	if(m-r[n]>=s){
		cout << "YES" << endl ;
		return ; 
	}
	cout << "NO" << endl ;
	return ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

D

贪心  : 从左往右匹配,设置t的指针 L (并初始化为0) ,遍历s ,  用'?'尽量匹配前面的字符 ;

非常典的题 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
#define pb push_back
const int N = 2e5+10;
#define endl '\n'

inline void solve(){
	string s , t ; cin >> s >> t ;
	int l = 0 , n = s.size() , m = t.size() ;
	for(int i=0;i<n;i++){
		if(s[i]=='?'){
			s[i]=t[l] ;
			l++ ;
		}else if(s[i]==t[l]){
			l++ ;
		}
		if(l==m) break ;
	}
	if(l==m){
		cout << "YES" << endl ;
		for(int i=0;i<n;i++) if(s[i]=='?') s[i]='a' ;
		cout << s << endl ;
	}else{
		cout << "NO" << endl ;
	}
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

E

贪心 + 前缀和 ;

令f(x)为让任意x每次/3最后得0得操作次数 ;

贪心 : 首先将 L 变为0 , 那么要从[l+1,r]取一个数每次*3 , 才能使 l 每次 /3 为0 ,这个选数是任意的 ;

先将 L 变为0 : 

        1 . L最小 , 操作次数最小

        2 . 有1个0之后 , 可以让0充当x每次*3 , 其他数每次/3变为0 , 这样能最小化*3使变量增大的影响 ;

前缀和 : 

       这里用前缀和记录[l,r]区间f(i)得累加 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
#define int long long 
#define pb push_back
const int N = 2e5+10;
#define endl '\n'

LL a[N] , b[N] ;

//int f(int x){
//	int t = 0 ;
//	while(x){
//		x/=3;
//		t++;
//	}
//	return t ;
//}

int f(int x){
	if(x==1) return 1 ;
	int i = 1 , t = 0 ;
	while(x>i){
		t++;
		i*=3 ;
	}
	if(x==i) t++ ;
	return t ;
}

void init(){
	int MAX = 2e5  ;
	for(int i=1;i<=MAX;i++) a[i] = f(i) ;
	for(int i=2;i<=MAX;i++) b[i] = b[i-1] + a[i] ;
}

inline void solve(){
	int l , r ; cin >> l >> r ;
	int ans = 0 , p = l+1 , x = l ;
	ans += 2*f(l) + f(l+1) ;
	if(r-l>=2) {
		ans += b[r] - b[l+1] ;
	}
	// for(int i=1;i<=3;i++) cout << a[i] << " ";
	//cout << endl ;
	cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    init() ;
    while(_ --) solve();
    return 0;
}

F

组合数 : 

设输入数组中1的数量为x , 0的数量为y ;

题目要求也就是选出来得k个数中 , 1的个数 > 0的个数 ;

那么枚举子序列中1的个数(直接从k/2+1开始) 为 i , 那么0的个数也就是k-i个;

那么(i,k-i)的方案数也就是C(x,i) * C(y,k-i)  , (即 : x中选i个1 , y中选k-i个0 , 然后累加即可) ;

这里组合数采用快速幂 + 乘法逆元来求 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL ;
#define int long long 
#define pb push_back
const int N = 2e5 + 10 ;
const int P = 1e9 + 7 ;
#define endl '\n'

int f[N], g[N];

int qpow(int a, int b){
  int res = 1;
  while(b){
    if(b & 1) res=res*a%P;
    a = a*a%P;
    b >>= 1;
  }
  return res;
}
void init(){
  f[0] = g[0] = 1;
  for(int i=1; i<N; i++){
    f[i] = f[i-1]*i%P;
    g[i] = g[i-1]*qpow(i,P-2)%P;
  }  
}
int getC(int n, int m){
  return f[n]*g[m]%P*g[n-m]%P;
}


inline void solve(){
	int n , k ; cin >> n >> k ;
	int x = 0 , y = 0 , p = 0 ;
	for(int i=1;i<=n;i++){
		cin >> p ; x += p ;
	}
	y = n - x ;
	// 奇数 > 偶数的数量即可
	LL ans = 0 ; 
	for(int i=k/2+1;i<=k-1;i++){
		if(x>=i){
//			if(y>=k-i) ans += Lucas(x,i,mod)*Lucas(y,k-i,mod)%mod ;
			if(y>=k-i) ans += getC(x,i)*getC(y,k-i)%P ;
		}else{
			break ;
		}
	}
	if(x>=k){
		ans += getC(x,k) % P ;
	}
	cout << ans % P << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    init() ;
    while(_ --) solve();
    return 0;
}

G1  G2

交互题不会 , 也不想写 ;

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值