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 number
i1, he can make a sequence of jumps through the platforms
i1?<?i2?<?...?<?ik, if
i2?-?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 sequence
i1,?i2,?...,?i5, consisting of
five 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 of
n 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 platforms
2,?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 depth
n. More formally, the entrance to the park is located at the square
1. The exits out of the park are located at squares
2n,?2n?+?1,?...,?2n?+?1?-?1 and these exits lead straight to the Om Nom friends‘ houses. From each square
i (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 exactly
n 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 numbers
a2,?a3,?...
a2n?+?1?-?1 — the initial numbers of street lights on each road of the park. Here
ai is the number of street lights on the road between squares
i and . All numbers
ai are positive integers, not exceeding
100.

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 weighs
Wr grams and each blue candy weighs
Wb grams. Eating a single red candy gives Om Nom
Hr joy units and eating a single blue candy gives Om Nom
Hb 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 than
C 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 a
regular pattern. A sequence of beads
S is regular if it can be represented as
S?=?A?+?B?+?A?+?B?+?A?+?...?+?A?+?B?+?A, where
A and B are some bead sequences, "?+?" is the concatenation of sequences, there are exactly
2k?+?1 summands in this sum, among which there are
k?+?1 "A" summands and
k "B" summands that follow in alternating order. Om Nelly knows that her friend is an eager mathematician, so she doesn‘t mind if
A 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 a
regular 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 number
k 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. Position
i (1?≤?i?≤?n) must contain either number one if the first
i 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 take
A?=?"", B?=?"bca"), and a sequence of the first 7 beads (we can take
A?=?"b",
B?=?"ca").

In the second sample test, for example, a sequence of the first 13 beads is regular, if we take
A?=?"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时, ab_ab_ab 或者 _ab_ab_都是可行方案,现在讨论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");
}
时间: 2024-10-10 11:12:59

Codeforces ZeptoLab Code Rush 2015的相关文章

【codeforces ZeptoLab Code Rush 2015】ABCD题解

A. King of Thieves time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves&quo

【codeforces】ZeptoLab Code Rush 2015 E 跳跃表?

题意就是给n个数,围成一圈,就是1和n是相邻的,然后给一个数b,总和不超过b的一段连续的数可以组成一组,问最少可以将n个数分成几组. 可以将n个数后面再接n个数,就变成n+n个数,然后以每个数为开头的组最远能到哪也是很容易求的,O(n)维护个指针可以处理.把远的位置视为跳一步能到的吧,这样问题就转化为1到n中的第i个数至少到第n+i个数要跳多少次.这个如果是一般图的话,就是类似树上求k步的祖先在哪,可以用倍增法,n*log(n).但是这题图比较特殊,i<j的话,i跳到的下一点位置不会超过j跳到的

ZeptoLab Code Rush 2015 A. King of Thieves 暴力

A. King of Thieves Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/problem/A Description In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves" y

ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS

B. Om Nom and Dark Park Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/problem/B Description Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living a

ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力

C. Om Nom and Candies Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/problem/C Description A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him

ZeptoLab Code Rush 2015

A 题意:给出一串由.*组成的字符串,如果有等间距的五个及五个以上的*存在,则输出yes 直接枚举就可以了 看题一定要仔细啊,做的时候看成必须有五个等间距的".*"才可以跳跃= = 然后就这样写居然过了预测= =后来果然被hack了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6

ZeptoLab Code Rush 2015 A. King of Thieves

题目大意: 就是说,对于一个起点,使得从这个起点开始,每次间隔相同的格子后,所经过的地方都是‘*’ 解题思路: 直接暴力,枚举起点+枚举公差 代码: 1 # include<cstdio> 2 # include<iostream> 3 4 using namespace std; 5 6 # define MAX 123 7 8 char s[MAX]; 9 int a[MAX]; 10 11 int main(void) 12 { 13 int n; 14 scanf(&quo

ZeptoLab Code Rush 2015 B. Om Nom and Dark Park

1.题目描述:点击打开链接 2.解题思路:比赛时候这道题没有做出来,第二天早晨补题时才发现就是简单的DFS应用.题目要求出最少需要增加几盏路灯.假设我们已经知道了root的左子结点一共有suml盏路灯,右子结点一共有sumr盏路灯,那么比较一下d[lson(root)]+suml和d[rson(root)]+sumr的大小即可.此时需要增加的路灯数量就是两者差的绝对值.同时返回较大的数即得到root的总路灯数量. 3.代码: #define _CRT_SECURE_NO_WARNINGS #in

ZeptoLab Code Rush 2015 C. Om Nom and Candies

1.题目描述:点击打开链接 2.解题思路:本题是无限背包问题,根据重量的约束关系,直接暴力搜索. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #incl