Codeforces Round #368 (Div. 2)
A. Brain's Photos
题意: 给你一个n*m的字符矩阵,如果字符中出现'C', 'M', 'Y'这三个中的任何一个就输出"#Color";否则输出"#Black&White".
代码如下:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <fstream>
5 #include <ctime>
6 #include <cmath>
7 #include <cstdlib>
8 #include <algorithm>
9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24 #define inf 0x7fffffffffffffff
25 #define maxf 0x7fffffff
26
27 const int maxn = 105;
28 char s[maxn][maxn];
29
30 int main()
31 {
32 int n, m;
33 while(scanf("%d%d", &n, &m)==2)
34 {
35 getchar();
36 int flag = 0;
37 for(int i = 0; i < n; i++ )
38 gets(s[i]);
39 for(int i = 0; i < n; i++ )
40 for(int j = 0; s[i][j]!='\0'; j++)
41 if(s[i][j]=='C'||s[i][j]=='M'||s[i][j]=='Y')
42 flag = 1;
43 if(flag==0)
44 printf("#Black&White\n");
45 else
46 printf("#Color\n");
47 }
48 return 0;
49 }
B. Bakery
题意: 给你三个数n,m,k分别代表城市数、道路数、面粉店数,剩下的n-k为面包店数;m条道路中每条道路都包含三个数u,v,l表示城市u到城市v的距离(每个城市中要么有面粉店要有面包店);求某个面粉店到某个面包店的最短距离.
分析:标记面粉店,再遍历m条道路,如果城市u和城市v中不都是面粉店或面包店则将它们之间的最短距离保存.
代码如下:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <fstream>
5 #include <ctime>
6 #include <cmath>
7 #include <cstdlib>
8 #include <algorithm>
9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24 #define inf 0x7fffffffffffffff
25 #define maxf 0x7fffffff
26
27 const int maxn = 100005;
28 int vis[maxn];
29
30 struct Node{
31 int u;
32 int v;
33 int l;
34 // bool operator < (const Node &x) const{
35 // return l < x.l;
36 // }
37 }node[maxn];
38
39
40 int check(int x, int y)
41 {
42 if(vis[x] != vis[y])
43 return 1;
44 return 0;
45 }
46
47 int main()
48 {
49 int n, m, k;
50 while(scanf("%d%d%d", &n, &m, &k)==3)
51 {
52 memset(vis, 0, sizeof(vis));
53 for(int i = 0; i < m; i++ )
54 scanf("%d%d%d", &node[i].u, &node[i].v, &node[i].l);
55 for(int i = 0; i < k; i++ )
56 {
57 int x;
58 scanf("%d", &x);
59 vis[x] = 1;
60 }
61 int ans = INF;
62 for(int i = 0; i < m; i++ )
63 {
64 if(check(node[i].u,node[i].v))
65 ans = min(ans, node[i].l);
66 }
67 if(ans == INF)
68 printf("-1\n");
69 else
70 printf("%d\n", ans);
71 }
72
73 return 0;
74 }
C. Pythagorean Triples
题意: 给你一条直角边,输出另外一条直角边和斜边,组成一个直角三角形.
分析:由勾股定理可知: a2 + b2 = c2则a2 = c2 - b2 = (c-b)(c+b);如果最短直角边小于或等于2时,不存在这样的直角三角形;否则:当a%2为1即a为奇数时,令c-b =1,c+b=a2则c = (a2+1)/2, b = (a2+1)/2-1;当a为偶数时,令c-b = 2,c+b=a2/2则c = a2/4+1, b = a2/4-1.
代码如下:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <fstream>
5 #include <ctime>
6 #include <cmath>
7 #include <cstdlib>
8 #include <algorithm>
9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24 #define inf 0x7fffffffffffffff
25 #define maxf 0x7fffffff
26
27 int main()
28 {
29 LL a;
30 while(scanf("%I64d", &a)==1)
31 {
32 LL b, c;
33 if(a <= 2)
34 printf("-1\n");
35 else if(a%2==0)
36 {
37 b = (a*a)/4 - 1;
38 c = (a*a)/4 + 1;
39 printf("%I64d %I64d\n", b,c);
40 }
41 else
42 {
43 b = (a*a+1)/2-1;
44 c = b+1;
45 printf("%I64d %I64d\n", b, c);
46 }
47 }
48 return 0;
49 }