现在的位置: 首页 > 综合 > 正文

Codeforces Round #290 (Div. 2)

2019年08月13日 ⁄ 综合 ⁄ 共 9821字 ⁄ 字号 评论关闭

A题:http://codeforces.com/contest/510/problem/A

A. Fox And Snake
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
#########
........#
#########
#........
#########
........#
#########
#........
#########

分析:从第一行开始,奇数行都用'#'填充就行,偶数行则根据i/2是奇数还是偶数来判断来填充。

代码:

#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include<map>
using namespace std;
typedef long long ll;
int n,m,k;
const int N=310;
char arr[N][N];

int main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
        {
            if(i&1)
            {
                for(int j=1;j<=m;j++)
                    cout<<"#";
                cout<<endl;
            }
            else
            {
                int t = i/2;
                if(t&1)
                {
                    for(int j=1;j<=m-1;j++)
                        cout<<".";
                    cout<<"#"<<endl;
                }
                else
                {
                    cout<<"#";
                    for(int j=1;j<=m-1;j++)
                        cout<<".";
                    cout<<endl;
                }
            }
        }
    }
    return 0;
}

 B题:http://codeforces.com/contest/510/problem/B

B. Fox And Two Dots
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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:

  1. These k dots are different: if i ≠ j then di is
    different from dj.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1di 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(),要是一直根据相同颜色dfs下去,如果碰到了访问过的某个节点,那么就说明必然有相同颜色的一个圈存在,要注意的是,在dfs()过程中,下一步是不能再回到上一步的那个节点的。

代码:

#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include<map>
using namespace std;
typedef long long ll;
int n,m,k;
const int N=310;
char arr[N][N];
bool flag = false;
bool visit[N][N];
int dirx[]={1,0,-1,0};
int diry[]={0,1,0,-1};
void dfs(int i,int j,int pi,int pj)
{
    if(visit[i][j])
    {
        flag = true;
        return;
    }
    visit[i][j]=true;
    for(int k=0;k<4;k++)
    {
        int nextx = i+dirx[k];
        int nexty = j+diry[k];
        if(nextx>=0&&nextx<n&&nexty>=0&&nexty<m&&arr[i][j]==arr[nextx][nexty])
        {
            if(!(nextx==pi&&nexty==pj))
                dfs(nextx,nexty,i,j);
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        flag = false;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                cin>>arr[i][j];
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                memset(visit,0,sizeof(visit));
                dfs(i,j,-1,-1);
            }
        if(flag)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

C题:http://codeforces.com/contest/510/problem/C

C. Fox And Names
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 inlexicographical 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 siand 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

分析:这道题看到之后就觉得是个拓扑排序嘛。。。。。然后发现自己不知道怎么写拓扑排序。。。。好在拓扑排序的思想非常简单,先构建一个有向图,a->b表示a有一条指向b的边,表明a必须在b的前面,然后在得到这些节点的顺序关系时,每次选取一个入度为0的节点,然后删除与该节点相邻的边,这样就能得到拓扑排序了。 当找不到入度为0的节点但是整个图还存在边的时候,就说明是impossible的,同时还要注意,如果abc在前,然后ab在后,这样的输入应该直接返回impossible.

代码:

#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
int n,k;
const int N=110;
string arr[N];
int indegree[N];
bool visited[N];
vector<int> edge[N];
int m,r,t;
void topsort()
{
    vector<int> ret;
    for(int i=0;i<26;i++)
    {
        if(indegree[i]==0&&edge[i].size()==0)
        {
            ret.push_back(i);
            visited[i]=true;
        }
    }
    
    while(true)
    {

        int i = 0;
        for(;i<26;i++)
        {
            if(indegree[i]==0&&!visited[i])
            {
                break;
            }
        }
        if(i<26)
        {
            ret.push_back(i);
            for(int j=0;j<edge[i].size();j++)
            {
                int t = edge[i][j];
                indegree[t]--;
            }
            edge[i].clear();
            visited[i]=true;
        }
        else
        {
            bool flag = false;
            for(int i=0;i<26;i++)
            {
                if(visited[i]==false)
                    flag = true;
            }
            if(flag)
            {
                cout<<"Impossible"<<endl;
                return;
            }
            else
                break;
        }
    }
    for(int i=0;i<ret.size();i++)
    {
        char c = ret[i]+'a';
        cout<<c;
    }
    cout<<endl;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>arr[i];
    memset(visited,0,sizeof(visited));
    for(int i=1;i<n;i++)
    {
        string sa = arr[i-1];
        string sb = arr[i];
        int a = -1;
        int b = -1;
        for(int j=0;j<min(sa.length(),sb.length());j++)
        {
            if(sa[j]==sb[j])
                continue;
            else
            {
                a = sa[j]-'a';
                b = sb[j]-'a';
                break;
            }
        }
        if(a==-1)
        {
            if(sa.length()>sb.length())
            {
                cout<<"Impossible"<<endl;
                return 0;
            }
            else
                continue;
        }
        indegree[b]++;
        edge[a].push_back(b);
    }
    topsort();
    return 0;
}

D题:http://codeforces.com/contest/510/problem/D

D. Fox And Jumping
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

分析:要能跳到所有点,那么必须得能得到1这个长度。然后分析几个例子看看,比如2和5,可以得到3,然后3和2又可以得到1,多分析几个,就能归纳出任给a,b两个数,我们能得到gcd(a,b). 题目意思要求最小代码,这题一看就不能用贪心什么的,所以只能往dp考虑 了(一般求最大值最小值都应该往这方面考虑考虑),那么考虑转移方程,最终结果要求最小代价,故应该写成f()=cost,剩下的就只有length这个变量了,故可以试试f(length)=cost这个方向,不难想到,对于给定的length,如有另一个length1,那么可以得到gcd(length,length1),那么转移方程就应该是f(gcd(a,b))
= min(f(gcd(a,b),f(a)+cost(b))

代码:

#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include<map>
using namespace std;
typedef long long ll;
int n,k;
const int N=310;
int l[N];
int c[N];
vector<int> edge[N];
int m,r,t;
map<int,int> dp;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>l[i];
    for(int i=0;i<n;i++)
        cin>>c[i];
    dp[0]=0;
    for(int i=0;i<n;i++)
    {
        map<int,int>::iterator it = dp.begin();
        for(;it!=dp.end();it++)
        {
            int t = gcd(l[i],it->first);
            if(dp.count(t))
                dp[t]  = min(dp[t],it->second+c[i]);
            else
                dp[t] = it->second+c[i];

        }
    }
    if(dp.count(1))
        cout<<dp[1]<<endl;
    else
        cout<<-1<<endl;
    return 0;
}

抱歉!评论已关闭.