【LDU】2018个人PK赛#6

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.

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.

Example
Input
2 2
C M
Y Y
Output
#Color
Input
3 2
W W
W W
B B
Output
#Black&White
Input
1 1
W
Output
#Black&White

注意灰色也在黑白里面,重点在读题。

My ugly code
#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.

Input

The first line of the input contains three integers nm and k (1 ≤ n, m ≤ 1050 ≤ 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 uv and l (1 ≤ u, v ≤ n1 ≤ l ≤ 109u ≠ 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 ncities, print  - 1 in the only line.

Example
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note

Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.


题意:某某某准备开个面包店,然后存储面粉的地方必须和面包店分开吧,运输面粉需要成本,然后让你找出来最小花费。
思路:仔细想想,最小花费,面包店和仓库直接相连,两条路的绝对没一条路的小,然后暴力去找就好了

My ugly code
#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?

Input

The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.

Output

Print two integers m and k (1 ≤ m, k ≤ 1018), such that nm 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.

Example
Input
3
Output
4 5
Input
6
Output
8 10
Input
1
Output
-1
Input
17
Output
144 145
Input
67
Output
2244 2245
Note

Illustration for the first sample.

这个是数学,我说一下
一开始直接暴力,他有三个参数n,m,k,给你n,他们之间满足n*n+m*m=k*k,然后我暴力m,然后判断n*n+m*m的和能否开方,结果wa,然后想着换换位置吧,n*n=k*k-m*m,n*n=(k+m)*(k-m),然后令A=k+m,B=k-m;把k和m都用A和B表示出来,接下来去枚举A,找到B,去弄k,m;如果k和m有一个为0,那么就输出-1,这儿的原因就是k和m不可能为0,因为他们都是三角形的边长,为0的话就gg了。
总是感觉上面方法时间复杂度高,但是能ac,可能是cf测评机比较好吧。
再来优化一下,n*n的值其实就两种,奇数或者偶数,然后 奇数的时候我们设B=1,然后A就求出来了,偶数的时候设B=2,A也求出来了,哈哈。去试试
My ugly code
#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.

Input

The first line contains two separated by a space integer numbers nm (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.

Output

Print the only number — the amount of President's deputies.

Example
Input
3 4 R
G.B.
.RR.
TTT.
Output
2
Input
3 3 Z
...
.H.
..Z
Output
0

就是个读题吧,水题,比赛没做,QAQ。
就是给你一个二维数组,给你个颜色,问你这个颜色周围有几个不同的颜色。换到题目里面的意思就是,n*m个桌子,每个人的颜色都不一样,给你老板的桌子的颜色,让你看他周围 有几个不同的颜色,即助手
暴力,就可以了
My ugly code

#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?

Input

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).

Output

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.

Example
Input
5
2 9 8 2 7
Output
2 3
题意:一排巧克力吧,两个人,一个人从左边开始吃,另外一个人从右边开始吃,如果两人同时去吃一块巧克力,左边的人吃,右边的人很绅士的不吃,当一个人在吃巧克力A,其他人不能在去吃巧克力A。
模拟一下,基本就过了
My ugly code

#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.

Input

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).

Output

Print nk + 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".

Example
Input
5 3
1
2
2
3
3
Output
1
3
2
Input
6 4
3
3
3
4
4
2
Output
4
Nothing
3

就是让你把一个字符串里面每个连续的长度为k的串都找到最大值且只含一个这个数,找不到的输出Nothing,在这里分析一下第一个实例
5 3
1 2 2 3 3
串的总长度为5,选择为3的串,并且输出他们的最大值。
1 2 2中最大值&数量为1-------------1
2 2 3中最大值&数量为1-------------3
2 3 3中最大值&数量为1-------------2

我用的是模拟,看网上的代码是线段树,在这里粘一下本菜的代码
思路:用set容器存储选出来的k个序列,并且用map进行标记,某个数在序列中出现多少次
My ugly code
#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;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值