codeforces510

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.

Example
Input
3 3
Output

#

..#

#

Input
3 4
Output

#

…#

#

Input
5 3
Output

#

..#

#

..

#

Input
9 9
Output

#

……..#

#

……..

#

……..#

#

……..

#

上来看这道题,很水,就秒切了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<new>
using namespace std;
bool pg[101][101];
int n,m;
bool flag=1;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        if(i%2==1) for(int j=1;j<=m;j++) cout<<'#';
        else
        {
            if(flag) 
            {
                for(int j=1;j<m;j++) cout<<'.';cout<<'#';
            }
            else
            {
                cout<<'#';for(int j=2;j<=m;j++) cout<<'.';
            }
            flag=flag^1;
        }
        cout<<endl;
    }
}

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.

Example
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).

B题,暴力深搜,一开始优化时数组开小了,剪枝没剪对

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 201
char xx[101][101];
int pg[128];
int n,m;
char c;
bool vis[201][201];
bool flag=0;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool dfs(int sx,int sy,int ix,int iy,char c,int len)
{
    vis[ix][iy]=1;
    if(flag) return 1;
    if(abs(ix-sx)+abs(iy-sy)==1&&len>=4) 
    {
        flag=1;return 1;
    }
    for(int i=0;i<4;i++)
    {
        int itx=dx[i]+ix,ity=dy[i]+iy;
        if((dx[i]+ix>n)||(dy[i]+iy>m)) continue;
        if(itx<1||ity<1) continue;
        if(vis[itx][ity]) continue;
        if(xx[itx][ity]==c)dfs(sx,sy,itx,ity,c,len+1);
    }
    vis[ix][iy]=0;
    return 0;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        scanf("%s",xx[i]+1);
    }
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    pg[xx[i][j]]++;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        if(pg[xx[i][j]]>=4) 
        {
            vis[i][j]=1;
            if(dfs(i,j,i,j,xx[i][j],1)) {puts("Yes");return 0;};
            vis[i][j]=0;
        }
    }
    puts("No");return 0;
}

An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, …, am, where ai is the number of details of the i-th type in this droid’s mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn’t have details of this type, nothing happens to it).

A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?

Input
The first line contains three integers n, m, k (1 ≤ n ≤ 105, 1 ≤ m ≤ 5, 0 ≤ k ≤ 109) — the number of droids, the number of detail types and the number of available shots, respectively.

Next n lines follow describing the droids. Each line contains m integers a1, a2, …, am (0 ≤ ai ≤ 108), where ai is the number of details of the i-th type for the respective robot.

Output
Print m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.

If there are multiple optimal solutions, print any of them.

It is not necessary to make exactly k shots, the number of shots can be less.

Example
Input
5 2 4
4 0
1 2
2 1
0 2
1 3
Output
2 2
Input
3 2 4
1 2
1 3
2 2
Output
1 3
Note
In the first test the second, third and fourth droids will be destroyed.

In the second test the first and second droids will be destroyed.
其实是可以线性跑一遍的,但我看保险,就跑n^2了。
细节是长度问题
topo——sort

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<new>
#include<queue>
using namespace std;
#define N 301
int n;
char xx[N][N];
int way[N][N];
int du[N];
char ans[N];
bool flag=0;
void cmp(int x,int y)
{
    if(flag) return;
    int lx=strlen(xx[x]+1),ly=strlen(xx[y]+1);
    int lk=min(lx,ly);
    for(int i=1;i<=min(lx,ly);i++)
    {
        if(i==lk&&xx[x][i]==xx[y][i])
        {
            if(lx>=ly)
            {
                flag=1;
                cout<<"Impossible";return;
            }
        }
        if(xx[x][i]==xx[y][i]) continue;
        else
        {
            if(!way[xx[x][i]][xx[y][i]])du[xx[y][i]]++;
            way[xx[x][i]][xx[y][i]]=1;
            break;
        }
    }
}
queue<char>q;
int tot=0;
bool vis[N];
void tpsort()
{
    if(flag) return;
    for(char i='a';i<='z';i++)
    {
        if(du[i]==0)
        {
            q.push(i);
        }
    }
    tot=0;
    while(!q.empty())
    {
        char ind=q.front();q.pop();tot++;
        ans[tot]=ind;
        for(int i='a';i<='z';i++)
        {
            if(way[ind][i])
            {
                du[i]--;
                if(!du[i]) q.push(i);
            }
        }
    }
    if(tot<26){cout<<"Impossible";return;return;}
    for(int i=1;i<=26;i++) putchar(ans[i]);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%s",xx[i]+1);
    for(int i=1;i<=n;i++)
    for(int j=i+1;j<=n;j++)
    cmp(i,j);
    tpsort();
}

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.

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

D题数论+BFS广搜DP,可以记忆化,所以就记忆化广搜之后,用MAP记一发就可以A了,
下面略证一下gcd=1的细节:
首先要覆盖整个数轴,要覆盖数轴的连续两个数,所以gcd为一
若当前gcd为n则能覆盖n的倍数
然后就排个序像背包一样跑一发即可。

//神奇的mapDP下次一定要会用BFS 
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<map>
using namespace std;
#define N 301
map<int,int>M;
int n;
int l[N],c[N];
map<int,int> ::iterator it;
int gcd(int x,int y)
{
    return y?gcd(y,x%y):x;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>l[i];
    for(int i=1;i<=n;i++) cin>>c[i];
    for(int i=1;i<=n;i++)
    {
        if(M[0]);
        for(it=M.begin();it!=M.end();it++)
        {
            int a=it->first;
            int b=it->second;
            int qq=gcd(l[i],a);
            int cc=b+c[i];
            if(!M.count(qq))M[qq]=cc;
            else M[qq]=min(M[qq],cc);
        }
    }
    if(M.count(1)) cout<<M[1];else cout<<-1;
}

Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.

They will have dinner around some round tables. You want to distribute foxes such that:

Each fox is sitting at some table.
Each table has at least 3 foxes sitting around it.
The sum of ages of any two adjacent foxes around each table should be a prime number.
If k foxes f1, f2, …, fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent.

If it is possible to distribute the foxes in the desired manner, find out a way to do that.

Input
The first line contains single integer n (3 ≤ n ≤ 200): the number of foxes in this party.

The second line contains n integers ai (2 ≤ ai ≤ 104).

Output
If it is impossible to do this, output “Impossible”.

Otherwise, in the first line output an integer m (): the number of tables.

Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers — indices of fox sitting around that table in clockwise order.

If there are several possible arrangements, output any of them.

Example
Input
4
3 4 8 9
Output
1
4 1 2 4 3
Input
5
2 2 2 2 2
Output
Impossible
Input
12
2 3 4 5 6 7 8 9 10 11 12 13
Output
1
12 1 2 3 6 5 12 9 8 7 10 11 4
Input
24
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output
3
6 1 2 3 6 5 4
10 7 8 9 12 15 14 13 16 11 10
8 17 18 23 22 19 20 21 24
Note
In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.

In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number.
网络流博大精深!
神奇的网络流,由此我想到了一点,当我们需要使一个二分图满足一定的性质时,可以先连一些边然后跑一遍最大流,看是否满流来判可行。
具体方法就是先从源点向所有向左侧点连一些边,容量为它的度,然后左右二分图可连的连上一条容量为一的边,然后右边的边向汇点连一条容量为度的边,跑最大流,看从源点出发和汇到汇点的边是否流满,就可以判断可不可行,再一遍DFS就能知道具体方案了。
本题就是分成奇数一边,偶数一边,然后,加起来为质数的连一条边,源点向奇点连一条容量为2的边,偶点向汇点连一条容量为2的边,跑网络流即可。二分图匹配也行,但要特判。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<stack>
#include<cstring>
#include<vector>
using namespace std;
#define M 80001
#define N 501
#define r(x) scanf("%d",&x)
#define forw(i,x) for(int i=fir[x];i;i=ne[i])
bool pr[100001];
void prepare()
{
    pr[1]=1;
    for(int i=2;i<=100000;i++)
    {
        if(!pr[i])
        {
            for(int j=2;j<=100000/i;j++)
            {
                pr[i*j]=1;
            }
        }
    }
}
int ss,tt,fir[N],to[M],C[M],ne[M],cnt=1;
int ji[N],ou[N];
int n,m;
int xji,xou;
int x;
void add(int x,int y,int z)
{
    to[++cnt]=y;ne[cnt]=fir[x];fir[x]=cnt;C[cnt]=z;
    to[++cnt]=x;ne[cnt]=fir[y];fir[y]=cnt;C[cnt]=0; 
}
int a[N];
int d[N];
bool BFS(int s,int t)
{
    queue<int>q;
    memset(d,-1,sizeof(d));
    d[s]=1;q.push(s);
    while(!q.empty())
    {
        int dx=q.front();q.pop();
        forw(i,dx)
        {
            int v=to[i];
            if(d[v]<0&&C[i]>0) 
            {
                q.push(v);
                d[v]=d[dx]+1;
            }
        }
    }
    if(d[t]>0) return 1;
    return 0;
}
int dfs(int x,int t,int f)
{
    int fl=0;
    if(x==t||f==0) return f;
    forw(i,x)
    {
        int v=to[i];
        if(d[v]>d[x]&&C[i])
        {
            int e=dfs(v,t,min(f,C[i]));
            if(e)
            {
                fl+=e;
                f-=e;
                C[i]-=e;
                C[i^1]+=e;
                if(f==0) break;
            }
        }
    }
    return fl;
}
int maxflow(int s,int t)
{
    int res=0,sss=0;
    while(BFS(s,t))
    {
        sss=dfs(s,t,2e9+3);
        if(!sss) return res;
        res+=sss;
    }
    return res;
}
int fa[N];
int getf(int x)
{
    if(fa[x]==x) return x;
    else return fa[x]=getf(fa[x]);
}
int ans[N][N];
int len[N];
void doit1()
{
    for(int i=xji*2+xou*2+2;i<=cnt;i+=2)
    if(!C[i])
    {
        int U=to[i^1],V=to[i];
        getf(U);getf(V);
        fa[fa[U]]=fa[V];
    }
    for(int i=1;i<=n;i++)
    {
        int k=getf(i);
        ans[k][++len[k]]=i;
    }
    cnt=0;
    for(int i=1;i<=n;i++) if(len[i]) cnt++;
    cout<<cnt<<endl;
    for(int i=1;i<=n;i++) 
    {
        if(len[i])
        {
            cout<<len[i]<<" ";
            for(int j=1;j<=len[i];j++)
            cout<<ans[i][j]<<" ";
            cout<<endl;
        }
    }
}
int F[M],T[M];
int tot;
bool vis[N];
vector<int>mp[N];
void DFS(int x,int t)
{
    if(vis[x]) return;
    mp[t].push_back(x);
    vis[x]=1;
    forw(i,x)
    {
        if(a[x]%2==1)
        {
            if(C[i]==0&&!vis[to[i]]&&to[i]!=ss&&to[i]!=tt) 
            {
                DFS(to[i],t);return;
            }
        }
        else
        {
            if(C[i]==1&&!vis[to[i]]&&to[i]!=ss&&to[i]!=tt)
            {
                DFS(to[i],t);return;
            }
        }
    }
}
void doit()
{
    tot=0;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]) DFS(i,++tot);
    }
    cout<<tot<<endl;
    for(int i=1;i<=tot;i++)
    {
        cout<<mp[i].size()<<" ";
        int k=mp[i].size();
        for(int j=0;j<k;j++)
        cout<<mp[i][j]<<" ";
        cout<<endl;
    }
}
int main()
{
    prepare();
    r(n);
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=n;i++)
    {
        r(a[i]);x=a[i];if(x%2) ji[++xji]=i;else ou[++xou]=i;
    }
    ss=0;tt=n+1;
    for(int i=1;i<=xji;i++) add(ss,ji[i],2);
    for(int i=1;i<=xou;i++) add(ou[i],tt,2);
    for(int i=1;i<=xji;i++)
    for(int j=1;j<=xou;j++)if(!pr[a[ji[i]]+a[ou[j]]]) add(ji[i],ou[j],1);
    if(maxflow(ss,tt)==n)   doit();else puts("Impossible");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值