Codeforces ZeptoLab Code Rush 2015

54 篇文章 0 订阅
53 篇文章 0 订阅

Codeforces ZeptoLab Code Rush 2015


比赛链接:http://codeforces.com/contest/526/


A. King of Thieves
time limit per test:1 second
memory limit per test:256 megabytes

In this problem you will meet the simplified model of game King of Thieves.

In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.

An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.

A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as'*' and pits are represented as '.'.

One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform numberi1, he can make a sequence of jumps through the platformsi1 < i2 < ... < ik, ifi2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ...ik should be exactly the platforms, not pits.

Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequencei1, i2, ..., i5, consisting offive platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.

Input

The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.

Next line contains the scheme of the level represented as a string ofn characters '*' and '.'.

Output

If the level is good, print the word"yes" (without the quotes), otherwise print the word"no" (without the quotes).

Sample test(s)
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note

In the first sample test you may perform a sequence of jumps through platforms2, 5, 8, 11, 14.


题目大意:给个字符串,问能不能找到连续5个下标成等差数列的位置上字符都为' * '的情况


题目分析:数字小,暴力枚举公差,然后模拟


#include <cstdio>
#include <cstring>
char s[105];
int a[105];

int main()
{
    int len;
    bool flag = false;
    scanf("%d %s", &len, s);
    int d = 1;
    while(d <= len / 4)
    {
        for(int i = 0; i < len; i++)
        {
            if(s[i] == '*' && s[i + d] == '*' && s[i + 2 * d] == '*' && s[i + 3 * d] == '*' && s[i + 4 * d] == '*')
            {
                flag = true;
                break;
            }
        }
        d ++;
        if(flag)
            break;
    }
    if(flag)
        printf("yes\n");
    else
        printf("no\n");
}   


B. Om Nom and Dark Park
time limit per test:1 second
memory limit per test:256 megabytes

Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him.

The park consists of 2n + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depthn. More formally, the entrance to the park is located at the square1. The exits out of the park are located at squares2n, 2n + 1, ..., 2n + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each squarei (2 ≤ i < 2n + 1) there is a road to the square. Thus, it is possible to go from the park entrance to each of the exits by walking along exactlyn roads.

To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square i to square has ai lights.

Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe.

He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.

Input

The first line contains integer n (1 ≤ n ≤ 10) — the number of roads on the path from the entrance to any exit.

The next line contains 2n + 1 - 2 numbersa2, a3, ...a2n + 1 - 1 — the initial numbers of street lights on each road of the park. Hereai is the number of street lights on the road between squaresi and . All numbersai are positive integers, not exceeding100.

Output

Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.

Sample test(s)
Input
2
1 2 3 4 5 6
Output
5
Note

Picture for the sample test. Green color denotes the additional street lights.




题目大意:一颗满二叉树,每条路上有一个权值表示灯的个数,现在要从根到所有叶子的路径上灯个数总和相同,问至少需要添加多少灯


题目分析:若要相等,显然兄弟到父亲路径上的灯数要一样,因此直接从叶子向上模拟即可,将灯数不断累加给父亲一直到根,每次兄弟到父亲路径上的差值的和就是最少需要补充的灯数


#include <cstdio>
#include <algorithm>
using namespace std;
int n, a[5000];

int main()
{
    int ans = 0;
    scanf("%d", &n);
    for(int i = 2; i <= (1 << (n + 1)) - 1; i++)
        scanf("%d", &a[i]);
    for(int i = (1 << (n + 1)) - 1; i >= 2; i -= 2)
    {
        ans += abs(a[i] - a[i - 1]);
        a[i / 2] += max(a[i], a[i - 1]);
    }
    printf("%d\n", ans);
}




C. Om Nom and Candies
time limit per test:1 second
memory limit per test:256 megabytes

A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?

One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighsWr grams and each blue candy weighsWb grams. Eating a single red candy gives Om NomHr joy units and eating a single blue candy gives Om NomHb joy units.

Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more thanC grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.

Input

The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).

Output

Print a single integer — the maximum number of joy units that Om Nom can get.

Sample test(s)
Input
10 3 5 2 3
Output
16
Note

In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.


题目大意:红糖有wr克,每克能得到hr的幸福值,蓝糖有wb克,每克能得到hb的幸福值,现在问最多吃c克能得到的最大幸福值


题目分析:这样的数据量显然不能用背包做,采用部分贪心策略,及另lcm为wr和wb的最小公倍数,如果c % lcm == 0且c / lcm >= 2,则对于容量为w1 = c / lcm - 1(这里减1是因为保证结果尽可能的大,如果直接取c/lcm的话不能除尽的部分可能会有很多空余位置)的部分我们可以直接贪心,结果记为sum,sum =w1 * max(lcm / wb * hb,lcm / wr * hr),剩下的部分直接枚举,枚举的时候取min(c / wr,c / wb)作为上界,这样可以防止超时。不妨假设wr比wb大,若wr数值很大,则枚举的时候c / wr就很小,若wr很小,则c = c % lcm后c就很小,则此时c / wr还是很小,由此可得本题部分贪心+部分暴力的方法是可行的


#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std; 

ll Gcd(ll a, ll b)
{
    return b ? Gcd(b, a % b) : a;  
}

ll Lcm(ll a, ll b)
{
    return a * b / Gcd(a, b);
}
int main()
{
    ll c, h1, h2, w1, w2, ans = 0; 
    //scanf("%lld %lld %lld %lld %lld", &c, &w1, &h1, &w2, &h2);
    scanf("%I64d %I64d %I64d %I64d %I64d", &c, &h1, &h2, &w1, &w2);
    ll lcm = Lcm(w1, w2);
    ll tmp = c / lcm;
    c = c % lcm;
    if(tmp)
    { 
        tmp --; 
        c += lcm; 
    } 
    ll sum = tmp * (max(lcm / w1 * h1, lcm / w2 * h2));
    if(w1 < w2)
    {
        swap(w1, w2);
        swap(h1, h2);
    }
    for(int i = 0; i <= c / w1; i++)
        ans = max(ans, i * h1 + (c - i * w1) / w2 * h2);
    ans += sum;
    printf("%I64d\n", ans);
    //printf("%lld\n", ans);
}





D. Om Nom and Necklace
time limit per test
1 second
memory limit per test
256 megabytes

One day Om Nom found a thread with n beads of different colors. He decided to cut the first several beads from this thread to make a bead necklace and present it to his girlfriend Om Nelly.

Om Nom knows that his girlfriend loves beautiful patterns. That's why he wants the beads on the necklace to form aregular pattern. A sequence of beads S is regular if it can be represented asS = A + B + A + B + A + ... + A + B + A, whereA and B are some bead sequences, " + " is the concatenation of sequences, there are exactly2k + 1 summands in this sum, among which there arek + 1 "A" summands andk "B" summands that follow in alternating order. Om Nelly knows that her friend is an eager mathematician, so she doesn't mind ifA or B is an empty sequence.

Help Om Nom determine in which ways he can cut off the first several beads from the found thread (at least one; probably, all) so that they form aregular pattern. When Om Nom cuts off the beads, he doesn't change their order.

Input

The first line contains two integers n,k (1 ≤ n, k ≤ 1 000 000) — the number of beads on the thread that Om Nom found and numberk from the definition of the regular sequence above.

The second line contains the sequence of n lowercase Latin letters that represent the colors of the beads. Each color corresponds to a single letter.

Output

Print a string consisting of n zeroes and ones. Positioni (1 ≤ i ≤ n) must contain either number one if the firsti beads on the thread form a regular sequence, or a zero otherwise.

Sample test(s)
Input
7 2
bcabcab
Output
0000011
Input
21 2
ababaababaababaababaa
Output
000110000111111000011
Note

In the first sample test a regular sequence is both a sequence of the first 6 beads (we can takeA = "", B = "bca"), and a sequence of the first 7 beads (we can takeA = "b",B = "ca").

In the second sample test, for example, a sequence of the first 13 beads is regular, if we takeA = "aba",B = "ba".


题目大意:给一个长为n的字符串,对于其前缀子串,如果满足A+B+A+B+...+这样的形式,并且刚好有k+1个A和k个B,则对应位置值为1否则为0,注意A和B可能是空串


题目分析:非常纠结的一道题,首先想到的是枚举一个周期的串长,联想到kmp里的next数组的一个性质,即i - next[i]就表示长度为i的前缀串中的周期串的一个周期的串长,先求next数组,注意这里求的next数组和一般kmp里的next数组不太一样,因为这里对任意前缀串它的前后缀子串不能有重叠的部分,比如ababaa,如果在普通next数组里会是-1 0 0 1 2 3,而我们目标的next数组为-1 -1 0 1 2 0

s         a  b   a   b   a   a

i          0  1   2   3   4   5

next  -1  -1  0   1   2   0

len     1   2  2    2  2   5

next里第二个为-1是因为它对应的是当前位置,若是0的话则1-next[1] = 1显然周期串长为2(ab)

得到next数组后,我们要计算几个值,len表示周期串长,num表示周期,t表示周期不能被k整除的部分,可以发现若t==0,则肯定可以找到可行方案,因为整除的话,根据周期的可加性,例如k=2我们必然可以得到_A_A_这样的串,现在讨论t不等于0时,如果不是一个完整的周期串,t要加1,把周期去掉不能整除的部分再分成k份,若每份的个数小于t,则显然没有可行方案,反之存在可行方案



#include <cstdio>
#include <cstring>
int const MAX = 1e6 + 5;

char s[MAX];  
int next[MAX];  
int n, k;

void get_next()
{ 
    int j = -1; 
    next[0] = -1; 
    for(int i = 1; i < n; i++)
    { 
        while(j != -1 && s[j + 1] != s[i])
            j = next[j]; 
        if(s[j + 1] == s[i] ) 
            j++; 
        next[i] = j; 
    } 
}

int main() 
{
    scanf("%d %d %s", &n, &k, s);
    get_next();
    for(int i = 0; i < n; i++) 
    {
        int len = i - next[i];   
        int num = (i + 1) / len;   
        int t = num % k; 
        if(t == 0)
            printf("1");
        else
        {
            if(len * num != i + 1) 
                t ++;
            if((num - (num % k)) / k >= t) 
                printf("1");
            else 
                printf("0");
        }
    }
    printf("\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值