Codeforces Round #368 (Div. 2) A~D

第三次CF。。排名还是380左右。。
一个小时不到做完前三道然后剩下两道题读懂题意用了20min。。苦苦冥思了半个小时并没有卵用。。就去hack耍
这里写图片描述
Codeforces第一次hack,还成功了233333
这里写图片描述


A. Brain’s Photos
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

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.

Examples
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


判断一下即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define AUTO "%I64d"
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            char ch=getchar();
            while(ch^'C' && ch^'M' && ch^'Y' && ch^'W' && ch^'G' && ch^'B') ch=getchar();
            if(ch=='C' || ch=='M' || ch=='Y')
            {
                printf("#Color");
                return 0;
            }
        }
    printf("#Black&White");
    return 0;
}

B. Bakery
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
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.

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


要找里面粉中心最近的点,且不能安放在面粉中心。
那么kruskal一样把边排序,然后一条一条判断,如果边连接了面粉中心和空白点,那么输出。
扫完了还没有找到就输出-1.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define AUTO "%I64d"
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int maxn = 100005;
struct Road
{
    int u,v;
    int c;
    bool operator < (const Road t) const
    {
        return c < t.c;
    }
}road[maxn];
int n,m,k;
bool base[maxn];
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=m;i++) scanf("%d%d%d",&road[i].u,&road[i].v,&road[i].c);
    sort(road+1,road+m+1);
    for(int i=1;i<=k;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        base[tmp]=true;
    }
    for(int i=1;i<=m;i++)
    {
        Road t=road[i];
        if(base[t.u] ^ base[t.v])
        {
            printf("%d",t.c);
            return 0;
        }
    }
    printf("-1");
    return 0;
}

C. Pythagorean Triples
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

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

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


这道题就是已知某勾股数三元组中的其中一个,要求另外两个(任意解)。
那么做法有很多。。
首先如果这个数一直除以2以后是一个大于等于3的奇数,设为x , 那么一定满足 x^2 = y + z (x < y < z)
与之对立的情况便是2的k次幂的情况,所以把原数一直除以2,期间判断(x*2+1)是不是完全平方数,如果是,那么找到解。
然后输出无解信息即可。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define AUTO "%I64d"
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
LL n;
inline bool is_sqr(LL x)
{
    LL tmp=floor(sqrt(x+0.5));
    tmp*=tmp;
    return tmp==x;
}
int main()
{
    scanf(AUTO,&n);
    if(n<3ll)
    {
        printf("-1");
        return 0;
    }
    int bits=0;
    while(!(n&1ll)) n>>=1,bits++;
    if(n>=3ll && n&1ll) // case 1 , left the exp of 2
    {
        LL x,y;
        x=(n*n-1ll)>>1; y=x+1ll;
        x<<=bits; y<<=bits;
        printf(AUTO " " AUTO,x,y);
        return 0;
    }
    n<<=bits;
    bits=0;
    while(n>3ll)
    {
        LL tmp=(n<<1)+1ll;
        if(is_sqr(tmp))
        {
            LL y=n+1ll;
            LL x=floor(sqrt(tmp+0.5));
            printf(AUTO " " AUTO , x<<bits , y<<bits);
            return 0;
        }
        bits++;
        n>>=1;
    }
    printf("-1");
    return 0;
}

D. Persistent Bookcase
time limit per test 2 seconds
memory limit per test 512 megabytes
input standard input
output standard output

Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.

After reaching home Alina decided to invent her own persistent data structure. Inventing didn’t take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.

The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1 to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.

Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:

1 i j — Place a book at position j at shelf i if there is no book at it.
2 i j — Remove the book from position j at shelf i if there is a book at it.
3 i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed.
4 k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.
After applying each of operation Alina is interested in the number of books in the bookcase. Alina got ‘A’ in the school and had no problem finding this values. Will you do so?

Input
The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively.

The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement.

It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.

Output
For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.

Examples
input
2 3 3
1 1 1
3 2
4 0
output
1
4
0
input
4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2
output
2
1
3
3
2
4
input
2 2 2
3 2
2 2 1
output
2
1
Note
这里写图片描述
This image illustrates the second sample case.


不知道这道题怎么写可持久化。。
那么就可以用类似与莫队一样的思想来合理安排处理顺序,使得耗时最小。。
当然要离线处理,然后对于第i个询问,如果是前三种类型就连边 i-1->i ,否则连边 k->i ,然后反转用标记的话,操作的状态转移都是O(1)的,那么从0开始dfs,一次对每个询问进行处理并回溯,注意写的时候要专心。。。。。要理清楚反转之后是true还是false。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define AUTO "%I64d"
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int maxn = 1005;
const int maxq = 100005;
struct Edge
{
    int to,next;
}edge[maxq<<1];
int head[maxq];
int maxedge;
inline void addedge(int u,int v)
{
    edge[++maxedge] = (Edge) { v,head[u] };
    head[u] = maxedge;
}
struct Que
{
    int opt;
    int i,j,k;
}que[maxq];
int n,m,q;
bool book[maxn][maxn];
bool rev[maxn];
int cnt[maxn];
int cntall;
int ans[maxq];
void dfs(int u)
{
    ans[u]=cntall;
    for(int e=head[u];~e;e=edge[e].next)
    {
        int v = edge[e].to;
        bool flag=false;
        int i=que[v].i,j=que[v].j;
        int tmp;
        switch(que[v].opt)
        {
            case 1: if(rev[i])
                        if(book[i][j]) book[i][j]=false,flag=true,cnt[i]++,cntall++;
                        else break;
                    else
                        if(book[i][j]) break;
                        else book[i][j]=true,flag=true,cnt[i]++,cntall++;
                    break;
            case 2: if(rev[i])
                        if(book[i][j]) break;
                        else book[i][j]=true,flag=true,cnt[i]--,cntall--;
                    else
                        if(book[i][j]) book[i][j]=false,flag=true,cnt[i]--,cntall--;
                        else break;
                    break;
            case 3: tmp=cnt[i];
                    cnt[i]=m-tmp;
                    cntall+=cnt[i]-tmp;
                    flag=true;
                    rev[i]^=1;
                    break;
            case 4: break;
        }
        dfs(v);
        if(!flag) continue;
        switch(que[v].opt)
        {
            case 1: if(rev[i]) book[i][j]=true;
                    else book[i][j]=false;
                    cnt[i]--; cntall--;
                    break;
            case 2: if(rev[i]) book[i][j]=false;
                    else book[i][j]=true;
                    cnt[i]++; cntall++;
                    break;
            case 3: tmp=cnt[i];
                    cnt[i]=m-tmp;
                    cntall+=cnt[i]-tmp;
                    rev[i]^=1;
                    break;
            case 4: break;
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&q);
    memset(head,-1,sizeof(head));
    maxedge=-1;
    for(int i=1;i<=q;i++)
    {
        scanf("%d",&que[i].opt);
        if(que[i].opt<=2) scanf("%d%d",&que[i].i,&que[i].j),addedge(i-1,i);
        if(que[i].opt==3) scanf("%d",&que[i].i),addedge(i-1,i);
        if(que[i].opt==4) scanf("%d",&que[i].k),addedge(que[i].k,i);
    }
    memset(book,0,sizeof(book));
    memset(rev,0,sizeof(rev));
    memset(cnt,0,sizeof(cnt));
    cntall=0;
    dfs(0);
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值