码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #290 (Div. 2)

时间:2015-02-03 17:23:56      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:codeforces

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


A. Fox And Snake
time limit per test 2 seconds
memory limit per test 256 megabytes


Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.

A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r,?c). The tail of the snake is located at (1,?1), then it‘s body extends to (1,?m), then goes down 2 rows to (3,?m), then goes left to (3,?1) and so on.

Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters (‘.‘) and the snake cells should be filled with number signs (‘#‘).

Consider sample tests in order to understand the snake pattern.

Input
The only line contains two integers: n and m (3?≤?n,?m?≤?50).

n is an odd number.

Output
Output n lines. Each line should contain a string consisting of m characters. Do not output spaces.

Sample test(s)
input
3 3
output
###
..#
###
input
3 4
output
####
...#
####
input
5 3
output
###
..#
###
#..
###
input
9 9
output
#########
........#
#########
#........
#########
........#
#########
#........
#########


题目大意:如题,按要求打印

题目分析:奇数行全#,能被4整除的偶数行先#,否则先.


#include <cstdio>

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(i % 2)
                printf("#");
            else if(i % 4 == 0)
            {
                if(j == 1)
                    printf("#");
                else
                    printf(".");
            }
            else
            {
                if(j == m)
                    printf("#");
                else
                    printf(".");
            }
        }
        printf("\n");
    }
}



B. Fox And Two Dots
time limit per test 2 seconds
memory limit per test 256 megabytes


Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m cells, like this:


Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.

The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1,?d2,?...,?dk a cycle if and only if it meets the following condition:

These k dots are different: if i?≠?j then di is different from dj.
k is at least 4.
All dots belong to the same color.
For all 1?≤?i?≤?k?-?1: di and di?+?1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.
Determine if there exists a cycle on the field.

Input
The first line contains two integers n and m (2?≤?n,?m?≤?50): the number of rows and columns of the board.

Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.

Output
Output "Yes" if there exists a cycle, and "No" otherwise.

Sample test(s)
input
3 4
AAAA
ABCA
AAAA

output
Yes

input
3 4
AAAA
ABCA
AADA

output
No

input
4 4
YYYR
BYBY
BBBY
BBBY

output
Yes

input
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB

output
Yes

input
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ

output
No

Note
In first sample test all ‘A‘ form a cycle.

In second sample there is no such cycle.

The third sample is displayed on the picture above (‘Y‘ = Yellow, ‘B‘ = Blue, ‘R‘ = Red).


题目大意:输入一个矩阵,判断会不会出现一个环


题目分析:我的做法感觉有点烦了,从一个点开始DFS,走过的标记为true,当step大于等于3(因为要成一个环至少得4个字符)并且此时的位置在起点的上下左右中的任意一个上面则说明找到环,还加了个感觉没什么用的剪枝,就是起点的上下左右的字符都与它本身不相同时,这个点可以不用搜


#include <cstdio>
#include <cstring>
char map[55][55];
bool vis[55][55], no[55][55], flag;
int n, m, sx, sy, ma, ex, ey;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

bool check(int x, int y)
{
    if(x + 1 == sx && y == sy)
        return true;
    if(x - 1 == sx && y == sy)
        return true;
    if(x == sx && y + 1 == sy)
        return true;
    if(x == sx && y - 1 == sy)
        return true;
    return false;
}

void DFS(int x, int y, int step)
{
    if(flag)
        return;
    if(step >= 3 && check(x, y))
    {
        flag = true;
        return;
    }
    for(int i = 0; i < 4; i++)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if(xx < 0 || yy < 0 || xx >= n || yy >= m || map[xx][yy] != map[sx][sy] || vis[xx][yy])
            continue;
        vis[xx][yy] = true;
        DFS(xx, yy, step + 1);
        vis[xx][yy] = false;
    }
    return;
}

bool ok(int x, int y)
{
    if((x + 1 < n && map[x][y] != map[x + 1][y]) &&
       (x - 1 >= 0 && map[x][y] != map[x - 1][y]) &&
       (y + 1 < m && map[x][y] != map[x][y + 1]) &&
       (y - 1 >= 0 && map[x][y] != map[x][y - 1]))
        return false;
    return true;
}

int main()
{
    flag = false;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++)
        scanf("%s", map[i]);
    memset(vis, false, sizeof(vis));
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            ma = -1;
            if(ok(i, j))
            {
                sx = i;
                sy = j;
                vis[sx][sy] = true;
                DFS(sx, sy, 0);
                vis[sx][sy] = false;
            }
            if(flag)
                break;
        }
        if(flag)
            break;
    }
    if(flag)
        printf("Yes\n");
    else
        printf("No\n");
}



D. Fox And Jumping
time limit per test2 seconds
memory limit per test256 megabytes


Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.

There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x?-?li) or cell (x?+?li).

She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.

If this is possible, calculate the minimal cost.

Input
The first line contains an integer n (1?≤?n?≤?300), number of cards.

The second line contains n numbers li (1?≤?li?≤?109), the jump lengths of cards.

The third line contains n numbers ci (1?≤?ci?≤?105), the costs of cards.

Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.

Sample test(s)
input
3
100 99 9900
1 1 1

output
2

input
5
10 20 30 40 50
1 1 1 1 1

output
-1

input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10

output
6

input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026

output
7237

Note
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can‘t jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.

In the second sample test, even if you buy all cards, you can‘t jump to any cell whose index is not a multiple of 10, so you should output -1.


题目大意:在一个无限长的数轴上从原点开始,你可以买一些卡片,这些卡片的数字代表你可以向正方向或者负方向跳 l[i] 距离,买卡片对应的花费为 c[i],现在问有没有一种方案可以使你可以到达数轴上的任意一个点,输出最小花费。


题目分析:很有趣的一道题,也不是很难,可是不明白为什么过的人不多,因为数轴是无限长的,所以可以把问题转化为在所给的卡片里我们是否能通过它们得到数字1,如果可以得到数字1则说明可以走到数轴上任意地方,如果有多种方案取花费最少的并输出花费,这样问题就清楚了,比如100和99,我们可以向前走100步再后退99,相当于前进1步,后退一步只要反过来就行了,我们可以发现,对于任意两个数字a,b我们可以向前或退后任意gcd(a, b)的倍数,我们把gcd(a, b)当作它的基数,则只要存在gcd(a,b)等于1则说明问题有解,我们可以把所有的基数放在一个集合(这里用map很方便)中,不断的任意取他们的gcd再产生新的基数,知道所有情况都出现,至于最小花费,若有多种方案得到1,取最小


#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
int l[305], c[305];
map<int, int> mp;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;  
}

int main() 
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d", &l[i]);
    for(int i = 0; i < n; i++)
        scanf("%d", &c[i]);
    mp[0] = 0;
    for(int i = 0; i < n; i++)
    {
        for(map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
        {
            int num = gcd(l[i], it -> first);
            if(mp.count(num))
                mp[num] = min(mp[num], c[i] + it -> second);
            else
                mp[num] = c[i] + it -> second;
        }   
    }
    if(mp.count(1))
        printf("%d\n", mp[1]);
    else
        printf("-1\n");
}


Codeforces Round #290 (Div. 2)

标签:codeforces

原文地址:http://blog.csdn.net/tc_to_top/article/details/43449559

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!