Codeforces Round #674 (Div. 3)题解

Codeforces Round #674 (Div. 3)

A. Floor Number

题意:该公寓第一层有两个房间,每上一层增加x个房间,给出一个n,求n在第几层。
题解:整除一下就可以

#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int t,n,x;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>x;
        if(n<=2) cout<<1<<endl;
        else{
            int ans = n-2;
            int cnt = (ans+x-1)/x;
            cout<<cnt+1<<endl;
        }
    }
	return 0;
}

B. Symmetric Matrix

题意:给出n个2×2的矩阵,问能不能拼成m×m的对称矩阵,矩阵可以使用无数次
题解:若m为奇数肯定不能,m为偶数时,如果有一个矩阵是对称矩阵,那么都用那一块矩阵也能拼成一个对称矩阵

#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int t,n,m,a,b,c,d;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>m;
        int f = 0;
        for(int i = 1;i <= n; i++){
            cin>>a>>b;
            cin>>c>>d;
            if(b == c) f = 1;
        }
        if(m % 2) f = 0;
        if( f ) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
	return 0;
}

C. Increase and Copy

题意:首先给出一个含有一个元素1的数组,有两种操作:1.选定数组中的一个数字将其+1, 2.选定数组中的一个数字复制一个在数组的末端,输入一个和,问最少几次操作后能得到这个和。
题解:通过观察发现输入与输出是有规律的,与和的sqrt有关,需要特判一下4,9,16这些平方数

#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int t,dp[maxn][5],sum;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        cin>>sum;
        if(sum == 1) cout<<0<<endl;
        else if(sum == 2) cout<<1<<endl;
        else if(sum == 3||sum == 4) cout<<2<<endl;
        else{
            double x = sqrt(sum);
            if (floor(x) * floor(x) == sum) cout<<2 * floor(x) -2<<endl;
            else if(floor(x) == round (x)) cout<<2 * floor(x)-1<<endl;
            else cout<<2 * floor(x)<<endl;
        }
    }
	return 0;
}

D. Non-zero Segments

题意:给出一串不含0的数组,问最少要添加多少个数字使该数组的任意子序列和不含0。
题解:计算该数组的前缀和,前缀和为0 ,计数器加1,前缀和出现过两次一样的也加1,计数器每次更新后,前缀和更新为当前的数字。1 ,-1, 1比如这个序列,前两个数字和为0,所以新增的数字只能在1和-1之间,所以当前和要更新为从-1开始。如果从第二个1开始,那-1,1之间就少增加了一个数字。

#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
long long n,a[maxn],ans,sum;
map<long long,int>m;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    m[0] = 1;
    for(int i = 1;i <= n; i++){
        cin>>a[i];
        sum += a[i];
        if(m[sum] == 1){
            m.clear();
            ans++;
            m[0] = 1;
            sum = a[i];
        }
        m[sum] = 1;
    }
    cout<<ans<<endl;
	return 0;
}

E. Rock, Paper, Scissors

题意:两个人在玩剪刀石头布,每个人出剪刀、石头、布的次数已经给出,问第一个人最少和最多赢多少次。
题解:想让第一个人每次都赢,那必须第一个人出剪刀,第二个人出布,第一个人出石头,第二个人出剪刀,第一个人出布,第二个人出石头。想让第一个人赢的少,比如第一个人出剪刀,第二个人出石头赢他,如果石头不够,那出剪刀和他平局,如果还不够才出布,尽量不能让第一个人赢,这一段代码我写的挺无脑。

#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
int n,a1,a2,a3,b1,b2,b3,maxx,minn;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    cin>>a1>>a2>>a3;
    cin>>b1>>b2>>b3;
    maxx = min(a1,b2) + min(a2,b3) + min(a3,b1);
    if(a1 < b3){
        a1 = 0;
        b3 = b3 - a1;
    }
    else{
        a1 = a1 - b3;
        if(a1 > b1){
            minn += a1 - b1;
            a1 = 0;
            b1 = 0;
            b2 = b2 -a1;
        }
        else{
            b1 = b1 - a1;
            a1 = 0;
        }
    }
    if(a2 < b1){
        a2 = 0;
        b1 = b1 - a2;
    }
    else{
        a2 = a2 - b1;
        if(a2 > b2){
            minn += a2 - b2;
            a2 = 0;
            b2 = 0;
            b3 = b3 -a2;
        }
        else{
            b2 = b2 - a2;
            a2 = 0;
        }
    }
    if(a3 < b2){
        a3 = 0;
        b2 = b2 - a3;
    }
    else{
        a3 = a3 - b2;
        if(a3 > b3){
            minn += a3 - b3;
        }
    }
    cout<<minn<<" "<<maxx<<endl;
	return 0;
}

F. Number of Subsequences

题意:给出一个只含有a、b、c和?的字符串,?可以是a,b,c中的任意一个,问总共有多少个abc的子串。
题解:首先先把问题简化,当没有?时,我们容易求出abc的子串个数,一个问号有3种可能,那么k个问号就有3^k种可能,所以遇到第一个问号,一条字符串就变成了三条,遇到第二个,三条字符串就变成了九条…

#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
#define ll long long
const ll mod = 1e9 + 7;
ll a[5];
int n;
string s;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    cin>>s;
    ll val = 1;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == 'a'){
            a[0] += val;
            a[0] %= mod;
        }
        else if (s[i] == 'b'){
            a[1] += a[0];
            a[1] %= mod;
        } 
        else if (s[i] == 'c'){
            a[2] += a[1];
            a[2] %= mod;
        }
        else{
            ll a0 = a[0] , a1 = a[1] ;
            a[0] = (a[0] * 3)%mod;
            a[1] = (a[1] * 3)%mod;
            a[2] = (a[2] * 3)%mod;
            a[2] = (a[2] + a1)%mod;
            a[1] = (a[1] + a0)%mod;
            a[0] = (a[0] + val)%mod;
            val = val*3%mod;
        }
    }
    cout<<a[2]<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值