Codeforces Round #482 (Div. 2)

A. Pizza, Pizza, Pizza!!!
time limit per test
1 second
memory limit per test
128 megabytes
input
standard input
output
standard output

Katie, Kuro and Shiro are best friends. They have known each other since kindergarten. That's why they often share everything with each other and work together on some very hard problems.

Today is Shiro's birthday. She really loves pizza so she wants to invite her friends to the pizza restaurant near her house to celebrate her birthday, including her best friends Katie and Kuro.

She has ordered a very big round pizza, in order to serve her many friends. Exactly nn of Shiro's friends are here. That's why she has to divide the pizza into n+1n+1 slices (Shiro also needs to eat). She wants the slices to be exactly the same size and shape. If not, some of her friends will get mad and go home early, and the party will be over.

Shiro is now hungry. She wants to cut the pizza with minimum of straight cuts. A cut is a straight segment, it might have ends inside or outside the pizza. But she is too lazy to pick up the calculator.

As usual, she will ask Katie and Kuro for help. But they haven't come yet. Could you help Shiro with this problem?

Input

A single line contains one non-negative integer nn (0n10180≤n≤1018) — the number of Shiro's friends. The circular pizza has to be sliced into n+1n+1 pieces.

Output

A single integer — the number of straight cuts Shiro needs.

Examples
input
Copy
3
output
Copy
2
input
Copy
4
output
Copy
5

思路:

找奇偶规律。注意特判0;

代码:

#include <iostream>

using namespace std;

int main()
{
    long long n;
    cin>>n;
    if(n==0)
    {
        cout<<0<<endl;
    }
    else
    {
    n++;
    if(n%2==0)
    {
        cout<<n/2<<endl;
    }
    else cout<<n<<endl;
    }
    return 0;
}


B. Treasure Hunt
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.

The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.

A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of 77 because its subribbon a appears 77 times, and the ribbon abcdabc has the beauty of 22 because its subribbon abc appears twice.

The rules are simple. The game will have nn turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after nn turns wins the treasure.

Could you find out who is going to be the winner if they all play optimally?

Input

The first line contains an integer nn (0n1090≤n≤109) — the number of turns.

Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than 105105uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.

Output

Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".

Examples
input
Copy
3
Kuroo
Shiro
Katie
output
Copy
Kuro
input
Copy
7
treasurehunt
threefriends
hiCodeforces
output
Copy
Shiro
input
Copy
1
abcabc
cbabac
ababca
output
Copy
Katie
input
Copy
15
foPaErcvJ
mZaxowpbt
mkuOlaHRE
output
Copy
Draw

思路:

就是看看那个先全转化为都是一种字母的情况,然是要注意n==1的情况。

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=110000;
char a[maxn],b[maxn],c[maxn];
long long a1[100],b1[100],c1[100];
long long maa=0,mab=0,mac=0;
int fa=0,fb=0,fc=0;
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s%s%s",a,b,c);
    int la=strlen(a);
    int lb=strlen(b);
    int lc=strlen(c);
    if(n>=la)
    {
        printf("Draw\n");
        return 0;
    }
    for(int i=0;i<la;i++)
    {
        int w=a[i]-'A';
        a1[w]++;
    }
    for(int i=0;i<60;i++)
    {
        maa=max(maa,a1[i]);
        //if((n-la+a1[i])%2==0)fa=1;
    }
    for(int i=0;i<lb;i++)
    {
        int w=b[i]-'A';
        b1[w]++;
    }
    for(int i=0;i<60;i++)
    {
        mab=max(mab,b1[i]);
        //if((n-lb-b1[i])%2==0)fb=1;
    }
    for(int i=0;i<lc;i++)
    {
        int w=c[i]-'A';
        c1[w]++;
    }
    for(int i=0;i<60;i++)
    {
        mac=max(mac,c1[i]);
        //if((n-lc+c1[i])%2==0)fc=1;
    }
    if(n==1&&maa==la)la--;
    if(n==1&&mab==lb)lb--;
    if(n==1&&mac==lc)lc--;
    if(maa+n>=la)
    {
        maa=la;
    }
    else maa=maa+n;
    if(mab+n>=lb)
    {
        mab=lb;
    }
    else mab=mab+n;
    if(mac+n>=lc)
    {
        mac=lc;
    }
    else mac=mac+n;
    long long ma=0;
    ma=max(ma,max(maa,max(mab,mac)));
    //cout<<maa<<" "<<mab<<" "<<mac<<endl;
    int sum=0;
    if(ma==maa)sum++;
    if(ma==mab)sum++;
    if(ma==mac)sum++;
    if(sum>=2)
    {
        printf("Draw\n");
    }
    else if(maa==ma)
    {
        printf("Kuro\n");
    }
    else if(ma==mab)
    {
        printf("Shiro\n");
    }
    else if(ma==mac)
    {
        printf("Katie\n");
    }
    return 0;
}
C. Kuro and Walking Route
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11 to nn, and n1n−1 bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns aa and bb. Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns (u,v)(u,v) (uvu≠v) and walk from uu using the shortest path to vv (note that (u,v)(u,v) is considered to be different from (v,u)(v,u)).

Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index xx) and Beetopia (denoted with the index yy). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns (u,v)(u,v) if on the path from uu to vv, he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuro’s body and sting him.

Kuro wants to know how many pair of city (u,v)(u,v) he can take as his route. Since he’s not really bright, he asked you to help him with this problem.

Input

The first line contains three integers nnxx and yy (1n31051≤n≤3⋅1051x,yn1≤x,y≤nxyx≠y) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

n1n−1 lines follow, each line contains two integers aa and bb (1a,bn1≤a,b≤naba≠b), describes a road connecting two towns aa and bb.

It is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree.

Output

A single integer resembles the number of pair of towns (u,v)(u,v) that Kuro can use as his walking route.

Examples
input
Copy
3 1 3
1 2
2 3
output
Copy
5
input
Copy
3 1 3
1 2
1 3
output
Copy
4
Note

On the first example, Kuro can choose these pairs:

  • (1,2)(1,2): his route would be 121→2,
  • (2,3)(2,3): his route would be 232→3,
  • (3,2)(3,2): his route would be 323→2,
  • (2,1)(2,1): his route would be 212→1,
  • (3,1)(3,1): his route would be 3213→2→1.

Kuro can't choose pair (1,3)(1,3) since his walking route would be 1231→2→3, in which Kuro visits town 11 (Flowrisa) and then visits town 33(Beetopia), which is not allowed (note that pair (3,1)(3,1) is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).

On the second example, Kuro can choose the following pairs:

  • (1,2)(1,2): his route would be 121→2,
  • (2,1)(2,1): his route would be 212→1,
  • (3,2)(3,2): his route would be 3123→1→2,
  • (3,1)(3,1): his route would be 313→1.

思路:

就是用dfs搜出,两颗x,y下的子树的全组合(x子树不能包含y,y也一样)。

自己做了一种从一颗子树种去除其他子树的计算方法,一直wa,这种异或的方法学习了。

#include <bits/stdc++.h>

using namespace std;
const int maxn=3e5+4;
int vis[maxn],chk_sub[maxn];
int sub_size[maxn];
vector<int>G[maxn];
int x,y;
int dfs(int u,int fa)
{
    sub_size[u]=1;
    if(u==x)
    {
        chk_sub[u]=1;
    }
    else chk_sub[u]=0;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(v==fa)continue;
        sub_size[u]+=dfs(v,u);
        chk_sub[u]|=chk_sub[v];
    }
    return sub_size[u];
}
int main()
{
    int n;
    scanf("%d%d%d",&n,&x,&y);
    int u,v;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(y,-1);
    long long sum=0;
    for(int i=0;i<G[y].size();i++)
    {
        int v=G[y][i];
        if(chk_sub[v])
        {
            sum=sub_size[y]-sub_size[v];
            break;
        }
    }
    printf("%I64d\n",(long long)n*(n-1)-sum*sub_size[x]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值