Codeforces Round #418 (Div. 2)

来源:互联网 发布:消防工程师题库软件 编辑:程序博客网 时间:2024/05/16 16:27

涨分场,啊哈哈哈~~~


A. An abandoned sentiment from past

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed.

To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long time, but now Kaiki provides an opportunity.

Hitagi's sequence a has a length of n. Lost elements in it are denoted by zeros. Kaiki provides another sequence b, whose length k equals the number of lost elements in a (i.e. the number of zeros). Hitagi is to replace each zero in a with an element from b so that each element in b should be used exactly once. Hitagi knows, however, that, apart from 0, no integer occurs in a and b more than once in total.

If the resulting sequence is not an increasing sequence, then it has the power to recover Hitagi from the oddity. You are to determine whether this is possible, or Kaiki's sequence is just another fake. In other words, you should detect whether it is possible to replace each zero in a with an integer from b so that each integer from b is used exactly once, and the resulting sequence is not increasing.

Input

The first line of input contains two space-separated positive integers n (2 ≤ n ≤ 100) and k (1 ≤ k ≤ n) — the lengths of sequence a and brespectively.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 200) — Hitagi's broken sequence with exactly k zero elements.

The third line contains k space-separated integers b1, b2, ..., bk (1 ≤ bi ≤ 200) — the elements to fill into Hitagi's sequence.

Input guarantees that apart from 0, no integer occurs in a and b more than once in total.

Output

Output "Yes" if it's possible to replace zeros in a with elements in b and make the resulting sequence not increasing, and "No" otherwise.

Examples
input
4 211 0 0 145 4
output
Yes
input
6 12 3 0 8 9 105
output
No
input
4 18 94 0 489
output
Yes
input
7 70 0 0 0 0 0 01 2 3 4 5 6 7
output
Yes
Note

In the first sample:

  • Sequence a is 11, 0, 0, 14.
  • Two of the elements are lost, and the candidates in b are 5 and 4.
  • There are two possible resulting sequences: 11, 5, 4, 14 and 11, 4, 5, 14, both of which fulfill the requirements. Thus the answer is "Yes".

In the second sample, the only possible resulting sequence is 2, 3, 5, 8, 9, 10, which is an increasing sequence and therefore invalid.


题意:

数组a和数组b,把数组b中的数换入a数组为0的位置,是否存在一种情况使换入的数组a非单调递增?

思路:

如果数组b大于一个元素的话,肯定是存在的。如果只有一个元素,带进去判断一下就行。

#include <iostream>#include<bits/stdc++.h>using namespace std;int n,k,a[1100],b[1100];int main(){    while(~scanf("%d%d",&n,&k))    {        for(int i=0;i<n;i++)    scanf("%d",&a[i]);        for(int j=0;j<k;j++)    scanf("%d",&b[j]);        int flag=1;        if(k>1) flag=0;        else        {            for(int i=0;i<n;i++)    if(a[i]==0) {a[i]=b[0];break;}            for(int i=1;i<n;i++)    if(a[i]<a[i-1]) {flag=0;break;}        }        if(flag)    printf("No\n");        else    printf("Yes\n");    }}




B. An express train to reveries

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.

On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

Well, she almost had it all — each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.

For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.

Input

The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst.

The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

Output

Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.

Input guarantees that such permutation exists.

Examples
input
51 2 3 4 31 2 5 4 5
output
1 2 5 4 3
input
54 4 2 3 15 4 5 3 1
output
5 4 2 3 1
input
41 1 3 41 4 3 4
output
1 2 3 4
Note

In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.

In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.


题意:

数列a和数列b,都是有一个1到n的数列改变一个元素得到的,求原数列

思路:

分为有一处不同和有两处不同两种情况考虑。

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,m,s[2000],t[2000],p[2000];int vis[2000],a[2000];int main(){    int pos=0;    cin>>n;    memset(vis,0,sizeof(vis));    for(int i=0;i<n;i++)        scanf("%d",&s[i]);    for(int i=0;i<n;i++)    {        scanf("%d",&t[i]);        if(t[i]==s[i])        {            vis[s[i]]=1;            p[i]=s[i];        }        else        {            a[pos++]=i;        }    }    if(pos==2)    {    int c=a[0],d=a[1];        if(t[c]!=t[d]&&(!vis[t[c]])&&(!vis[t[d]]))    {        p[c]=t[c];p[d]=t[d];    }    if(s[c]!=s[d]&&(!vis[s[c]])&&(!vis[s[d]]))    {        p[c]=s[c];p[d]=s[d];    }    if(s[c]!=t[d]&&(!vis[s[c]])&&(!vis[t[d]]))    {        p[c]=s[c];p[d]=t[d];    }    if(t[c]!=s[d]&&(!vis[t[c]])&&(!vis[s[d]]))    {        p[c]=t[c];p[d]=s[d];    }    }    else if(pos==1)    {        int c=a[0];        if(!vis[s[c]])        {            p[c]=s[c];        }        if(vis[t[c]])        {            for(int i=1;i<=n;i++)            {                if(!vis[i]) p[c]=i;            }        }        else        {            p[c]=t[c];        }    }    for(int i=0;i<n-1;i++)  printf("%d ",p[i]);    printf("%d\n",p[n-1]);}


C. An impassioned circulation of affection

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!

Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.

For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.

But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.

Input

The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland.

The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland.

The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has.

The next q lines describe one plan each: the i-th among them contains an integer mi (1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi's possible favourite colour.

Output

Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.

Examples
input
6koyomi31 o4 o4 m
output
365
input
15yamatonadeshiko101 a2 a3 a4 a5 a1 b2 b3 b4 b5 b
output
3457812345
input
10aaaaaaaaaa210 b10 z
output
1010
Note

In the first sample, there are three plans:

  • In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable;
  • In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6;
  • In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.

题意:

给一串字符串,q次询问,每次询问:改变mi个字母最多能得到多少个连续字母ci?

思路:

询问很多,但是字符串长度只有1500,字母只有26种。暴力求得长度为n所有情况,然后O(1)查询即可。

#include <iostream>#include<bits/stdc++.h>using namespace std;const int N=2200;int q;char s[N];int d[N][110],a[N][110];int main(){    int n;    cin>>n;    scanf("%s",s);    memset(d,0,sizeof(d));    for(int i=0;i<n;i++)    {        for(int j=0;j<26;j++)        {            if(i==0)    a[i][j]=0;            else    a[i][j]=a[i-1][j];        }        a[i][s[i]-'a']++;    }    for(int i=0;i<n;i++)    {        for(int j=i;j<n;j++)        {            for(int k=0;k<26;k++)            {                int sum=0;                if(i==0)    sum=a[j][k];                else    sum=a[j][k]-a[i-1][k];                int t=j-i+1;                if(t>d[t-sum][k])   d[t-sum][k]=t;            }        }    }    for(int i=1;i<=n;i++)        for(int j=0;j<26;j++)            if(d[i][j]<d[i-1][j])   d[i][j]=d[i-1][j];    cin>>q;    while(q--)    {        char c[2];        int t;        scanf("%d%s",&t,c);        printf("%d\n",d[t][c[0]-'a']);    }}


D. An overnight dance in discotheque

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it?

The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area Ci described by a center (xi, yi) and a radius riNo two ranges' borders have more than one common point, that is for every pair (i, j) (1 ≤ i < j ≤ n) either ranges Ci and Cj are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges.

Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time.

But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above.

By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum.

Input

The first line of input contains a positive integer n (1 ≤ n ≤ 1 000) — the number of dancers.

The following n lines each describes a dancer: the i-th line among them contains three space-separated integers xiyi and ri( - 106 ≤ xi, yi ≤ 1061 ≤ ri ≤ 106), describing a circular movement range centered at (xi, yi) with radius ri.

Output

Output one decimal number — the largest achievable sum of spaciousness over two halves of the night.

The output is considered correct if it has a relative or absolute error of at most 10 - 9. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .

Examples
input
52 1 60 4 12 -1 31 -2 14 -1 1
output
138.23007676
input
80 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 8
output
289.02652413
Note

The first sample corresponds to the illustrations in the legend.


题意:

给n个圆,分成两组,求最大的总阴影面积(如上图 2)

思路:

贪心。把圆按面积排序,分成两堆,如果放左放右都是增加面积或都减少面积,就随意放。如果放一堆增加面积,而放另一堆减少面积,则放增加面积的一堆。

#include <iostream>#include<bits/stdc++.h>#include<math.h>#define pi 3.14159265358979323846using namespace std;const int N=5500;struct node{    double x,y,r;}a[N];bool cmp(node a,node b){    return a.r>b.r;}int c1[N],c2[N];int main(){    int n;    cin>>n;    for(int i=0;i<n;i++)    {        scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r);    }    sort(a,a+n,cmp);    double ans=0;    int cnt1=0;    int cnt2=0;    for(int i=0;i<n;i++)    {        int na=0,nb=0;        for(int j=0;j<cnt1;j++)        {            int t1=c1[j];            if(sqrt((a[i].x-a[t1].x)*(a[i].x-a[t1].x)+(a[i].y-a[t1].y)*(a[i].y-a[t1].y))+a[i].r<=a[t1].r)            {                na++;            }        }        for(int j=0;j<cnt2;j++)        {            int t1=c2[j];            if(sqrt((a[i].x-a[t1].x)*(a[i].x-a[t1].x)+(a[i].y-a[t1].y)*(a[i].y-a[t1].y))+a[i].r<=a[t1].r)            {                nb++;            }        }        if((na%2==1)&&(nb%2==0))        {            c2[cnt2++]=i;        }        else        {            c1[cnt1++]=i;        }        if((na%2==1)&&(nb%2==1))        {            ans-=pi*a[i].r*a[i].r;        }        else    ans+=pi*a[i].r*a[i].r;    }    printf("%.10f\n",ans);}

原创粉丝点击