1、
Brain's Photos
题目链接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 2 C M Y Y
#Color
3 2 W W W W B B
#Black&White
1 1 W
#Black&White
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#define ll long long
using namespace std;
int n,m;
string s[128];
int main(){
while(~scanf("%d%d",&n,&m)){
getchar();
map<char,int> mp;
for(int i=0;i<n;i++){
getline(cin,s[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<s[i].size();j++){
if(s[i][j] >= 'A' && s[i][j] <='Z'){
mp[ s[i][j] ]++;
}
}
}
if(mp['C'] == 0 && mp['M'] == 0 && mp['Y'] == 0 && (mp['B'] > 0 || mp['W'] > 0 || mp['G'] > 0)){
printf("#Black&White\n");
}
else
printf("#Color\n");
}
return 0;
}
2、
Bakery
题目链接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 ncities, print - 1 in the only line.
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
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#define ll long long
using namespace std;
const int maxn=1e6+10;
const int inf=1000000007;
int n,m,k;
struct node{
int v,l;
};
vector<node> u[2*maxn];
int a[maxn];
int vis[maxn];
int main(){
int t1,t2,t3;
scanf("%d%d%d",&n,&m,&k);
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++){
scanf("%d%d%d",&t1,&t2,&t3);
node tmp;
tmp.v=t2;
tmp.l=t3;
u[t1].push_back(tmp);
tmp.v=t1;
tmp.l=t3;
u[t2].push_back(tmp);
}
for(int i=0;i<k;i++){
scanf("%d",&a[i]);
vis[ a[i] ]=1;
}
int ans=inf;
for(int i=0;i<k;i++){
for(int j=0;j<u[a[i]].size();j++){
if( !vis[ u[a[i]][j].v ] ){
ans=min(ans,u[a[i]][j].l);
}
}
}
if(ans==inf) printf("-1\n");
else printf("%d\n",ans);
return 0;
}
3、
Pythagorean Triples
题目链接Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.
For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.
Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.
Katya had no problems with completing this task. Will you do the same?
The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.
Print two integers m and k (1 ≤ m, k ≤ 1018), such that n, m and k form a Pythagorean triple, in the only line.
In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them.
3
4 5
6
8 10
1
-1
17
144 145
67
2244 2245
Illustration for the first sample.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#define ll long long
using namespace std;
ll n,m,k;
int main(){
scanf("%I64d",&n);
ll nc2=n*n;
for(ll i=1;i<=nc2;i++){
if(nc2 % i == 0){
ll A=i,B=nc2/i;
if( (A+B)%2==0 && (B-A)%2==0 ){
k=(A+B)/2;
m=(B-A)/2;
break ;
}
}
}
if(m==0 || k==0) printf("-1\n");
else printf("%I64d %I64d\n",m,k);
return 0;
}
4、
President's Office
题目链接President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length.
The office-room plan can be viewed as a matrix with n rows and m columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.») stands for an empty cell.
The first line contains two separated by a space integer numbers n, m (1 ≤ n, m ≤ 100) — the length and the width of the office-room, and c character — the President's desk colour. The following n lines contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.
Print the only number — the amount of President's deputies.
3 4 R G.B. .RR. TTT.
2
3 3 Z ... .H. ..Z
0
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define ll long long
using namespace std;
struct node{
int x,y;
};
stack<node> st;
set< char > st1;
int n,m;
char s[2];
char mp[128][128];
int main(){
while(~scanf("%d%d%s",&n,&m,s)){
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mp[i][j] == s[0]){
node tmp;
tmp.x=i;
tmp.y=j;
st.push(tmp);
}
}
}
while(!st.empty()){
node tmp=st.top();
st.pop();
int x=tmp.x;
int y=tmp.y;
x--;
while(x >= 0){
if(mp[x][y] != s[0]){
if(mp[x][y] != '.')
st1.insert(mp[x][y]);
break ;
}
x--;
}
x=tmp.x;
x++;
while(x < n){
if(mp[x][y] != s[0]){
if(mp[x][y] != '.')
st1.insert(mp[x][y]);
break ;
}
x++;
}
x=tmp.x;
y=tmp.y;
y--;
while(y >= 0){
if(mp[x][y] != s[0]){
if(mp[x][y] != '.')
st1.insert(mp[x][y]);
break ;
}
y--;
}
y=tmp.y;
y++;
while(y < m){
if(mp[x][y] != s[0]){
if(mp[x][y] != '.')
st1.insert(mp[x][y]);
break ;
}
y++;
}
}
printf("%d\n",st1.size());
}
return 0;
}
5、
Alice, Bob and Chocolate
题目链接Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman.
How many bars each of the players will consume?
The first line contains one integer n (1 ≤ n ≤ 105) — the amount of bars on the table. The second line contains a sequence t1, t2, ..., tn (1 ≤ ti ≤ 1000), where ti is the time (in seconds) needed to consume the i-th bar (in the order from left to right).
Print two numbers a and b, where a is the amount of bars consumed by Alice, and b is the amount of bars consumed by Bob.
5 2 9 8 2 7
2 3
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#define ll long long
using namespace std;
const int maxn=1e5+10;
int n;
int a[maxn];
int main(){
scanf("%d",&n);
int sum=0;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
int left=0,right=n-1;
int flag=0;
int sumLef=a[0],sumRig=a[n-1];
while(left < right-1){
if(left == right){
flag=1;
break ;
}
if(sumLef < sumRig){
left++;
sumLef+=a[left];
}
else if(sumLef > sumRig){
right--;
sumRig+=a[right];
}
else{
left++;
right--;
sumLef+=a[left];
sumRig+=a[right];
}
/*printf("left=%d right=%d\n",left,right);
printf("lSum=%d rSum=%d\n",sumLef,sumRig);*/
}
if(left == right){
flag=1;
}
printf("%d %d\n",left+1,n-right-flag);
return 0;
}
6、
Subsegments
题目链接Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.
The first line contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n) — the number of array elements and the length of the segment.
Then follow n lines: the i-th one contains a single number ai ( - 109 ≤ ai ≤ 109).
Print n–k + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 … ai + k - 1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".
5 3 1 2 2 3 3
1 3 2
6 4 3 3 3 4 4 2
4 Nothing 3
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
int n,k;
int a[maxn];
map<int,int> mp;
set<int,std::greater<int> > st;
list<int> lt;
int main(){
while(~scanf("%d%d",&n,&k)){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<k-1;i++){
st.insert(a[i]);
mp[ a[i] ]++;
}
int cnt=0;
for(int i=k-1;i<n;i++){
st.insert(a[i]);
mp[ a[i] ]++;
int flag=0;
set<int>::iterator it;
for(it=st.begin();it!=st.end();it++){
if(mp[*it] == 1){
int tmp=*it;
lt.push_back(tmp);
flag=1;
break ;
}
}
if(!flag){
lt.push_back(-1);
}
mp[ a[cnt] ]--;
if( mp[ a[cnt] ] == 0 )
st.erase(a[cnt]);
cnt++;
}
list<int>::iterator iot;
for(iot=lt.begin();iot!=lt.end();iot++){
if(*iot == -1){
printf("Nothing\n");
}
else{
printf("%d\n",*iot);
}
}
}
return 0;
}