Codeforces Round #290 (Div. 2)

54 篇文章 0 订阅
53 篇文章 0 订阅

比赛链接: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");
}



C. Fox And Names
time limit per test2 seconds
memory limit per test256 megabytes


Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.

After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!

She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.

Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and ti according to their order in alphabet.

Input
The first line contains an integer n (1 ≤ n ≤ 100): number of names.

Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.

Output
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).

Otherwise output a single word "Impossible" (without quotes).

Sample test(s)
input

3
rivest
shamir
adleman

output
bcdefghijklmnopqrsatuvwxyz

input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer

output
Impossible

input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever

output
aghjlnopefikdmbcqrstuvwxyz

input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck

output
acbdefhijklmnogpqrstuvwxyz

题目大意,给你一些字符串,问如果它们是按“字典序”排序的,给出它们所按的字典序的定义,若不存在则输出impossible


题目分析:其实这个所谓的字典序就是离散数学上的一组偏序关系,满足自反性,反对称性和传递性,我们可以用hash[a][b]来表示字符a的字典序在字符b之前,因为数据量不大,我们可以两两比较,得到偏序集合,按照偏序关系确立优先级


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char s[105][105];
bool hash[30][30];
struct CH
{
    int id, cnt;
}c[30];

int cmp(CH a, CH b)
{
    return a.cnt > b.cnt;
}

int main()
{
    int n, pos;
    memset(hash, false, sizeof(hash));
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%s", s[i]);
    for(int i = 0; i < n; i++)
    {
        int l1 = strlen(s[i]);
        for(int j = i + 1; j < n; j++)
        {
            int l2 = strlen(s[j]);
            pos = 0;
            while(pos < l1 && pos < l2 && s[i][pos] == s[j][pos])
                pos++;
            if((pos == l1 || pos == l2))
            {
                if(l1 > l2)
                {
                    printf("Impossible\n");
                    return 0;
                }
                else
                    continue;
            }
            else if(hash[s[j][pos] - 'a'][s[i][pos] - 'a']) //反对称性
            {
                printf("Impossible\n");
                return 0;
            }
            else
                hash[s[i][pos]-'a'][s[j][pos]-'a'] = true;
        }
    }
    for(int i = 0; i < 26; i++)
    {
        for(int j = 0; j < 26; j++)
        {
            for(int k = 0; k < 26; k++)
            {
                if(hash[i][k] && hash[k][j])
                {
                    if(hash[j][i])  //反对称性
                    {
                        printf("Impossible\n");
                        return 0;
                    }
                    hash[i][j] = true;  //传递性
                }
            }
        }
    }
    for(int i = 0; i < 26; i++)
    {
        c[i].id = i;
        c[i].cnt = 0;
    }
    for(int i = 0; i < 26; i++)
        for(int j = 0; j < 26; j++)
            if(hash[i][j]) //计算字符i的优先级
                c[i].cnt++;
    sort(c, c + 26, cmp); //按优先级从大到小排序
    for(int i = 0; i <= 25; i++)
        printf("%c", c[i].id + 'a');
    printf("\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");
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值