codeforces544

来源:互联网 发布:至尊妖孽软件 编辑:程序博客网 时间:2024/06/05 10:26

You are given a string q. A sequence of k strings s1, s2, …, sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + … + sk = q) and the first characters of these strings are distinct.

Find any beautiful sequence of strings or determine that the beautiful sequence doesn’t exist.

Input
The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.

The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.

Output
If such sequence doesn’t exist, then print in a single line “NO” (without the quotes). Otherwise, print in the first line “YES” (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, …, sk.

If there are multiple possible answers, print any of them.

Example
Input
1
abca
Output
YES
abca
Input
2
aaacas
Output
YES
aaa
cas
Input
4
abc
Output
NO
Note
In the second sample there are two possible answers: {“aaaca”, “s”} and {“aaa”, “cas”}.

这题只要输一种就行了QAQ

using namespace std;#include<bits/stdc++.h>#define N 1001char it[N];int n,len;char hh[N][101];int p;int l[N];int r[N];int have[128];int ans;bool flag=0;int main(){    cin>>n;    int cnt=0;    scanf("%s",it+1);    len=strlen(it+1);    int p=1;    while(p<=len)    {        int pre=p;        have[it[p]]=1;        while(have[it[p]]&&p<=len)p++;        p--;        l[++cnt]=pre;r[cnt]=p;        p++;    }    if(cnt<n) {puts("NO");return 0;}    puts("YES");    for(int i=1;i<=n-1;i++)    {        for(int j=l[i];j<=r[i];j++) cout<<it[j];        puts("");    }    for(int i=l[n];i<=len;i++)cout<<it[i];}

A map of some object is a rectangular field consisting of n rows and n columns. Each cell is initially occupied by the sea but you can cover some some cells of the map with sand so that exactly k islands appear on the map. We will call a set of sand cells to be island if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side-adjacent cell. The cells are called to be side-adjacent if they share a vertical or horizontal side. It is easy to see that islands do not share cells (otherwise they together form a bigger island).

Find a way to cover some cells with sand so that exactly k islands appear on the n × n map, or determine that no such way exists.

Input
The single line contains two positive integers n, k (1 ≤ n ≤ 100, 0 ≤ k ≤ n2) — the size of the map and the number of islands you should form.

Output
If the answer doesn’t exist, print “NO” (without the quotes) in a single line.

Otherwise, print “YES” in the first line. In the next n lines print the description of the map. Each of the lines of the description must consist only of characters ‘S’ and ‘L’, where ‘S’ is a cell that is occupied by the sea and ‘L’ is the cell covered with sand. The length of each line of the description must equal n.

If there are multiple answers, you may print any of them.

You should not maximize the sizes of islands.

Example
Input
5 2
Output
YES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSS
Input
5 25
Output
NO
贪心,一格间一格摆沙子,数量到了就输出

using namespace std;#include<bits/stdc++.h>#define N 101int n,k;bool ans[N][N];bool flag=0;int main(){    cin>>n>>k;    int kk=(n*n+1)/2;    if(k>kk) {puts("NO");return 0;}    ans[1][1]=1;    int cnt=0;    puts("YES");    if(k==0)    {        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++) cout<<"S";            puts("");        }        return 0;    }    for(int i=1;i<=n;i++)    {        ans[i][1]=ans[i-1][1]^1;        if(ans[i][1])cnt++;        if(cnt==k) break;        for(int j=2;j<=n;j++)         {            ans[i][j]=ans[i][j-1]^1;            if(ans[i][j])cnt++;            if(cnt==k) {flag=1;break;}        }        if(flag) break;    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)if(ans[i][j]) cout<<"L";else cout<<"S";        puts("");     }}

Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs in every line of code that he writes.

Let’s call a sequence of non-negative integers v1, v2, …, vn a plan, if v1 + v2 + … + vn = m. The programmers follow the plan like that: in the beginning the first programmer writes the first v1 lines of the given task, then the second programmer writes v2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let’s call a plan good, if all the written lines of the task contain at most b bugs in total.

Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer mod.

Input
The first line contains four integers n, m, b, mod (1 ≤ n, m ≤ 500, 0 ≤ b ≤ 500; 1 ≤ mod ≤ 109 + 7) — the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.

The next line contains n space-separated integers a1, a2, …, an (0 ≤ ai ≤ 500) — the number of bugs per line for each programmer.

Output
Print a single integer — the answer to the problem modulo mod.

Example
Input
3 3 3 100
1 1 1
Output
10
Input
3 6 5 1000000007
1 2 3
Output
0
Input
3 5 6 11
1 2 1
Output
0

就是一个无限背包,我真的是傻了

%:pragma GCC optimize(4)using namespace std;#include<cstdio>#include<iostream>#include<cstring>#define N 505long long n,mm,k;long long mod;long long dp[N*5][N*5];long long tot;long long a[N];long long m[N*5];long long v[N*5];int it[N];int num=0;int sum;int cnt;int main(){    cin>>n>>mm>>k>>mod;    for(int i=1;i<=n;i++) cin>>a[i];    it[1]=1;num=1;sum=1;    for(int i=1;i<=n;i++)    {        m[++cnt]=a[i];        v[cnt]=1;    }    dp[0][0]=1;    for(int i=1;i<=cnt;i++)    {        for(int hh=0;hh<=mm-v[i];hh++)        {            for(int j=k+1;j>=0;j--)            {                dp[hh+v[i]][min(j+m[i],k+1)]+=dp[hh][j];                dp[hh+v[i]][min(j+m[i],k+1)]%=mod;            }           }    }    long long res=0;    for(int i=0;i<=k;i++) {res+=dp[mm][i];res=res%mod;}    cout<<res;}

In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.

You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.

Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.

Input
The first line contains two integers n, m (1 ≤ n ≤ 3000, ) — the number of cities and roads in the country, respectively.

Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.

The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n, 0 ≤ li ≤ n).

Output
Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

Example
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
Output
0
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
Output
1
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
Output
-1

当权值相同时,可以BFS求任意两点最短路,O(n^2)然后就是一个补集转化,重复走的尽量多,就可以A了

using namespace std;#include<bits/stdc++.h>#define N 3001#define forw(i,x) for(int i=fir[x];i;i=ne[i])int way[N][N];int n,m;int x,y;int s1,t1,l1,s2,t2,l2;int ne[N*2],fir[N],to[N*2],cnt=1;void add(int x,int y){    ne[++cnt]=fir[x];fir[x]=cnt;to[cnt]=y;}bool vis[N];queue<int>q;int res=2e9;int main(){    cin>>n>>m;    for(int i=1;i<=m;i++)    {        cin>>x>>y;add(x,y);add(y,x);    }    for(int i=1;i<=n;i++)    {        memset(vis,0,sizeof(vis));        vis[i]=1;way[i][i]=0;        while(!q.empty())q.pop();        q.push(i);        while(!q.empty())        {            int ind=q.front();q.pop();            forw(j,ind)            {                int v=to[j];                if(!vis[v])                {                    vis[v]=1;                    way[i][v]=way[i][ind]+1;                    q.push(v);                }            }        }    }    if(0)    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            cout<<way[i][j]<<" ";        }        puts("");    }    cin>>s1>>t1>>l1;    cin>>s2>>t2>>l2;    if(way[s1][t1]>l1||way[s2][t2]>l2)    {        puts("-1");return 0;    }    res=way[s1][t1]+way[s2][t2];//可能不选仅这一种可能,无相交     for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            int it=way[i][j];            int jj=way[s1][i]+way[j][t1]+it;            int kk=way[s2][i]+way[j][t2]+it;            if(jj<=l1&&kk<=l2) res=min(res,jj+kk-it);            it=way[i][j];            jj=way[s1][i]+way[j][t1]+it;            kk=way[s2][j]+way[i][t2]+it;            if(jj<=l1&&kk<=l2) res=min(res,jj+kk-it);                   }    }    cout<<m-res<<endl;}

You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position i and some letter c of the English alphabet, such that this string is the only string in the multiset that has letter c in position i.

For example, a multiset of strings {“abc”, “aba”, “adc”, “ada”} are not easy to remember. And multiset {“abc”, “ada”, “ssa”} is easy to remember because:

the first string is the only string that has character c in position 3;
the second string is the only string that has character d in position 2;
the third string is the only string that has character s in position 2.
You want to change your multiset a little so that it is easy to remember. For aij coins, you can change character in the j-th position of the i-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember.

Input
The first line contains two integers n, m (1 ≤ n, m ≤ 20) — the number of strings in the multiset and the length of the strings respectively. Next n lines contain the strings of the multiset, consisting only of lowercase English letters, each string’s length is m.

Next n lines contain m integers each, the i-th of them contains integers ai1, ai2, …, aim (0 ≤ aij ≤ 106).

Output
Print a single number — the answer to the problem.

Example
Input
4 5
abcde
abcde
abcde
abcde
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
3
Input
4 3
abc
aba
adc
ada
10 10 10
10 1 10
10 10 10
10 1 10
Output
2
Input
3 3
abc
ada
ssa
1 1 1
1 1 1
1 1 1
Output
0

状压DP,取lowzero的思想比较巧妙。

然后你可以看官方题解发现可以A

%:pragma GCC optimize(4)using namespace std;#include<bits/stdc++.h>#define N 30int dp[1<<25];int n,m;int a[N][N];#define r(x) scanf("%d",&x)#define F(i,a,b) for(int i=a;i<=b;i++) int qi[N][N];int st[N][N];char c[N][N];int lowbit(int x){    return (x&(-x));}int main(){    r(n);r(m);    for(int i=1;i<=n;i++) scanf("%s",c[i]+1);    F(i,1,n)    F(j,1,m)     r(a[i][j]);    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            int it=-2e9,sum=0;            for(int k=1;k<=n;k++)            {                if(c[k][j]==c[i][j])                {                    st[i][j]|=(1<<(k-1));                    sum+=a[k][j];                    it=max(it,a[k][j]);                }             }            sum-=it;            qi[i][j]=sum;                    }    }    for(int i=0;i<=(1<<n)-1;i++) dp[i]=2e9;//初值赋太大     dp[0]=0;    for(int i=0;i<=(1<<n)-1;i++)    {        int k;        for(k=0;k<=n;k++)        if(!((1<<k)&i))        {            break;        }        for(int j=1;j<=m;j++)        {            dp[i|(1<<k)]=min(dp[i|(1<<k)],dp[i]+a[k+1][j]);            dp[i|st[k+1][j]]=min(dp[i]+qi[k+1][j],dp[i|st[k+1][j]]);        }    }    cout<<dp[(1<<n)-1]<<endl;}

Recently, the ACM/ICPC team of Marjar University decided to choose some new members from freshmen to take part in the ACM/ICPC competitions of the next season. As a traditional elite university in ACM/ICPC, there is no doubt that application forms will fill up the mailbox. To constitute some powerful teams, coaches of the ACM/ICPC team decided to use a system to score all applicants, the rules are described as below. Please note that the score of an applicant is measured by pts, which is short for “points”.

  1. Of course, the number of solved ACM/ICPC problems of a applicant is important. Proudly, Marjar University have a best online judge system called Marjar Online Judge System V2.0, and in short, MOJ. All solved problems in MOJ of a applicant will be scored under these rules:

(1) The problems in a set, called MaoMao Selection, will be counted as 2.5 pts for a problem.
(2) The problems from Old Surgeon Contest, will be counted as 1.5 pts for a problem.
There is no problem in MaoMao Selection from Old Surgeon Contest.
(3) Besides the problem from MaoMao Selection and Old Surgeon Contest, if the problem’s id is a prime, then it will be counted as 1 pts.
(4) If a solved problem doesn’t meet above three condition, then it will be counted as 0.3 pts.
2. Competitions also show the strength of an applicant. Marjar University holds the ACM/ICPC competition of whole school once a year. To get some pts from the competition, an applicant should fulfill rules as below:

The member of a team will be counted as 36 pts if the team won first prize in the competition.
The member of a team will be counted as 27 pts if the team won second prize in the competition.
The member of a team will be counted as 18 pts if the team won third prize in the competition.
Otherwise, 0 pts will be counted.
3. We all know that some websites held problem solving contest regularly, such as JapanJam, ZacaiForces and so on. The registered member of JapanJam will have a rating after each contest held by it. Coaches thinks that the third highest rating in JapanJam of an applicant is good to show his/her ability, so the scoring formula is:

Pts = max(0, (r - 1200) / 100) * 1.5
Here r is the third highest rating in JapanJam of an applicant.

  1. And most importantly - if the applicant is a girl, then the score will be added by 33 pts.

The system is so complicated that it becomes a huge problem for coaches when calculating the score of all applicants. Please help coaches to choose the best M applicants!

Input
There are multiple test cases.

The first line of input is an integer T (1 ≤ T ≤ 10), indicating the number of test cases.

For each test case, first line contains two integers N (1 ≤ N ≤ 500) - the number of applicants and M (1 ≤ M ≤ N) - the number of members coaches want to choose.

The following line contains an integer R followed by R (0 ≤ R ≤ 500) numbers, indicating the id of R problems in MaoMao Selection.

And then the following line contains an integer S (0 ≤ S ≤ 500) followed by S numbers, indicating the id of S problems from Old Surgeon Contest.

The following line contains an integer Q (0 ≤ Q ≤ 500) - There are Q teams took part in Marjar University’s competition.

Following Q lines, each line contains a string - team name and one integer - prize the team get. More specifically, 1 means first prize, 2 means second prize, 3 means third prize, and 0 means no prize.

In the end of each test case, there are N parts. In each part, first line contains two strings - the applicant’s name and his/her team name in Marjar University’s competition, a char sex - M for male, F for female and two integers P (0 ≤ P ≤ 1000) - the number of problem the applicant solved, C (0 ≤ C ≤ 1000) - the number of competitions the applicant have taken part in JapanJam.

The following line contains P integers, indicating the id of the solved problems of this applicant.

And, the following line contains C integers, means the rating for C competitions the applicant have taken part in.

We promise:

The problems’ id in MaoMao Selection, Old Surgeon Contest and applicant’s solving list are distinct, and all of them have 4 digits (such as 1010).
All names don’t contain spaces, and length of each name is less than 30.
All ratings are non-negative integers and less than 3500.
Output
For each test case, output M lines, means that M applicants and their scores. Please output these informations by sorting scores in descending order. If two applicants have the same rating, then sort their names in alphabet order. The score should be rounded to 3 decimal points.

Sample Input
1
5 3
3 1001 1002 1003
4 1004 1005 1006 1007
3
MagicGirl!!! 3
Sister’s_noise 2
NexusHD+NexusHD 1
Edward EKaDiYaKanWen M 5 3
1001 1003 1005 1007 1009
1800 1800 1800
FScarlet MagicGirl!!! F 3 5
1004 1005 1007
1300 1400 1500 1600 1700
A NexusHD+NexusHD M 0 0

B None F 0 0

IamMM Sister’s_noise M 15 1
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
3000
Sample Output
FScarlet 60.000
IamMM 44.300
A 36.000

大模拟题,思路清晰一边打下来就可以A了

using namespace std;#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<new>#include<map>map<string,double>team;#define N 10001int t;int n,m;int pr[N];int maomao[N];int old[N];void prepare(){    pr[1]=1;    for(int i=2;i<=N;i++)    {        if(!pr[i])        {            for(int j=2;j<=N/i;j++)            {                pr[i*j]=1;            }        }    }}struct people{    double rank;    string name;}xx[N];string tname,mname;string sex;double rank[N];int solved,cnum;map<int,double>problem;int japan[N];#define eps 0.0000001bool cmp(people x,people y){    return x.rank==y.rank?x.name<y.name:x.rank>y.rank;}bool cmp6(int x,int y){    return x>y;}void doit(){    team.clear();    problem.clear();    memset(rank,0,sizeof(rank));    memset(japan,0,sizeof(japan));    int r,x;    cin>>n>>m;    cin>>r;    for(int i=1;i<=r;i++) {cin>>x;problem[x]=2.5;}    cin>>r;    for(int i=1;i<=r;i++) {cin>>x;problem[x]=1.5;}    int q,prize;    cin>>q;    for(int i=1;i<=q;i++)    {        cin>>tname>>prize;        if(prize==1) team[tname]=36.0;        else if(prize==2) team[tname]=27.0;        else if(prize==3) team[tname]=18.0;        else team[tname]=0.0;    }    string man;    for(int i=1;i<=n;i++)    {        rank[i]=0;        xx[i].name="";        xx[i].rank=0;        cin>>man>>tname>>sex>>solved>>cnum;        if(sex=="F"){rank[i]+=33.0;}        rank[i]+=team[tname];        for(int j=1;j<=solved;j++)        {            int it;            cin>>it;            if(!problem.count(it))            {                if(pr[it]) rank[i]+=0.3;                else rank[i]+=1.0;            }            else            {                rank[i]+=problem[it];            }        }        if(0)if(i==1) cout<<rank[i]<<endl;        memset(japan,0,sizeof(japan));        for(int j=1;j<=cnum;j++)        {            int it;            cin>>it;            japan[j]=it;        }        sort(japan+1,japan+cnum+1,cmp6);        xx[i].rank=rank[i]+max(0.0,(double(japan[3])-1200.0)/100.0)*1.5;        xx[i].name=man;    }    sort(xx+1,xx+n+1,cmp);    for(int i=1;i<=m;i++)    {        cout<<xx[i].name<<" ";        printf("%.3lf",xx[i].rank);        puts("");    }}int main(){    cin>>t;    prepare();    while(t--)    {        doit();    }}
原创粉丝点击