Codeforces Round #395 (Div. 2)

来源:互联网 发布:fanuc pmc编程手册 编辑:程序博客网 时间:2024/06/06 09:42

A - Taymyr is calling you(water)

题意:给三个数n,m,z。问你1-z之间,既是n的倍数又是m的倍数的数有几个。

#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 100 + 5;typedef long long LL;typedef pair<int, int>pii;int main(){    int n, m, z;    cin >> n >> m >> z;    set<int>s;    for(int i = 1; i * n <= z; i++)    {        s.insert(i * n);    }    int cnt = 0;    for(int i = 1; i * m <= z; i++)    {        if(s.find(i * m) != s.end())        {            cnt++;        }    }    cout << cnt << endl;    return 0;}


 

B - Timofey and cubes(water)

题意:给你n个数字,然后执行翻转,翻转区间:i-th to (n - i + 1)-th,翻转次数while i ≤ n - i + 1.给你翻转后的结果,输出翻转前的结果

思路:i-th to(n - i + 1)-th,i为奇数的时候翻转奇数次,i为偶数的时候翻转偶数次,前者相当于翻转一次,后者相当于没有翻转。

#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 1e5 + 5;typedef long long LL;typedef pair<int, int>pii;int a[2 * maxn], b[2 * maxn];int main(){    int n;    cin >> n;    for(int i = 1; i <= n; i++)    {        cin >> a[i];    }    for(int i = 1; i <= (n + 1) / 2; i++)    {        if(i % 2 == 1)        {            b[i] = a[n - i + 1];            b[n - i + 1] = a[i];        }        else        {            b[i] = a[i];            b[n - i + 1] = a[n - i + 1];        }    }    for(int i = 1; i <= n ; i++)    {cout << b[i];        if(i == n)            cout << endl;        else            cout << " ";    }    return 0;}


 

C - Timofey and a tree(思维?)

题意:给你一个n个结点树,第i个结点的颜色是color[i],问你能否找到一个结点,使得它的子树们,每一颗子树内的所有结点的颜色相同。

思路:这题不能缩点。。会wa的,缩点会改变图一些原有的特征orz。其实应该这样想,如果存在,那么是不是根结点连着一堆子树,他们有什么特征,每一颗子树内的所有节点的颜色相同对不对,那么整棵树,连接两个不同颜色的结点的边是不是都应该在根节点上。所以拿一个数组记录一下每个结点延展出去的边中,和它自身颜色不同的另一个结点个数。如果能匹配上的是不是就一定是根节点orz。

  

 

#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 1e5 + 5;typedef long long LL;typedef pair<int, int>pii;vector<int>G[maxn];vector<pii>edge;int col[maxn];int d[maxn];int main(){    int n;    scanf("%d", &n);    for(int i = 0; i < n - 1; i++)    {        int u, v;        scanf("%d%d", &u, &v);        G[u].push_back(v);        G[v].push_back(u);        edge.push_back({u, v});    }    for(int i = 1; i <= n; i++)    {        scanf("%d", &col[i]);    }    int tot = 0;    for(auto o : edge)    {        int u = o.first, v = o.second;        if(col[u] != col[v])        {            d[u]++, d[v]++;            tot++;        }    }    for(int i = 1; i <= n; i++)    {        if(d[i] == tot)        {            puts("YES");            cout << i << endl;            return 0;        }    }    puts("NO");    return 0;}


 

 

D - Timofey and rectangles(奇偶性+构造)

题意:给你n个矩形,在一个坐标系内。每个矩形的边长都是奇数且不会重叠,接触的均不同色,问你能不能用四种颜色填涂,满足条件。若存在输出涂色方案。

思路:首先四色定理发现,肯定有解。然后发现题干里加粗了“奇数”,两次还是三次。是一种提示吧,奇数,可以联想到奇偶性啊!!!谁有奇偶性???。坐标啊!!!!坐标只有四种情况x,y分别是奇数偶数,奇数奇数,偶数奇数,偶数偶数。然后每种情况对应一种颜色就好了。

#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 100 + 5;typedef long long LL;typedef pair<int, int>pii;#define judge(x) (x & 1)int main(){    int n;    scanf("%d", &n);    puts("YES");    for(int i = 0; i < n; i++)    {        int x, y, z, p;        scanf("%d%d%d%d", &x, &y, &z, &p);        x = abs(x), y = abs(y);        if(judge(x) && judge(y))    puts("1");        else if(judge(x) && !judge(y)) puts("2");        else if(!judge(x) && judge(y))   puts("3");        else if(!judge(x) && !judge(y))  puts("4");    }    return 0;}


 

E - Timofey and remoduling

 

思路:




0 0
原创粉丝点击