Codeforces Round #368 (Div. 2)

来源:互联网 发布:知乐百度网盘 编辑:程序博客网 时间:2024/04/30 16:54

因为太弱,所以做不了太难的题,水一波题解。
Codeforces Round #368 (Div. 2)

A
————————————————————————
题意:给你一个字符矩阵,代表一张照片,每个字符是一个像素点,代表颜色,如果里面的字符全是 ‘W’ (white)
‘G’ (grey) ‘B’ (black) 那么这就是一个黑白照片,如果混有了其他颜色:’C’ (cyan) ‘M’ (magenta) ‘Y’ (yellow) 那么这是一个彩色照片,黑白照片输出“#Black&White”,彩色照片输出“#Color”。

题解:我是在读入的时候就判断了-判断到彩色像素点就直接结束程序,这样可能读入都没完成,不知道这样可不可以被Hack。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;int main(){    char c;     int n, m;    cin >> n >> m;    for(int i = 1; i <= n; i ++)        for(int j = 1; j <= m; j ++){            c = getchar();            while(c < 'A' || c > 'Z')                c = getchar();            if(c == 'Y' || c == 'M' || c == 'C'){                cout << "#Color" << endl;                return 0;            }        }    cout << "#Black&White" << endl;    return 0;}

B
————————————————————————
题意:有一个n个城市m条双向道路的图,在n个城市中有k个是仓库,现在让你选择一个商店,这个商店不能和仓库在同一个城市,还要保证商店与最近的仓库之间的距离最小。

题解:从每个仓库开始扩展一条边,遇到的最近城市即答案。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int MAX_V = 100010;const int MAX_E = 100010; int V, E, K, tot = 0, ans = 0x3f3f3f3f; int first[100010], nxt[100010 << 1];bool vis[100010];struct edge{    int from, to, cost;}es[100010 << 1];void build(int ff, int tt, int dd){    es[++tot] = (edge){ff,tt,dd};    nxt[tot] = first[ff];    first[ff] = tot;}void dfs(int x){    for(int i = first[x]; i != -1; i = nxt[i])    {        int v = es[i].to;        if(v != x && !vis[v])            ans = min(ans, es[i].cost);        else continue;    }}int main(){    cin >> V >> E >> K;    memset(first,-1,sizeof(first));    for(int i = 1; i <= E; i ++)    {        int f, t, d;        scanf("%d%d%d",&f,&t,&d);        build(f,t,d);        build(t,f,d);    }    for(int i = 1; i <= K; i ++)    {        int s;        scanf("%d",&s);        vis[s] = 1;    }    for(int i = 1; i <= V; i ++)        if(vis[i])            dfs(i);    if(ans == 0x3f3f3f3f)        cout << "-1" << endl;    else cout << ans << endl;    return 0;}

C
————————————————————————
题意:给你一个直接边,求另外两条边。

题解:数论啊QAQ ,不过不是很难啦。强势整理:

假设一组勾股数为a,b,c; 输入a 求b,c

1.
当 a >= 1 && a&1 时;
令 n = a/2;
则b = 2n2+2n;
c = b+1 = 2n2+2n+1;
2.
当 a >= 4 && a%2 == 0 时;
令 n = a/2; ;
则b = n21;
c = n2+1;

注意特判一下 a < 4;

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;int main(){    long long n,a,b,c;    cin >> a;    if(a&1 && a > 1)    {        a --;        n = a/2;        b = 2*n*n + 2*n;        c = b + 1;        cout << b << " " << c << endl;     }    else if(a%2==0 && a >= 4)    {        n = a/2;        b = n*n-1;        c = n*n+1;        cout << b << " " << c << endl;    }    else cout << "-1" << endl;    return 0;}
0 0
原创粉丝点击