codeforces-707【思维】【图论】【勾股数】
来源:互联网 发布:美橙互联域名待开通 编辑:程序博客网 时间:2024/04/30 01:00
题目链接:点击打开链接
Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.
As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).
Brain took a lot of colourful pictures on colored and black-and-white film. Then he developed and translated it into a digital form. But now, color and black-and-white photos are in one folder, and to sort them, one needs to spend more than one hour!
As soon as Brain is a photographer not programmer now, he asks you to help him determine for a single photo whether it is colored or black-and-white.
Photo can be represented as a matrix sized n × m, and each element of the matrix stores a symbol indicating corresponding pixel color. There are only 6 colors:
- 'C' (cyan)
- 'M' (magenta)
- 'Y' (yellow)
- 'W' (white)
- 'G' (grey)
- 'B' (black)
The photo is considered black-and-white if it has only white, black and grey pixels in it. If there are any of cyan, magenta or yellow pixels in the photo then it is considered colored.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of photo pixel matrix rows and columns respectively.
Then n lines describing matrix rows follow. Each of them contains m space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', 'M', 'Y', 'W', 'G' or 'B'.
Print the "#Black&White" (without quotes), if the photo is black-and-white and "#Color" (without quotes), if it is colored, in the only line.
2 2C MY Y
#Color
3 2W WW WB B
#Black&White
1 1W
#Black&White
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int n,m;char a[10010];int main(){while(~scanf("%d %d",&n,&m)){bool flag=0;for(int i=0;i<n*m;i++){cin>>a[i];if(a[i]=='C'||a[i]=='M'||a[i]=='Y'){flag=1;}}if(flag)puts("#Color");elseputs("#Black&White");}return 0;}
题目链接:点击打开链接
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai ≠ b for every 1 ≤ i ≤ k) and choose a storage in some city s (s = aj for some 1 ≤ j ≤ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤ n) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 ≤ u, v ≤ n, 1 ≤ l ≤ 109, u ≠ v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n) — the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
5 4 21 2 51 2 32 3 41 4 101 5
3
3 1 11 2 33
-1
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
题意:给你 n 个城市,m 条无向边,,k 个有卖面粉的城市,你要在没有卖面粉的地方建一家面包店,每条无向边有一个权值,求从面包店到一个卖面粉的地方花费的最小权值是多少;若不存在到一个卖面粉的城市路,就输出 -1
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int INF=0x3f3f3f3f;const int MAX=1e5+10;int n,m,k;bool vis[MAX];int u[MAX],v[MAX],l[MAX];int main(){while(~scanf("%d %d %d",&n,&m,&k)){memset(vis,0,sizeof(vis));for(int i=1;i<=m;i++)scanf("%d %d %d",&u[i],&v[i],&l[i]);int a;while(k--){scanf("%d",&a);vis[a]=1; // 标记有卖面粉的城市 }int ans=INF;for(int i=1;i<=m;i++){if(vis[u[i]]!=vis[v[i]]){ans=min(ans,l[i]);}}if(ans==INF)puts("-1");elseprintf("%d\n",ans);}return 0;}
题目链接:点击打开链接
题解:上初中的时候也没听老师说过勾股数有这个性质啊;还是网上看了别人总结的才知道。注意:答案是不唯一的,如输入 9,输出 12,15正确;输出 40,41也正确。
经过分析,可以发现当 a > 1 并且 a 为奇数的时候,把 a 分解成一半 n = ( a - 1 ) / 2;则 b = 2 * n * ( n + 1 ),c = 2 * n * ( n + 1 ) + 1;当 a > 2 并且 a 为偶数的时候,把 a 分解成一半 n = a / 2;则 b = n * n - 1,c = n * n + 1;
#include<cstdio>#include<algorithm>#include<cstring>#define Int __int64using namespace std;Int n;int main(){while(~scanf("%I64d",&n)){if(n<=2)puts("-1");else{Int m=n>>1;if(n&1)printf("%I64d %I64d\n",2*m*(m+1),2*m*(m+1)+1);elseprintf("%I64d %I64d\n",m*m-1,m*m+1);}}return 0;}
- codeforces-707【思维】【图论】【勾股数】
- 思维 CodeForces
- Codeforces Problem 707B Bakery(思维题)
- CodeForces 707B Bakery(思维+贪心)
- codeforces 232A Cycles 图论 贪心 思维
- [思维]CodeForces 135B
- Codeforces 611F 思维
- codeforces-660【思维】
- codeforces-554【思维】
- codeforces-632【思维】
- codeforces-597【思维】
- 【codeforces B + 思维】
- 【codeforces 723C】 + 思维
- codeforces-233【B思维】
- codeforces 734 (思维 暴力)
- codeforces C. Queue (思维)
- codeforces-745【思维】
- codeforces-746【思维】【模拟】
- Java虚拟机学习 - 类加载器(ClassLoader)
- xml的实例--学生管理系统
- VBA IE对象的操作方法
- C++ 构造/析构函数中调用虚函数的问题
- Java回调机制
- codeforces-707【思维】【图论】【勾股数】
- Hibernate知识总结(转载)
- SSH 多关键字查询
- kubernetes 高可用部署 HA
- UI控件分类
- 383. Ransom Note
- Dojo初探之4:dojo的event(鼠标/键盘)事件绑定操作(基于dojo1.11.2版本)
- 地位尴尬的北斗导航
- 文件类型判断宏与文件类型常量(怎么判断文件类型的两种方法)