Google Code Jam 2014 Qualification Round 2014 A B D

来源:互联网 发布:单片机工程师 编辑:程序博客网 时间:2024/06/05 11:00

Problem A. Magic Trick

Problem

Recently you went to a magic show. You were very impressed by one of the tricks, so you decided to try to figure out the secret behind it!

The magician starts by arranging 16 cards in a square grid: 4 rows of cards, with 4 cards in each row. Each card has a different number from 1 to 16 written on the side that is showing. Next, the magician asks a volunteer to choose a card, and to tell him which row that card is in.

Finally, the magician arranges the 16 cards in a square grid again, possibly in a different order. Once again, he asks the volunteer which row her card is in. With only the answers to these two questions, the magician then correctly determines which card the volunteer chose. Amazing, right?

You decide to write a program to help you understand the magician's technique. The program will be given the two arrangements of the cards, and the volunteer's answers to the two questions: the row number of the selected card in the first arrangement, and the row number of the selected card in the second arrangement. The rows are numbered 1 to 4 from top to bottom.

Your program should determine which card the volunteer chose; or if there is more than one card the volunteer might have chosen (the magician did a bad job); or if there's no card consistent with the volunteer's answers (the volunteer cheated).

Solving this problem

Usually, Google Code Jam problems have 1 Small input and 1 Large input. This problem has only 1 Small input. Once you have solved the Small input, you have finished solving this problem.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with a line containing an integer: the answer to the first question. The next 4 lines represent the first arrangement of the cards: each contains 4 integers, separated by a single space. The next line contains the answer to the second question, and the following four lines contain the second arrangement in the same format.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1).

If there is a single card the volunteer could have chosen, y should be the number on the card. If there are multiple cards the volunteer could have chosen, y should be "Bad magician!", without the quotes. If there are no cards consistent with the volunteer's answers, y should be "Volunteer cheated!", without the quotes. The text needs to be exactly right, so consider copying/pasting it from here.

Limits

1 ≤ T ≤ 100.
1 ≤ both answers ≤ 4.
Each number from 1 to 16 will appear exactly once in each arrangement.

Sample


Input 
 
Output 
 
321 2 3 45 6 7 89 10 11 1213 14 15 1631 2 5 43 11 6 159 10 7 1213 14 8 1621 2 3 45 6 7 89 10 11 1213 14 15 1621 2 3 45 6 7 89 10 11 1213 14 15 1621 2 3 45 6 7 89 10 11 1213 14 15 1631 2 3 45 6 7 89 10 11 1213 14 15 16
Case #1: 7Case #2: Bad magician!Case #3: Volunteer cheated!

解题报告:

题意很简单,再第一次所报的行中的数是否在第二次报的行中存在。只存在一个输出这个数字。多个输出Bad magician!。没有输出Volunteer cheated!。代码如下

#include <stdio.h>int s1[5][5];int s2[5][5];int main(){    //freopen("fun.in","r",stdin);    //freopen("fun.out","w",stdout);    int t;    int a,b,ans,w;    scanf("%d",&t);    for(int i=1;i<=t;i++){        scanf("%d",&a);        for(int j=1;j<=4;j++)            for(int k=1;k<=4;k++)                scanf("%d",&s1[j][k]);        scanf("%d",&b);        for(int j=1;j<=4;j++)            for(int k=1;k<=4;k++)                scanf("%d",&s2[j][k]);        ans=0;        for(int j=1;j<=4;j++)            for(int k=1;k<=4;k++)                if(s1[a][j]==s2[b][k]){                    ans++;                    w=k;                }        if(ans==0)            printf("Case #%d: Volunteer cheated!\n",i);        else if(ans==1)            printf("Case #%d: %d\n",i,s2[b][w]);        else            printf("Case #%d: Bad magician!\n",i);    }    return 0;}


Problem B. Cookie Clicker Alpha

Introduction

Cookie Clicker is a Javascript game by Orteil, where players click on a picture of a giant cookie. Clicking on the giant cookie gives them cookies. They can spend those cookies to buy buildings. Those buildings help them get even more cookies. Like this problem, the game is very cookie-focused. This problem has a similar idea, but it does not assume you have played Cookie Clicker. Please don't go play it now: it might be a long time before you come back.

Problem

In this problem, you start with 0 cookies. You gain cookies at a rate of 2 cookies per second, by clicking on a giant cookie. Any time you have at least C cookies, you can buy a cookie farm. Every time you buy a cookie farm, it costs you C cookies and gives you an extra F cookies per second.

Once you have X cookies that you haven't spent on farms, you win! Figure out how long it will take you to win if you use the best possible strategy.

Example

Suppose C=500.0, F=4.0 and X=2000.0. Here's how the best possible strategy plays out:

  1. You start with 0 cookies, but producing 2 cookies per second.
  2. After 250 seconds, you will have C=500 cookies and can buy a farm that producesF=4 cookies per second.
  3. After buying the farm, you have 0 cookies, and your total cookie production is 6 cookies per second.
  4. The next farm will cost 500 cookies, which you can buy after about 83.3333333seconds.
  5. After buying your second farm, you have 0 cookies, and your total cookie production is 10 cookies per second.
  6. Another farm will cost 500 cookies, which you can buy after 50 seconds.
  7. After buying your third farm, you have 0 cookies, and your total cookie production is 14 cookies per second.
  8. Another farm would cost 500 cookies, but it actually makes sense not to buy it: instead you can just wait until you have X=2000 cookies, which takes about142.8571429 seconds.
Total time: 250 + 83.3333333 + 50 + 142.8571429 = 526.1904762 seconds.

Notice that you get cookies continuously: so 0.1 seconds after the game starts you'll have 0.2 cookies, and π seconds after the game starts you'll have 2π cookies.

Input

The first line of the input gives the number of test cases, TT lines follow. Each line contains three space-separated real-valued numbers: CF and X, whose meanings are described earlier in the problem statement.

CF and X will each consist of at least 1 digit followed by 1 decimal point followed by from 1 to 5 digits. There will be no leading zeroes.

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 seconds it takes before you can have X delicious cookies.

We recommend outputting y to 7 decimal places, but it is not required. y will be considered correct if it is close enough to the correct number: within an absolute or relative error of 10-6. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ C ≤ 500.
1 ≤ F ≤ 4.
1 ≤ X ≤ 2000.

Large dataset

1 ≤ C ≤ 10000.
1 ≤ F ≤ 100.
1 ≤ X ≤ 100000.

Sample


Input 
 
Output 
 
430.0 1.0 2.030.0 2.0 100.030.50000 3.14159 1999.19990500.0 4.0 2000.0
Case #1: 1.0000000Case #2: 39.1666667Case #3: 63.9680013Case #4: 526.1904762

Note

Cookie Clicker was created by Orteil. Orteil does not endorse and has no involvement with Google Code Jam.


解题报告
题意:刚开始时玩家每秒钟可收益2 cookies。当有C个cookies时就可以花去C个cookies,然后每秒收益在原来的基础上增加F。当cookies达到X时玩家胜利。统计所需时间。

解法:首先将时间等于X。然后计算能买到C所需要的时间和直接到达X所需要的时间。看是否比原来的小。然后用买了一次C的收益去算到达X需要多少时间(需要加上第一次买C的时间),再次比较是否比原来的少。依次类推,当比原来的大的时候那么最短时间就算出来了。代码如下:

#include <stdio.h>int main(){    //freopen("c1.in","r",stdin);    //freopen("c1.out","w",stdout);    double c,f,x,cur,sum,s,ans;    int t,flag;    scanf("%d",&t);    for(int i=1;i<=t;i++){        scanf("%lf%lf%lf",&c,&f,&x);        ans=x;flag=1;s=2;cur=0;        while(flag){            sum=x/s+cur;            cur=c/s+cur;            s=s+f;            if(ans>sum)                ans=sum;            else                flag=0;        }        printf("Case #%d: %.7lf\n",i,ans);    }    return 0;}


Problem D. Deceitful War


This problem is the hardest problem to understand in this round. If you are new to Code Jam, you should probably try to solve the other problems first.

Problem

Naomi and Ken sometimes play games together. Before they play, each of them gets Nidentical-looking blocks of wood with masses between 0.0kg and 1.0kg (exclusive). All of the blocks have different weights. There are lots of games they could play with those blocks, but they usually play something they call War. Here is how War works:

  1. Each player weighs each of his or her own blocks, so each player knows the weights of all of his or her own blocks, but not the weights of the other player's blocks.
  2. They repeat the following process N times:
    1. Naomi chooses one of her own blocks, with mass ChosenNaomi.
    2. Naomi tells Ken the mass of the block she chose.
    3. Ken chooses one of his own blocks, with mass ChosenKen.
    4. They each put their block on one side of a balance scale, and the person whose block is heavier gets one point.
    5. Both blocks are destroyed in a fire.

Naomi has realized three things about War. First, she has realized that she loses a lot. Second, she has realized that there is a unique strategy that Ken can follow to maximize his points without assuming anything about Naomi's strategy, and that Ken always uses it. Third, she has realized that she hates to lose. Naomi has decided that instead of playing War, she will play a game she calls Deceitful War. The great thing about Deceitful War is that Ken will think they're playing War!

Here is how Deceitful War works, with differences between Deceitful War and War in bold:

  1. Each player weighs each of his or her own blocks. Naomi also weighs Ken's blocks while he isn't looking, so Naomi knows the weights of all blocks and Ken only knows the weights of his own blocks.
  2. They repeat the following process N times:
    1. Naomi chooses one of her own blocks, with mass ChosenNaomi.
    2. Naomi tells Ken a number, ToldNaomi, between 0.0kg and 1.0kg exclusive. Ken, who thinks they're playing War, thinks the number Naomi just told him is ChosenNaomi.
    3. Ken chooses one of his own blocks, with mass ChosenKen.
    4. They each put their block on one side of a balance scale, and the person whose block is heavier gets one point.
    5. Both blocks are destroyed in a fire.

Naomi doesn't want Ken to know that she isn't playing War; so when she is choosing which block to play, and what mass to tell Ken, she must make sure that the balance scale won't reveal that ChosenNaomi ≠ ToldNaomi. In other words, she must make decisions so that:

  • ChosenNaomi > ChosenKen if, and only if, ToldNaomi > ChosenKen, and
  • ToldNaomi is not equal to the mass of any of Ken's blocks, because he knows that isn't possible.

It might seem like Naomi won't win any extra points by being deceitful, because Ken might discover that she wasn't playing War; but Naomi knows Ken thinks both players are playing War, and she knows what he knows, and she knows Ken will always follow his unique optimal strategy for War, so she can always predict what he will play.

You'll be given the masses of the blocks Naomi and Ken started with. Naomi will play Deceitful War optimally to gain the maximum number of points. Ken will play War optimally to gain the maximum number of points assuming that both players are playing War. What will Naomi's score be? What would it have been if she had played War optimally instead?

Examples

If each player has a single block left, where Naomi has 0.5kg and Ken has 0.6kg, then Ken is guaranteed to score the point. Naomi can't say her number is ≥ 0.6kg, or Ken will know she isn't playing War when the balance scale shows his block was heavier.

If each player has two blocks left, where Naomi has [0.7kg, 0.2kg] and Ken has [0.8kg, 0.3kg], then Naomi could choose her 0.2kg block, and deceive Ken by telling him that she chose a block that was 0.6kg. Ken assumes Naomi is telling the truth (as in how the War game works) and will play his 0.8kg block to score a point. Ken was just deceived, but he will never realize it because the balance scale shows that his 0.8kg block is, like he expected, heavier than the block Naomi played. Now Naomi can play her 0.7kg block, tell Ken it is 0.7kg, and score a point. If Naomi had played War instead of Deceitful War, then Ken would have scored two points and Naomi would have scored zero.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with a line containing a single integer N, the number of blocks each player has. Next follows a line containing N space-separated real numbers: the masses of Naomi's blocks, in kg. Finally there will be a line containing N space-separated real numbers: the masses of Ken's blocks, in kg.

Each of the masses given to Ken and Naomi will be represented as a 0, followed by a decimal point, followed by 1-5 digits. Even though all the numbers in the input have 1-5 digits after the decimal point, Ken and Naomi don't know that; so Naomi can still tell Ken that she played a block with mass 0.5000001kg, and Ken has no reason not to believe her.

Output

For each test case, output one line containing "Case #xy z", where x is the test case number (starting from 1), y is the number of points Naomi will score if she plays Deceitful War optimally, and z is the number of points Naomi will score if she plays War optimally.

Limits

1 ≤ T ≤ 50.
All the masses given to Ken and Naomi are distinct, and between 0.0 and 1.0 exclusive.

Small dataset

1 ≤ N ≤ 10.

Large dataset

1 ≤ N ≤ 1000.

Sample


Input 
  
410.50.620.7 0.20.8 0.330.5 0.1 0.90.6 0.4 0.390.186 0.389 0.907 0.832 0.959 0.557 0.300 0.992 0.8990.916 0.728 0.271 0.520 0.700 0.521 0.215 0.341 0.458


Output 
 
Case #1: 0 0Case #2: 1 0Case #3: 2 1Case #4: 8 4

解题报告:

此题真长,解法是:
当是Deceitful War optimally时。用Naomi最大的去比Ken最大的,如果比Ken最大的小,那么就用最小的去和Ken比。然后比较Ken第二大的,当比Ken的大就拿这个和Ken去比,否则拿一个第二个最小的和Ken比。依次类推。

当是War optimally时,就相当于Ken采取上面那种方式获得的最大得分,那么Naomi的得分就是N减去Ken的得分。代码如下:

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;double s1[1010],s2[1010];bool cmp(double a,double b){    return a>b;}int main(){    //freopen("d1.in","r",stdin);    //freopen("d1.out","w",stdout);    int t,n,ans1,ans2;    scanf("%d",&t);    for(int i=1;i<=t;i++){        ans1=0;ans2=0;        scanf("%d",&n);        for(int j=0;j<n;j++)            scanf("%lf",&s1[j]);        for(int j=0;j<n;j++)            scanf("%lf",&s2[j]);        sort(s1,s1+n,cmp);        sort(s2,s2+n,cmp);        int k=0;        for(int j=0;j<n;j++){            while(k<n){                if(s1[j]>s2[k]){                    ans1++;                    k++;                    break;                }                k++;            }        }        k=0;        for(int j=0;j<n;j++){            while(k<n){                if(s2[j]>s1[k]){                    ans2++;                    k++;                    break;                }                k++;            }        }        printf("Case #%d: %d %d\n",i,ans1,n-ans2);    }    return 0;}


0 0