Educational Codeforces Round 56 (Rated for Div. 2) ABCD

题目链接https://codeforces.com/contest/1093

A. Dice Rolling

题意:

有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次,能使扔出的总和等于xi。

题解:

由于是special judge,模拟一下搞搞就行了= =

代码如下:

#include <bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin>>t;
    int n;
    while(t--){
        cin>>n;
        for(int i=2;i<=7;i++){
            if(n%i!=1){
                cout<<n/i+(n%i!=0)<<endl;
                break ;
            }
        }
    }
    return 0;
}

B. Letters Rearranging

题意:

给出一个字符串,现在你可以任意交换字符位置,然后输出一种非回文串的方案,如果没有这样一种方案,输出-1。

题解:

注意回文串的性质,那么我们首先判断一下所有字符是否相同,如若相同直接输出-1。否则排个序就好了~

代码如下:

#include <bits/stdc++.h>
using namespace std;
int t;
const int N = 1005;
char s[N];
int main(){
    cin>>t;
    while(t--){
        scanf("%s",s);
        int flag = 1;
        int len  =strlen(s);
        s[len]=s[0];
        for(int i=1;i<=len;i++){
            if(s[i]!=s[0]) flag=0;
        }
        if(flag) cout<<-1<<endl;
        else{
            sort(s,s+len);
            for(int i=0;i<len;i++) cout<<s[i];
            cout<<endl;
        }
    }
    return 0;
}

C. Mishka and the Last Exam

题意:

一共有n个数,现在给出b1,b2....bn/2,满足bi=ai+an-i+1。

现在要你构造出合法的a1....an,并且a1<=a2<=...<=an,保证输入有解。

题解:

对于b1=a1+an来说,我们让a1=0,a2=b1是最好的,这样可以让左右端点最大。

对于后面的每个bi,我们令d=bi-bi-1,那么如若d>0,我们可以想,假若a2,an-1不变,那么他们之和是小于b2的,所以我们就让左端点变大(贪心策略)。

对于d=0或者d<=0都可以同样的策略去想(因为题目保证输入有解)。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5+5;
int n;
ll a[N],b[N];
int main(){
    cin>>n;
    for(int i=1;i<=n/2;i++) cin>>b[i];
    a[1]=0;a[n]=b[1];
    for(int i=2;i<=n/2;i++){
        ll d = b[i]-b[i-1];
        if(d>=0) a[i]=d+a[i-1],a[n-i+1]=a[n-i+2];
        else a[i]=a[i-1],a[n-i+1]=a[n-i+2]+d;
    }
    for(int i=1;i<=n;i++) cout<<a[i]<<" ";
    return 0;
}

D. Beautiful Graph

题意:

给出一个图,有n个点,m条边,每个点都有权值1,2或3。现在要你给点权赋值,满足相邻的两个点和为奇数。问一共有多少情况,注意这个图不一定保证连通。

题解:

根据题意我们知道,相邻的两个点必然为一奇一偶,所以我们可以用二分图染色来判断是否给出的图合法,能够满足条件。

在二分图染色的过程中,记录一下两种颜色的个数a,b,那么最终的答案就是对于每个图的2^a+2^b再求和。

注意一下单独一个点可能有1,2,3三种情况。

代码如下:

#include <bits/stdc++.h>
#define MOD 998244353
using namespace std;
typedef long long ll;
const int N = 3e5+5;
int T;
int n,m,flag,num,sum;
vector <int> g[N];
int color[N];
void dfs(int u,int c){
    color[u]=c;sum++;
    if(color[u]==1) num++;
    if(flag) return ;
    for(auto v:g[u]){
        if(!color[v]) dfs(v,3-c);
        else if(color[v]==c){
            flag=1;
            return ;
        }
    }
    return ;
}
ll quick(ll a,int b){
    ll ans = 1;
    while(b){
        if(b&1) ans=(ans*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return ans ;
}
int main(){
    cin>>T;
    while(T--){
        scanf("%d%d",&n,&m);flag=0;
        for(int i=0;i<=n;i++) g[i].clear(),color[i]=0;
        for(int i=1;i<=m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);g[v].push_back(u);
        }
        int tot =0;
        ll ans =1;
        for(int i=1;i<=n;i++){
            if(!color[i] && !flag){
                num=0;sum=0;
                dfs(i,1);
                if(sum==1) ans=(ans*3)%MOD;
                else ans=(ans*(quick(2,num)+quick(2,sum-num))%MOD)%MOD;
            }
        }
        if(flag) printf("0\n");
        else printf("%I64d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/10134975.html

时间: 2024-08-29 22:54:12

Educational Codeforces Round 56 (Rated for Div. 2) ABCD的相关文章

Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))

题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一种操作是查询[l,r]中曼哈顿距离最大的两个点的最大曼哈顿距离. 思路: 对于曼哈顿距离,我们将其绝对值去掉会发现如下规律(以二维为例): 故这题我们可以用线段树来维护[l,r]中上述每种情况的最大值和最小值,用二进制来枚举xy的符号(1为正,0为负),最后答案是 每种情况中区间最大值-区间最小值

Educational Codeforces Round 56 (Rated for Div. 2)

涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<c

Educational Codeforces Round 49 (Rated for Div. 2) ABCD

A. Palindromic Twist You are given a string ss consisting of nn lowercase Latin letters. nn is even. For each position ii (1≤i≤n1≤i≤n) in string ss you are required to change the letter on this position either to the previous letter in alphabetic ord

Educational Codeforces Round 59 (Rated for Div. 2)(ABCD)

A. Digits Sequence Dividing 题意:给你一个数字串,只包含1-9,让你至少分成两段,使得每一段的数字大于前一段的数字: 解:对n特判,如果n为2,那么比较一下两个数字大小,如果n>2,那么就可以直接分成两部分,第一部分1个数字,剩下都为第二部分: #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e3+10; const int mod=9982

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w