Google Code Jam 2015

Problem A. Standing Ovation

Problem

It's opening night at the opera, and your friend is the prima donna (the lead female singer). You will not be in the audience, but you want to make sure she receives a standing ovation -- with every audience member standing up and clapping their hands for her.

Initially, the entire audience is seated. Everyone in the audience has a shyness level. An audience member with shyness level Si will wait until at least Si other audience members have already stood up to clap, and if so, she will immediately stand up and clap. If  Si = 0 , then the audience member will always stand up and clap immediately, regardless of what anyone else does. For example, an audience member with Si = 2 will be seated at the beginning, but will stand up to clap later after she sees at least two other people standing and clapping.

You know the shyness level of everyone in the audience, and you are prepared to invite additional friends of the prima donna to be in the audience to ensure that everyone in the crowd stands up and claps in the end. Each of these friends may have any shyness value that you wish, not necessarily the same. What is the minimum number of friends that you need to invite to guarantee a standing ovation?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of one line with Smax, the maximum shyness level of the shyest person in the audience, followed by a string of  Smax + 1  single digits. The kth digit of this string (counting starting from 0) represents how many people in the audience have shyness level k. For example, the string "409" would mean that there were four audience members with  Si = 0  and nine audience members with  Si = 2  (and none with  Si = 1  or any other value). Note that there will initially always be between 0 and 9 people with each shyness level.

The string will never end in a 0. Note that this implies that there will always be at least one person in the audience.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum number of friends you must invite.

Limits

1 ≤ T ≤ 100.

Small dataset

0 ≤ Smax ≤ 6.

Large dataset

0 ≤ Smax ≤ 1000.

Sample


Input 
 

Output 
 
4
4 11111
1 09
5 110011
0 1

Case #1: 0
Case #2: 1
Case #3: 2
Case #4: 0
In Case #1, the audience will eventually produce a standing ovation on its own, without you needing to add anyone -- first the audience member with  Si = 0  will stand up, then the audience member with  Si = 1  will stand up, etc.

In Case #2, a friend with  Si = 0  must be invited, but that is enough to get the entire audience to stand up.

In Case #3, one optimal solution is to add two audience members with  Si = 2 .

In Case #4, there is only one audience member and he will stand up immediately. No friends need to be invited.

题意清晰明了,没什么好说的,为节省空间,不贴代码了

Problem B. Infinite House of Pancakes

Problem

At the Infinite House of Pancakes, there are only finitely many pancakes, but there are infinitely many diners who would be willing to eat them! When the restaurant opens for breakfast, among the infinitely many diners, exactly D have non-empty plates; the ith of these has Pi pancakes on his or her plate. Everyone else has an empty plate.

Normally, every minute, every diner with a non-empty plate will eat one pancake from his or her plate. However, some minutes may be special. In a special minute, the head server asks for the diners' attention, chooses a diner with a non-empty plate, and carefully lifts some number of pancakes off of that diner's plate and moves those pancakes onto one other diner's (empty or non-empty) plate. No diners eat during a special minute, because it would be rude.

You are the head server on duty this morning, and it is your job to decide which minutes, if any, will be special, and which pancakes will move where. That is, every minute, you can decide to either do nothing and let the diners eat, or declare a special minute and interrupt the diners to make a single movement of one or more pancakes, as described above.

Breakfast ends when there are no more pancakes left to eat. How quickly can you make that happen?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of one line with D, the number of diners with non-empty plates, followed by another line with D space-separated integers representing the numbers of pancakes on those diners' plates.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the smallest number of minutes needed to finish the breakfast.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ D ≤ 6.
1 ≤ Pi ≤ 9.

Large dataset

1 ≤ D ≤ 1000.
1 ≤ Pi ≤ 1000.

Sample


Input 
 

Output 
 
3
1
3
4
1 2 1 2
1
4

Case #1: 3
Case #2: 2
Case #3: 3

In Case #1, one diner starts with 3 pancakes and everyone else's plate is empty. One optimal strategy is:

Minute 1: Do nothing. The diner will eat one pancake.

Minute 2 (special): Interrupt and move one pancake from that diner's stack onto another diner's empty plate. (Remember that there are always infinitely many diners with empty plates available, no matter how many diners start off with pancakes.) No pancakes are eaten during an interruption.

Minute 3: Do nothing. Each of those two diners will eat one of the last two remaining pancakes.

In Case #2, it is optimal to let the diners eat for 2 minutes, with no interruptions, during which time they will finish all the pancakes.

In Case #3, one diner starts with 4 pancakes and everyone else's plate is empty. It is optimal to use the first minute as a special minute to move two pancakes from the diner's plate to another diner's empty plate, and then do nothing and let the diners eat for the second and third minutes.

一开始一不小心看错题目了。。。蛋疼
因为每分钟只能选一个人来重新分配他的食物,所以如果有x个人,他们手上的食物全部来源于一个人a,那么最节省时间的分配方案必然是a把自己的食物按x-1份分配出去。节省空间,懒得贴代码了



Problem C. Dijkstra

Problem

The Dutch computer scientist Edsger Dijkstra made many important contributions to the field, including the shortest path finding algorithm that bears his name. This problem is not about that algorithm.

You were marked down one point on an algorithms exam for misspelling "Dijkstra" -- between D and stra, you wrote some number of characters, each of which was either ij, or k. You are prepared to argue to get your point back using quaternions, an actual number system (extended from complex numbers) with the following multiplicative structure:



To multiply one quaternion by another, look at the row for the first quaternion and the column for the second quaternion. For example, to multiply i by j, look in the row for i and the column for j to find that the answer is k. To multiply j by i, look in the row for j and the column for i to find that the answer is -k.

As you can see from the above examples, the quaternions are not commutative -- that is, there are some a and b for which a * b != b * a. However they are associative -- for any a,b, and c, it's true that a * (b * c) = (a * b) * c.

Negative signs before quaternions work as they normally do -- for any quaternions a andb, it's true that -a * -b = a * b, and -a * b = a * -b = -(a * b).

You want to argue that your misspelling was equivalent to the correct spelling ijk by showing that you can split your string of is, js, and ks in two places, forming three substrings, such that the leftmost substring reduces (under quaternion multiplication) to i, the middle substring reduces to j, and the right substring reduces to k. (For example, jijwould be interpreted as j * i * jj * i is -k, and -k * j is i, so jij reduces to i.) If this is possible, you will get your point back. Can you find a way to do it?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of one line with two space-separated integers L and X, followed by another line with L characters, all of which are ij, or k. Note that the string never contains negative signs, 1s, or any other characters. The string that you are to evaluate is the given string ofL characters repeated X times. For instance, for L = 4, X = 3, and the given string kiij, your input string would be kiijkiijkiij.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is either YES or NO, depending on whether the string can be broken into three parts that reduce to ij, and k, in that order, as described above.

Limits

1 ≤ T ≤ 100.
1 ≤ L ≤ 10000.

Small dataset

1 ≤ X ≤ 10000.
1 ≤ L * X ≤ 10000.

Large dataset

1 ≤ X ≤ 1012.
1 ≤ L * X ≤ 1016.

Sample


Input 
 

Output 
 
5
2 1
ik
3 1
ijk
3 1
kji
2 6
ji
1 10000
i

Case #1: NO
Case #2: YES
Case #3: NO
Case #4: YES
Case #5: NO
In Case #1, the string is too short to be split into three substrings.

In Case #2, just split the string into  i j , and  k .

In Case #3, the only way to split the string into three parts is  k j i , and this does not satisfy the conditions.

In Case #4, the string is  jijijijijiji . It can be split into  jij  (which reduces to  i ),  iji (which reduces to  j ), and  jijiji  (which reduces to  k ).

In Case #5, no matter how you choose your substrings, none of them can ever reduce to a j  or a  k .

小数据可以暴力搜,解大数据的突破口就在于如果字符串s重复4次,其运算结果必然是1。因此对于重复x的情况,尽可能按4的倍数消去就好了
#include <cstdio>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
#include <array>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int MAXN = 10005;
typedef long long LL;
int ad[300];
int mmp[9][10] = {{},
    {0,1,2,3,4},{0,2,-1,4,-3},{0,3,-4,-1,2},{0,4,3,-2,-1}
};
struct node
{
    int n,f;
    node operator * (const node &a)
    {
        node res = node {mmp[n][a.n],f*a.f};
        if (res.n < 0) res.f = -res.f, res.n = -res.n;
        return res;
    }
    bool operator == (const node &a) const
    {
        return n == a.n && f == a.f;
    }
};
node mpow(node a, LL n)
{
    node p = a, res {1,1};
    while (n)
    {
        if (n & 1LL) res = res*p;
        p = p*p;
        n >>= 1;
    }
    return res;
}
node pow(node a, int n)
{
    node res {1,1};
    if (n <= 0) return res;
    n %= 4;
    for (LL i = 0; i< n; ++i)
        res = res * a;
    return res;
}
node num[MAXN], lbg[MAXN], rbg[MAXN];
char s[MAXN];
int solve()
{
    int n;
    LL m;
    scanf("%d%lld", &n, &m);
    scanf("%s", s+1);
    node seg {1,1};
    rbg[0] = {1,1};
    for (int i = 1; i<= n; ++i)
    {
        num[i] = node {ad[s[i]], 1};
        seg = seg * num[i];
        rbg[i] = rbg[i-1]*num[i];
    }
    lbg[n+1] = {1,1};
    for (int i = n; i>0; --i)
    {
        lbg[i] = num[i]*lbg[i+1];
    }
    node sum {1,1};
    LL df = (m*n - 4LL*n)/(4LL*n);
    int mn = m*n-4LL*n*df;
    for (int i = 1, ii; i<= mn; ++i)
    {
        ii = (i-1)%n + 1;
        sum = sum * num[ii];
        if (sum == node {2,1})
        {
            node bsum {1,1};
            for (int j = i+1, jj; j<= mn; ++j)
            {
                jj = (j-1)%n + 1;
                bsum = bsum*num[jj];
                if (bsum == node {3,1})
                {
                    if (j == mn) continue;
                    int kk = (j)%n + 1;
                    node csum = lbg[kk]*pow(seg, (mn-j-1)/n);
                    if (csum == node{4,1})
                        return 1;
                }
            }
        }
    }
    return 0;
}
int main()
{
    freopen("C-large.in", "r",stdin);
    //freopen("in.txt", "r",stdin);
    freopen("out.txt", "w", stdout);
    int t;
    scanf("%d", &t);
    ad['1']=1;
    ad['i']=2;
    ad['j']=3;
    ad['k']=4;
    for (int _ = 1; _<= t; ++_)
    {
        printf("Case #%d: ", _);
        if (solve())
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}



Problem D. Ominous Omino

Problem

An N-omino is a two-dimensional shape formed by joining N unit cells fully along their edges in some way. More formally, a 1-omino is a 1x1 unit square, and an N-omino is an (N-1)omino with one or more of its edges joined to an adjacent 1x1 unit square. For the purpose of this problem, we consider two N-ominoes to be the same if one can be transformed into the other via reflection and/or rotation. For example, these are the five possible 4-ominoes:



And here are some of the 108 possible 7-ominoes:



Richard and Gabriel are going to play a game with the following rules, for some predetermined values of XR, and C:

1. Richard will choose any one of the possible X-ominoes.
2. Gabriel must use at least one copy of that X-omino, along with arbitrarily many copies of any X-ominoes (which can include the one Richard chose), to completely fill in an R-by- C  grid, with no overlaps and no spillover. That is, every cell must be covered by exactly one of the X cells making up an X-omino, and no X-omino can extend outside the grid. Gabriel is allowed to rotate or reflect as many of the X-ominoes as he wants, including the one Richard chose. If Gabriel can completely fill in the grid, he wins; otherwise, Richard wins.

Given particular values XR, and C, can Richard choose an X-omino that will ensure that he wins, or is Gabriel guaranteed to win no matter what Richard chooses?

Input

The first line of the input gives the number of test cases, TT lines follow. Each contains three space-separated integers: XR, and C.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is either RICHARD (if there is at least one choice that ensures victory for Richard) or GABRIEL (if Gabriel will win no matter what Richard chooses).

Limits

Small dataset

T = 64.
1 ≤ X, R, C ≤ 4.

Large dataset

1 ≤ T ≤ 100.
1 ≤ X, R, C ≤ 20.

Sample


Input 
 

Output 
 
4
2 2 2
2 1 3
4 4 1
3 2 3

Case #1: GABRIEL
Case #2: RICHARD
Case #3: RICHARD
Case #4: GABRIEL
In case #1, Richard only has one 2-omino available to choose -- the 1x2 block formed by joining two unit cells together. No matter how Gabriel places this block in the 2x2 grid, he will leave a hole that can be exactly filled with another 1x2 block. So Gabriel wins.

In case #2, Richard has to choose the 1x2 block, but no matter where Gabriel puts it, he will be left with a single 1x1 hole that he cannot fill using only 2-ominoes. So Richard wins.

In case #3, one winning strategy for Richard is to choose the 2x2 square 4-omino. There is no way for Gabriel to fit that square into the 4x1 grid such that it is completely contained within the grid, so Richard wins.

In case #4, Richard can either pick the straight 3-omino or the L-shaped 3-omino. In either case, Gabriel can fit it into the grid and then use another copy of the same 3-omino to fill in the remaining hole.

如果N大于等于7,必然可以围成一个中间含有洞的图形,这样就无法实现完全覆盖
如果 r*c % N!=0 ,也无法完全覆盖
枚举根据N生成的图形,按90°旋转四次,在此基础上枚举该图形的摆放位置,对于剩下的每个连通区域内矩形个数a,判断 a%N == 0是否成立
#include <cstdio>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
#include <array>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int MAXN = 50;
int vis[MAXN][MAXN];
int r, c, N;
inline int isoverborder(int x, int y, int xx = 0)
{
    return x<xx || x >= r || y < 0 || y >= c;
}
const int dir[4][2] = {0,1,0,-1,-1,0,1,0};
int bx;
int da[MAXN][2], nd;
int mvis[MAXN][MAXN];
int bfs(int x, int y)
{
    static int qq[500][2];
    int head = 0, tail = 0;
    qq[tail][0] = x;
    qq[tail][1] = y;
    ++tail;
    int cnt = 0;
    while (head < tail)
    {
        int xx = qq[head][0];
        int yy = qq[head][1];
        ++head;
        if (mvis[xx][yy]) continue;
        mvis[xx][yy] = 1;
        ++cnt;
        for (int i = 0; i< 4; ++i)
        {
            int a = xx+dir[i][0], b = yy+dir[i][1];
            if (isoverborder(a,b) || mvis[a][b]) continue;
            qq[tail][0] = a;
            qq[tail++][1] = b;
        }
    }
    return cnt;
}
int msearch()
{
    int cnt = 0;
    for (int i = 0; i< r; ++i)
    {
        for (int j = 0; j< c; ++j)
        {
            if (mvis[i][j]) continue;
            if (bfs(i, j) % N != 0)
                return 0;
        }
    }
    return 1;
}
void zhuan(int nd)
{
    for (int i = 0, j; i< nd; ++i)
    {
        j = da[i][0];
        da[i][0] = -da[i][1];
        da[i][1] = j;
    }
}
int caonima[MAXN][2];
int dfs1(int nd)
{
    if (nd == N)
    {
        memcpy(caonima, da, sizeof da);
        for (int dg = 0; dg < 4; ++dg)
        {
            zhuan(nd);
            int mxx=da[0][0], mnx=da[0][0], mxy=da[0][1], mny=da[0][1];
            for (int i = 1; i< N; ++i)
            {
                mxx = max(mxx, da[i][0]);
                mnx = min(mnx, da[i][0]);
                mxy = max(mxy, da[i][1]);
                mny = min(mny, da[i][1]);
            }
            for (int i = 0; i< r; ++i)
            {
                for (int j = 0; j< c; ++j)
                {
                    // 找起点
                    if ((mxx + i) < r &&(mnx + i) >= 0 && (mxy+j) < c && (mny+j)>=0)
                    {
                        memset(mvis, 0, sizeof mvis);
                        for (int u = 0; u< N; ++u)
                        {
                            mvis[i+da[u][0]][j+da[u][1]] = 1;
                        }
                        // 剩余的联通区域内未覆盖的数量都是N的倍数
                        if (msearch())
                        {
                            memcpy(da, caonima, sizeof da);
                            return 0;
                        }
                    }
                }
            }
        }
        return 1;
    }
    for (int i = 0; i< nd; ++i)
    {
        int x = da[i][0], y = da[i][1];
        for (int j = 0; j< 4; ++j)
        {
            int xx = x+dir[j][0], yy = y+dir[j][1];
            if (xx>=0 && !vis[xx+20][yy+20])
            {
                vis[xx+20][20+yy] = 1;
                da[nd][0] = xx;
                da[nd][1] = yy;
                if (dfs1(nd+1))
                    return 1;
                vis[xx+20][20+yy] = 0;
            }
        }
    }
    return 0;
}
int solve()
{
    scanf("%d%d%d", &N, &r, &c);
    //printf("(%d,%d,%d) ", N, r, c);
    if (r*c % N!=0) return 1;
    if (N == 1) return 0;
    if (N >= 7) return 1;
    da[0][0] = da[0][1] = 0;
    memset(vis, 0, sizeof vis);
    vis[20][20] = 1;
    return dfs1(1);
}
int main()
{
    freopen("D-large.in", "r",stdin);
    //freopen("in.txt", "r",stdin);
    freopen("out.txt", "w", stdout);
    int t;
    scanf("%d", &t);
    for (int _ = 1; _<= t; ++_)
    {
        printf("Case #%d: ", _);
        if (solve())
            puts("RICHARD");
        else
            puts("GABRIEL");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值