您的位置:首页 > 其它

Codeforces Round #292 (Div. 2)C,D拓扑排序

2015-02-25 16:51 232 查看
C. Drazil and Factorial
Drazil is playing a math game with Varda.

Let's define

for positive integer
x as a product of factorials of its digits. For example,


.

First, they choose a decimal number a consisting of
n digits that contains at least one digit larger than
1. This number may possibly start with leading zeroes. Then they should find maximum positive number
x satisfying following two conditions:

1. x doesn't contain neither digit
0 nor digit 1.

2.

=


.

Help friends find such number.

Input
The first line contains an integer n (1 ≤ n ≤ 15) — the number of digits in
a.

The second line contains n digits of
a. There is at least one digit in a that is larger than
1. Number a may possibly contain leading zeroes.

Output
Output a maximum possible integer satisfying the conditions above. There should be no zeroes and ones in this number decimal representation.

Sample test(s)

Input
4
1234


Output
33222


Input
3
555


Output
555


Note
In the first case,


思路:因为要数字尽量大,所以尽量把每个数拆分成尽量多的位,然后排序就行了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=20;
char a[maxn][maxn]={"","","2","3","322","5","53","7","7222","7332"};
int N;
char str[maxn];
char ans[maxn*maxn];
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        scanf("%s",str);
        int n=strlen(str);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            int len=strlen(a[str[i]-'0']);
            for(int j=0;j<len;j++)
                ans[cnt++]=a[str[i]-'0'][j];
        }
        ans[cnt]=0;
        sort(ans,ans+cnt,greater<char>() );
        cout<<ans<<endl;
    }
    return 0;
}


D. Drazil and Tiles
Drazil created a following problem about putting 1 × 2 tiles into an
n × m grid:

"There is a grid with some cells that are empty and some cells that are occupied. You should use
1 × 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it."

But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution
when it exists and it is unique? Otherwise contestant may print 'Not unique' ".

Drazil found that the constraints for this task may be much larger than for the original task!

Can you solve this new problem?

Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.

Input
The first line contains two integers n and
m (1 ≤ n, m ≤ 2000).

The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.

Output
If there is no solution or the solution is not unique, you should print the string "Not unique".

Otherwise you should print how to cover all empty cells with
1 × 2 tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

Sample test(s)

Input
3 3
...
.*.
...


Output
Not unique


Input
4 4
..**
*...
*.**
....


Output
<>**
*^<>
*v**
<><>


Input
2 4
*..*
....


Output
*<>*
<><>


Input
1 1
.


Output
Not unique


Input
1 1
*


Output
*


Note
In the first case, there are indeed two solutions:

<>^
^*v
v<>

and

^<>
v*^
<>v

so the answer is "Not unique".

题意:把点变成^,v,<,>使相应的能够对应;

思路:拓扑排序,有几个跟当前点相邻的点,当前点的度就是几,然后把度为一的点先加入到队列中,然后进行拓扑排序,如果可以进行拓扑排序就唯一

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=2010;
char a[maxn][maxn];
int N,M;
queue<pair<int,int> > q;
int deg[maxn][maxn];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int cal(int x,int y)
{
    if(a[x][y]=='*')return 0;
    int cnt=0;
    for(int i=0;i<4;i++)
    {
        int tx=x+dx[i];
        int ty=y+dy[i];
        if(tx<0||tx>=N||ty<0||ty>=M||a[tx][ty]=='*')continue;
        cnt++;
    }
    return cnt;
}
char A[]={'^','<','v','>'};
char B[]={'v','>','^','<'};
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        for(int i=0;i<N;i++)scanf("%s",a[i]);
        while(!q.empty())q.pop();
        for(int i=0;i<N;i++)
            for(int j=0;j<M;j++)
            {
                deg[i][j]=cal(i,j);
                if(deg[i][j]==1)q.push(make_pair(i,j));
            }
        while(!q.empty())
        {
            pair<int,int> tmp=q.front();q.pop();
            int x=tmp.first,y=tmp.second;
            deg[x][y]=0;
            for(int i=0;i<4;i++)
            {
                int tx=x+dx[i];
                int ty=y+dy[i];
                if(tx<0||tx>=N||ty<0||ty>=M||deg[tx][ty]==0)continue;
                a[x][y]=A[i];
                a[tx][ty]=B[i];
                deg[tx][ty]=0;
                for(int j=0;j<4;j++)
                {
                    int ttx=tx+dx[j];
                    int tty=ty+dy[j];
                    if(ttx<0||ttx>=N||tty<0||tty>=M||deg[ttx][tty]==0)continue;
                    deg[ttx][tty]--;
                    if(deg[ttx][tty]==1)q.push(make_pair(ttx,tty));
                }
                break;
            }
        }
        bool flag=true;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<M;j++)
                if(a[i][j]=='.')
                {
                    flag=false;
                    break;
                }
            if(!flag)break;
        }
        if(!flag)printf("Not unique\n");
        else
            for(int i=0;i<N;i++)puts(a[i]);

        cout<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: