Codeforces Round #395 (Div. 2)

今天自己模拟了一套题,只写出两道来,第三道时间到了过了几分钟才写出来,啊,太菜了。

A. Taymyr is calling you

水题,问你在z范围内  两个序列  n,2*n,3*n......  和 m,2*m,3*m.....有多少个是一样的。

#include<bits/stdc++.h>
using namespace std;
int n,m,z;
int main()
{
    cin>>n>>m>>z;
    int gcd=__gcd(n,m);
    int now=n*m/gcd;
    int ans=0;
    int res=now;
    while(res<=z)
    {
        ans++;
        res+=now;
    }
    cout<<ans<<endl;
    return 0;
}

B. Timofey and cubes

题目大意:给你n个数,进行(n/2)次操作,每次操作将制定区间里的数交换,给你交换后的

让你求原来的。

思路:水题,奇偶讨论。

#include<bits/stdc++.h>
const int N=2*1e5+5;
using namespace std;
int a[N];
int n;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int mid=n/2;
    int i=1,j=n;
    bool flag=true;
    while(i<=j)
    {
        if(flag)
        {
            int t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
        flag=!flag;
        i++,j--;
    }
    for(int i=1;i<=n;i++) printf("%d%c",a[i],i==n? ‘\n‘:‘ ‘);
    return 0;
}

C. Timofey and a tree

题目大意:给你一颗树,每个节点都有一个颜色,让你挑选一个节点当做整棵树的根,满足所有

子树中节点的颜色都是一样的。如果没有输出NO 否则输入YES 且输出选择的节点。

思路:刚开始的思路是我觉得这棵树中的颜色不能超过 3 种,后来一想这种想法好智障啊,明显

不对,后来枚举树根每个都dfs一下超时,结束后想我随便找一个点dfs找到两个相邻的颜色不同的

点,那么这两个点中间肯定有一个要当做树根的,否则不成立。这样只要dfs两次就行了。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
vector<int> e[N];
int c[N],item1,item2,n;
map<int,bool> mp;
bool flag;
void dfs1(int u,int pre)
{
    //printf("%d\n",u);
    if(pre!=-1 && c[u]!=c[pre])
    {
        item1=u;
        item2=pre;
        return;
    }
    for(int i=0;i<e[u].size();i++)
    {
        int to=e[u][i];
        if(to!=pre) dfs1(to,u);
        if(item1!=-1) return;
    }
}
void dfs(int u,int pre,int item)
{
   // printf("%d %d %d**\n",u,pre,item);
    if(pre!=item && pre!=-1 && c[u]!=c[pre])
    {
        flag=false;
        return;
    }
    for(int i=0;i<e[u].size();i++)
    {
        int to=e[u][i];
        if(to!=pre) dfs(to,u,item);
        if(!flag) return;
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int f,t;
        scanf("%d%d",&f,&t);
        e[f].push_back(t);
        e[t].push_back(f);
    }
    int cnt=0;
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    item1=-1,item2=-1;
    dfs1(1,-1);
    if(item1==-1)
    {
        puts("YES");
        puts("1");
        return 0;
    }
    flag=true;
    dfs(item1,-1,item1);
    if(flag)
    {
        puts("YES");
        printf("%d\n",item1);
        return 0;
    }
    flag=true;
    dfs(item2,-1,item2);
    if(flag)
    {
        puts("YES");
        printf("%d\n",item2);
        return 0;
    }
    puts("NO");
    return 0;
}

D. Timofey and rectangles

题目大意:给你n个矩形,且矩形的边长为奇数,任意两个矩形都不相交,最多相邻,现在让你给这些矩形上色,

一种4种颜色,相邻的矩形颜色不能相等,问你有没有这样的方案,如果有则输出。

思路:赤裸裸的脑洞题,专门碾压我这种低智商的,这题最关键的是矩形的边长是奇数,虽然我知道,但是

我还是不会写QAQ。

首先,四个矩形不可能两两互相相邻,所以结论一定是YES

然后,我们考虑矩形的左下角的顶点,如果 x 和 y 的值都为奇数,因为边长是奇数,所以这个矩形不可能和

另一个左下角坐标都为奇数的矩形相邻。所以我们给这类矩形染色 1  。

其他三种同理。

#include<bits/stdc++.h>
using namespace std;
const int N=5*1e5+5;
int n,vis[N],m=1e9;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if((x1+m)%2 && (y1+m)%2) vis[i]=1;
        else if((x1+m)%2) vis[i]=2;
        else if((y1+m)%2) vis[i]=3;
        else vis[i]=4;
    }
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++) printf("%d\n",vis[i]);
    return 0;
}

时间: 2024-07-30 08:21:41

Codeforces Round #395 (Div. 2)的相关文章

Codeforces Round #395 (Div. 2) C

题意 : 给出一颗树 每个点都有一个颜色 选一个点作为根节点 使它的子树各自纯色 我想到了缩点后check直径 当<=3的时候可能有解 12必定有解 3的时候需要check直径中点的组成点里是否有一个点连接了另外所有的点 如果有就是ans 没有就是no 这个方法是对了 不过比较麻烦..需要很多check 但是看div1的做法 有很棒的姿势用来解这个题 扫一下所有的边 如果两边的点颜色不一样 就增加一个连通分量数 这样可以很方便的算出有多少连通分量 同时记录这个点连着多少个其余的颜色(连通分量)

Codeforces Round #395 (Div. 2) D

Description One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, b

Codeforces Round #395 (Div. 2) 解题报告

年后恢复训练参加的第一场,表现的不是很好,必须要赶紧振作起来了啊. A.B题不再赘述. C题 不要被树的形式吓到,实际上不需要换根DFS,只需要看两顶点颜色不同的线段即可. 1 #include<stdio.h> 2 #include<bits/stdc++.h> 3 #include <iostream> 4 using namespace std; 5 typedef long long ll; 6 typedef unsigned long long ull; 7

【树形DP】Codeforces Round #395 (Div. 2) C. Timofey and a tree

先把1看作根. 预处理出f[i]表示以i为根的子树是什么颜色,如果是杂色的话,就是0. 然后从根节点开始转移,转移到某个子节点时,如果其子节点都是纯色,并且它上面的那一坨结点也是纯色,就输出解. 否则如果其上面的一坨是纯色,并且其子节点有且只有一个杂色的时候,就递归处理该子节点. #include<cstdio> #include<cstdlib> using namespace std; #define N 100050 int v[N<<1],first[N],ne

Codeforces Round #395 (Div. 2)(未完)

2.2.2017 9:35~11:35 A - Taymyr is calling you 直接模拟 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; typedef long long ll; const int N=1e4+5; inline int read()

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我