1.Brain's Photos
Description
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.
Input
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'.
Output
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.
Sample Input
2 2
C M
Y Y
#Color
3 2
W W
W W
B B
#Black&White
1 1
W
#Black&White
题目意思:只有全部是黑色白色和灰色才要打印#Black&White,一旦含有其他颜色就要打印#Color
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int MAX =1e6+10; 6 int a[MAX]; 7 int main() 8 { 9 int n,m,flag,i,j; 10 char c; 11 scanf("%d%d",&n,&m); 12 flag=0; 13 getchar(); 14 for(i=0;i<n;i++) 15 { 16 for(j=0;j<m;j++) 17 { 18 scanf("%c",&c); 19 getchar(); 20 if(c=='B'||c=='W'||c=='G') 21 { 22 ; 23 } 24 else 25 { 26 flag=1; 27 } 28 } 29 30 } 31 if(flag==0) 32 { 33 printf("#Black&White "); 34 } 35 else 36 { 37 printf("#Color "); 38 } 39 return 0; 40 }
2.Interesting drink
Description
Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal toxi coins.
Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of "Beecola".
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy's favourite drink.
The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop.
The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink.
Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.
Output
Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.
Sample Input
5
3 10 8 6 11
4
1
10
3
11
0
4
1
5
Hint
On the first day, Vasiliy won't be able to buy a drink in any of the shops.
On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.
On the third day, Vasiliy can buy a drink only in the shop number 1.
Finally, on the last day Vasiliy can buy a drink in any shop.
题目意思:同一种饮料在不同的商店里面的价格是不一样的,主人公每天能够花一些钱,问他能够买到一瓶饮料的商店数量。
解题思路:先排序,之后二分查找。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int MAX =1e6+10; 6 int a[MAX]; 7 int main() 8 { 9 int n,i,ans,m,p; 10 int l,r,mid; 11 scanf("%d",&n); 12 for(i=1; i<=n; i++) 13 { 14 scanf("%d",&a[i]); 15 } 16 ans=0; 17 sort(a+1,a+n+1); 18 scanf("%d",&m); 19 while(m--) 20 { 21 scanf("%d",&p); 22 if(p<a[1]) 23 { 24 ans=0; 25 } 26 else if(p>=a[n]) 27 { 28 ans=n; 29 } 30 else 31 { 32 l=1; 33 r=n; 34 while(l<=r) 35 { 36 mid=(l+r)/2; 37 if(a[mid]<=p) 38 { 39 ans=mid; 40 l=mid+1; 41 } 42 else 43 { 44 r=mid-1; 45 } 46 } 47 } 48 printf("%d ",ans); 49 } 50 return 0; 51 }
其实在STL库中由专门的二分查找函数
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX =1e6+10; int a[MAX]; int main() { int n,i,ans,m,p; int l,r,mid; scanf("%d",&n); for(i=0; i<n; i++) { scanf("%d",&a[i]); } ans=0; sort(a,a+n); scanf("%d",&m); while(m--) { scanf("%d",&p); ans=upper_bound(a,a+n,p)-a; printf("%d ",ans); } return 0; }
这道题其实还可以使用前缀和来记录
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX =1e5+10; int ans[MAX]; int a[MAX]; int main() { int n,i,m,p,x,maxs; maxs=0; scanf("%d",&n); for(i=1; i<=n; i++) { scanf("%d",&x); a[x]++; if(x>maxs) { maxs=x; } } ans[0]=0; for(i=1;i<=maxs;i++) { ans[i]=ans[i-1]+a[i]; } scanf("%d",&p); while(p--) { scanf("%d",&m); if(m<=maxs) { printf("%d ",ans[m]); } else { printf("%d ",ans[maxs]); } } return 0; }
3.Bakery
Description
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.
Input
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.
Output
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.
Sample Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
3
3 1 1
1 2 3
3
-1
题目意思:
n个城市,m条双向路,n个城市中有k个城市中有仓库。现在要在一个没有仓库的城市开一家面包店,使得该面包店能够至少到达一个仓库,且到达该仓库的距离尽可能小,问最小距离为多少,若没有城市适合开设面包店,则输出"-1"
解题思路:
当时做这道题的时候主要是翻译花费的很多时间,不过最后好歹看懂了,原来是一道水题。既然是图的题首先需要考虑的是怎么样去存图,幸好数据量不大,我使用的是邻接矩阵,按照题目意思转换过来其实我们是要找一条一头是面包店一头是仓库的边中的最短边。那么我们只需要对点进行标记一下就行了,仓库是1,面包店是-1,我们只需要遍历所有的边找到最小的那一条就可以了。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define INF 0x3f3f3f3f 5 using namespace std; 6 const int MAX =1e5+10; 7 struct node 8 { 9 int x; 10 int y; 11 int cost; 12 }a[MAX]; 13 int vis[MAX]; 14 int main() 15 { 16 int n,m,k,i,j,p,minx; 17 minx=INF; 18 memset(vis,-1,sizeof(vis)); 19 scanf("%d%d%d",&n,&m,&k); 20 for(i=0;i<m;i++) 21 { 22 scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].cost); 23 } 24 for(i=0;i<k;i++) 25 { 26 scanf("%d",&p); 27 vis[p]=1; 28 } 29 for(i=0;i<m;i++) 30 { 31 if((vis[a[i].x]==-1&&vis[a[i].y]==1)||((vis[a[i].y]==-1)&&vis[a[i].x]==1)) 32 { 33 minx=min(minx,a[i].cost); 34 } 35 } 36 if(minx==INF) 37 { 38 minx=-1; 39 } 40 printf("%d ",minx); 41 return 0; 42 }