Google Code Jam 2017 资格赛

https://code.google.com/codejam/contest/3264486/dashboard#s=p3


Oversized Pancake Flipper

(1)问题描述:

Problem

Last year, the Infinite House of Pancakes introduced a new kind of pancake. It has a happy face made of chocolate chips on one side (the "happy side"), and nothing on the other side (the "blank side").
You are the head cook on duty. The pancakes are cooked in a single row over a hot surface. As part of its infinite efforts to maximize efficiency, the House has recently given you an oversized pancake flipper that flips exactly K consecutive pancakes. That is, in that range of K pancakes, it changes every happy-side pancake to a blank-side pancake, and vice versa; it does not change the left-to-right order of those pancakes.
You cannot flip fewer than K pancakes at a time with the flipper, even at the ends of the row (since there are raised borders on both sides of the cooking surface). For example, you can flip the first K pancakes, but not the first K - 1 pancakes.
Your apprentice cook, who is still learning the job, just used the old-fashioned single-pancake flipper to flip some individual pancakes and then ran to the restroom with it, right before the time when customers come to visit the kitchen. You only have the oversized pancake flipper left, and you need to use it quickly to leave all the cooking pancakes happy side up, so that the customers leave feeling happy with their visit.
Given the current state of the pancakes, calculate the minimum number of uses of the oversized pancake flipper needed to leave all pancakes happy side up, or state that there is no way to do it.
Input

The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with a string S and an integer K. S represents the row of pancakes: each of its characters is either + (which represents a pancake that is initially happy side up) or - (which represents a pancake that is initially blank side up).
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 IMPOSSIBLE if there is no way to get all the pancakes happy side up, or an integer representing the the minimum number of times you will need to use the oversized pancake flipper to do it.
Limits

1 ≤ T ≤ 100.
Every character in S is either + or -.
2 ≤ K ≤ length of S.
Small dataset

2 ≤ length of S ≤ 10.
Large dataset

2 ≤ length of S ≤ 1000.
Sample


Input 
 	
Output 
 
3
---+-++- 3
+++++ 4
-+-+- 4

Case #1: 3
Case #2: 0
Case #3: IMPOSSIBLE
In Case #1, you can get all the pancakes happy side up by first flipping the leftmost 3 pancakes, getting to ++++-++-, then the rightmost 3, getting to ++++---+, and finally the 3 pancakes that remain blank side up. There are other ways to do it with 3 flips or more, but none with fewer than 3 flips.
In Case #2, all of the pancakes are already happy side up, so there is no need to flip any of them.
In Case #3, there is no way to make the second and third pancakes from the left have the same side up, because any flip flips them both. Therefore, there is no way to make all of the pancakes happy side up.

(2)要点:从最左边起,每一个‘-‘’都需要变成'+'

(3)代码:

#include <stdio.h>
#include <string.h>

int main()
{
	static const size_t buff_size = 1000;
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		char buff[buff_size+1] = { 0 };
		unsigned int k = 0,ans = 0;
		scanf("%s%d",buff,&k);
		for(size_t i = 0,len = strlen(buff);i + k <= len;++i)
		{
			if(buff[i] == '+') continue;
			++ ans;
			for(size_t j = 0;j < k;++j) buff[i+j] = '+' + '-' - buff[i+j];
		}
		for(size_t i = 0,len = strlen(buff);i < len;++i)
		{
			if(buff[i] == '-')
			{
				ans = (unsigned int)(-1);
				break;
			}
		}

		printf("Case #%d: ",iCases);
		if((unsigned int)(-1) == ans) printf("IMPOSSIBLE\n");
		else printf("%d\n",ans);
	}
	return 0;
}



Tidy Numbers

(1)问题描述:

Problem

Tatiana likes to keep things tidy. Her toys are sorted from smallest to largest, her pencils are sorted from shortest to longest and her computers from oldest to newest. One day, when practicing her counting skills, she noticed that some integers, when written in base 10 with no leading zeroes, have their digits sorted in non-decreasing order. Some examples of this are 8, 123, 555, and 224488. She decided to call these numbers tidy. Numbers that do not have this property, like 20, 321, 495 and 999990, are not tidy.
She just finished counting all positive integers in ascending order from 1 to N. What was the last tidy number she counted?
Input

The first line of the input gives the number of test cases, T. T lines follow. Each line describes a test case with a single integer N, the last number counted by Tatiana.
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 last tidy number counted by Tatiana.
Limits

1 ≤ T ≤ 100.
Small dataset

1 ≤ N ≤ 1000.
Large dataset

1 ≤ N ≤ 1018.
Sample


Input 
 	
Output 
 
4
132
1000
7
111111111111111110

Case #1: 129
Case #2: 999
Case #3: 7
Case #4: 99999999999999999

Note that the last sample case would not appear in the Small dataset.

(2)要点:从最低位开始,将其变成v - 1,后面变成9,如果变化后的不满足条件也往前一位尝试

(3)代码:

#include <stdio.h>
#include <string.h>
#include <assert.h>

template<class data_t> inline data_t str_to_int(const unsigned int* digit,size_t size)
{
    data_t num = 0;
    for(size_t i = size - 1;i != (size_t)(-1);--i)
    {
        num *= 10;
        num += digit[i];
    }
    return num;
} 

template<class data_t> inline size_t int_to_str(data_t num,unsigned int* digit,size_t size)
{
    memset(digit,0,sizeof(unsigned int)*size);
    size_t pos = 0;
    if(0 == num) ++ pos;
    for(;num > 0;num /= 10,++pos) digit[pos] = num%10;  
    return pos;
}

bool is_tidy(const unsigned int* digit,size_t size)
{
    for(size_t i = 0;i + 1 < size;++i)
    {
        if(digit[i] < digit[i+1]) return false;
    }
    return true;
}

int main()
{
    static const size_t buff_size = 100;
    unsigned int nCases = 0;scanf("%d",&nCases);
    for(unsigned int iCases = 1;iCases <= nCases;++iCases)
    {
        unsigned long long v = 0;scanf("%I64d",&v);

        unsigned int digit[buff_size] = { 0 },output[buff_size] = { 0 };
        size_t pos = int_to_str(v,digit,buff_size);

        unsigned long long ans = 0;
        if(is_tidy(digit,pos)) ans = v;
        else
        {
            for(size_t i = 0;i < pos;++i)
            {
                if(0 == digit[i]) continue;
                output[pos] = 0;
                for(size_t j = pos - 1;j != i;--j) output[j] = digit[j];
                output[i] = digit[i] - 1;
                for(size_t j = i - 1;j != (size_t)(-1);--j) output[j] = 9;
                if(is_tidy(output,pos))
                {
                    ans = str_to_int<unsigned long long>(output,pos);
                    break;
                }
            }
        }

        printf("Case #%u: %I64u\n",iCases,ans);
    }
    return 0;
}


Bathroom Stalls

(1)问题描述:

Problem

A certain bathroom has N + 2 stalls in a single row; the stalls on the left and right ends are permanently occupied by the bathroom guards. The other N stalls are for users.
Whenever someone enters the bathroom, they try to choose a stall that is as far from other people as possible. To avoid confusion, they follow deterministic rules: For each empty stall S, they compute two values LS and RS, each of which is the number of empty stalls between S and the closest occupied stall to the left or right, respectively. Then they consider the set of stalls with the farthest closest neighbor, that is, those S for which min(LS, RS) is maximal. If there is only one such stall, they choose it; otherwise, they choose the one among those where max(LS, RS) is maximal. If there are still multiple tied stalls, they choose the leftmost stall among those.
K people are about to enter the bathroom; each one will choose their stall before the next arrives. Nobody will ever leave.
When the last person chooses their stall S, what will be the values of max(LS, RS) and min(LS, RS) be?
Solving this problem

This problem has 2 Small datasets and 1 Large dataset. You must solve the first Small dataset before you can attempt the second Small dataset. You will be able to retry either of the Small datasets (with a time penalty). You will be able to make a single attempt at the Large, as usual, only after solving both Small datasets.
Input

The first line of the input gives the number of test cases, T. T lines follow. Each line describes a test case with two integers N and K, as described above.
Output

For each test case, output one line containing Case #x: y z, where x is the test case number (starting from 1), y is max(LS, RS), and z is min(LS, RS) as calculated by the last person to enter the bathroom for their chosen stall S.
Limits

1 ≤ T ≤ 100.
1 ≤ K ≤ N.
Small dataset 1

1 ≤ N ≤ 1000.
Small dataset 2

1 ≤ N ≤ 106.
Large dataset

1 ≤ N ≤ 1018.
Sample


Input 
 	
Output 
 
5
4 2
5 2
6 2
1000 1000
1000 1

Case #1: 1 0
Case #2: 1 0
Case #3: 1 1
Case #4: 0 0
Case #5: 500 499

In Case #1, the first person occupies the leftmost of the middle two stalls, leaving the following configuration (O stands for an occupied stall and . for an empty one): O.O..O. Then, the second and last person occupies the stall immediately to the right, leaving 1 empty stall on one side and none on the other.
In Case #2, the first person occupies the middle stall, getting to O..O..O. Then, the second and last person occupies the leftmost stall.
In Case #3, the first person occupies the leftmost of the two middle stalls, leaving O..O...O. The second person then occupies the middle of the three consecutive empty stalls.
In Case #4, every stall is occupied at the end, no matter what the stall choices are.
In Case #5, the first and only person chooses the leftmost middle stall.

(2)要点:归纳法总结规律

(3)代码:

#include <stdio.h>
#include <assert.h>

int main()
{
    static const size_t size = 64;
    unsigned long long exps[size] = { 1 };
    for(size_t i = 1;i < size;++i) exps[i] = exps[i-1]*2;
    unsigned int nCases = 0;scanf("%d",&nCases);
    for(unsigned int iCases = 1;iCases <= nCases;++iCases)
    {
        unsigned long long n = 0,p = 0,x = 0;scanf("%I64d%I64d",&n,&p);
        assert(p <= n && n < exps[size-1] && 0 != p);
        size_t k = size - 1;
        for(;p < exps[k];--k);
        x = p - exps[k];

        unsigned long long a = n%exps[k] + 1;
        unsigned long long b = exps[k] - a;

        unsigned long long ans = 0;
        if(x < a) ans = n/exps[k];
        else ans = n/exps[k] - 1;

        unsigned long long minans = 0,maxns = 0;
        if(ans&1) minans = ans/2,maxns = ans/2;
        else minans = ans/2 - 1,maxns = ans/2;

        printf("Case #%u: %I64u %I64u\n",iCases,maxns,minans);
    }
    return 0;
}



Fashion Show

(1)问题描述:

Problem

You are about to host a fashion show to show off three new styles of clothing. The show will be held on a stage which is in the most fashionable of all shapes: an N-by-N grid of cells.
Each cell in the grid can be empty (which we represent with a . character) or can contain one fashion model. The models come in three types, depending on the clothing style they are wearing: +, x, and the super-trendy o. A cell with a + or x model in it adds 1 style pointto the show. A cell with an o model in it adds 2 style points. Empty cells add no style points.
To achieve the maximum artistic effect, there are rules on how models can be placed relative to each other.
Whenever any two models share a row or column, at least one of the two must be a +.
Whenever any two models share a diagonal of the grid, at least one of the two must be an x.
Formally, a model located in row i0 and column j0 and a model located in row i1 and column j1 share a row if and only if i0 = i1, they share a column if and only if j0 = j1, and they share a diagonal if and only if i0 + j0 = i1 + j1 or i0 - j0 = i1 - j1.
For example, the following grid is not legal:
...
x+o
.+.
The middle row has a pair of models (x and o) that does not include a +. The diagonal starting at the + in the bottom row and running up to the o in the middle row has two models, and neither of them is an x.
However, the following grid is legal. No row, column, or diagonal violates the rules.
+.x
+x+
o..
Your artistic advisor has already placed M models in certain cells, following these rules. You are free to place any number (including zero) of additional models of whichever types you like. You may not remove existing models, but you may upgrade as many existing +and x models into o models as you wish, as long as the above rules are not violated.
Your task is to find a legal way of placing and/or upgrading models that earns the maximum possible number of style points.
Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with one line with two integers N and M, as described above. Then, Mmore lines follow; the i-th of these lines has a +, x, or o character (the type of the model) and two integers Ri and Ci (the position of the model). The rows of the grid are numbered 1 through N, from top to bottom. The columns of the grid are numbered 1 through N, from left to right.
Output

For each test case, first output one line containing Case #x: y z, where x is the test case number (starting from 1), y is the number of style points earned in your arrangement, and z is the total number of models you have added and/or substituted in. Then, for each model that you have added or substituted in, output exactly one line in exactly the same format described in the Input section, where the character is the type of the model that you have added or substituted in. These z lines can be in any order.
If there are multiple valid answers, you may output any one of them.
Limits

1 ≤ T ≤ 100.
1 ≤ N ≤ 100.
1 ≤ Ci ≤ N, for all i.
0 ≤ M ≤ N2.
No two pre-placed models appear in the same cell.
It is guaranteed that the set of pre-placed models follows the rules.
Small dataset

Ri = 1, for all i. (Any models that are pre-placed are in the top row. Note that you may add/replace models in that row and/or add models in other rows.)
Large dataset

1 ≤ Ri ≤ N, for all i.
Sample


Input 
 	
Output 
 
3
2 0
1 1
o 1 1
3 4
+ 2 3
+ 2 1
x 3 1
+ 2 2

Case #1: 4 3
o 2 2
+ 2 1
x 1 1
Case #2: 2 0
Case #3: 6 2
o 2 3
x 1 2

The sample output displays one set of answers to the sample cases. Other answers may be possible. Note that the last sample case would not appear in the Small dataset.
In sample case #1, the grid is 2-by-2 and is initially blank. The output corresponds to the following grid. (In these explanations, we will use . to denote a blank cell.)
x.
+o
In sample case #2, the only cell is already occupied by an o model, and it is impossible to add a new model or replace the o model.
In sample case #3, the grid looks like this before you place any models:
...
+++
x..
The output corresponds to this grid:
.x.
++o
x..

(2)要点:

(3)代码:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值