Codeforces round #259 (Div. 2) A/B/C/d

Source: Internet
Author: User

A little pony and crystal mine


Q: print a diamond pattern with an odd number of n. The entire figure occupies N x n.

For example, ---> input 3, the print pattern is as follows:

*D*DDD*D*


Algorithm:

The number of rows separated by D is the distance from the middle row.

D is n-the distance between the current row and the middle row * 2.

The lower half side is the same.


#include<cstdio>#include<iostream>#include<cstring>using namespace std;int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int h = n/2;        for(int i=1;i<=h;i++)        {            for(int j=0;j

B. Little Pony and sort by shift
Question: This is a sequence composed of N numbers. You can only move the last number to the beginning at a time and ask if the sequence can be changed to a non-decreasing sequence through several such moves, if-1 cannot be output, otherwise it takes at least a few such operations to output.
Algorithm: it is observed that the Operation changes to a non-decreasing sequence. Only one of the two adjacent numbers is decreasing and is cut between the two numbers, the sequence on both sides should be incremental. So first find the point that suddenly decreases, and then scan the next section (because the previous section has indirectly determined that it is increasing when looking for the demarcation line) if there is another point that suddenly decreases at the second place, output-1. Otherwise, the number of subsequent sections is output.
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#define maxn 100010using namespace std;int a[maxn];int main(){    int n,flag,t;    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<n;i++)            scanf("%d",&a[i]);        flag = 0,t = -1;        for(int i=0;i<n-1;i++)        {            if(a[i+1]<a[i])            {                t = i;                break;            }        }        if(t==-1)        {            printf("0\n");            continue;        }        for(int i=t+1;i<n-1;i++)        {            if(a[i+1]<a[i])            {                flag = 1;                break;            }        }        if(flag || a[0]<a[n-1])        {            printf("-1\n");            continue;        }        else printf("%d\n",n-t-1);    }    return 0;}


C. Little Pony and expected maximum

Question: I threw a dice on the m Plane n times and expected the maximum number.
Algorithm: the maximum number is I, then the probability of I is (I/M) ^ N-(I-1/m) ^ N (I/M) ^ N minus the probability of the maximum number of I-1.
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#define maxm 100010using namespace std;double p[maxm],ans[maxm];int main(){    int m,n;    while(scanf("%d%d",&m,&n)!=EOF)    {        double res = 0;        ans[0] = 0;        for(int i=1;i<=m;i++)        {            ans[i] = pow((i*1.0)/m,n);            p[i] = ans[i]-ans[i-1];        }        for(int i=1;i<=m;i++)            res += p[i]*i;        printf("%lf\n",res);    }    return 0;}


D. Little Pony and harmony chest
To give a sequence a, construct a sequence B so that sum (| BI-ai |) is the smallest and the maximum public factor of any two bi is 1.
The biggest common factor is 1, so the B sequence should be composed of all prime numbers. Find the nearest prime number each time. But this is wrong. If the same prime number appears twice, their maximum public factor is themselves.
Algorithm: 1. The maximum value of AI is 30, and the minimum value is 1. Therefore, in any case, the value of BI 1 can satisfy the limit of the maximum public factor 1, now we need to find a solution that is smaller than sum (| BI-ai |) by 1. Therefore, Bi cannot exceed 59 (30-1 = 29, 30 + 29 = 59 ).
2. Any number can be expressed by several prime factor. If the maximum public factor is 1, the prime factor status cannot be repeated. There are only 17 prime numbers within 60. In this case, we compress the state and use each bit to represent the first prime factor. Record the prime factor from 1 to 59 with the State.
3. DP [I] [J] indicates that the sum (| BI-ai |) value of the smallest J state is obtained by the number of the first I.
When K & Val [x] of the current State DP [I-1] [k] is 0, DP [I] [k] = max (DP [I] [K], DP [I-1] [k ^ Val [x] + ABS (A [I]-x), Val [x] represents the prime factor state of X.
Use ans [I] [J] to record the transition of the State, so that the B sequence is output. Here we use the reversible operation of ^.
#include<cstdio>#include<iostream>#include<cstring>#define maxn 60#define INF 0x3f3f3f3f#include<cmath>using namespace std;bool isp[maxn];int pri[maxn],c,a[110],b[110],val[maxn],dp[110][(1<<18)],ans[110][(1<<18)];void init(){    memset(isp,0,sizeof(isp));    c = 0;    for(int i=2;i<maxn;i++)    {        if(!isp[i])        {            pri[c++] = i;            for(int j=i*2;j<maxn;j+=i)                isp[j] = true;        }    }}int main(){    int n;    init();    while(scanf("%d",&n)!=EOF)    {        memset(val,0,sizeof(val));        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        for(int i=1;i<60;i++)        {            for(int j=0;j<c;j++)            {                if(i%pri[j]==0)                    val[i] = (val[i]|(1<<j));            }        }        for(int i=0;i<=n;i++)        {            for(int j=0;j<(1<<c);j++)                dp[i][j] = INF;        }        for(int i=0;i<(1<<c);i++)            dp[0][i] = 0;        for(int i=1;i<=n;i++)        {            for(int j=0;j<(1<<c);j++)            {                for(int k=1;k<60;k++)                {                    if((j&val[k])==0)                    {                        int tmp = dp[i-1][j^val[k]]+fabs(a[i]-k);                        if(tmp<dp[i][j])                        {                            dp[i][j] = tmp;                            ans[i][j] = k;                        }                    }                }            }        }        int res = INF,state,x=0;        for(int i=0;i<(1<<c);i++)        {            if(dp[n][i]<res)            {                res = dp[n][i];                state = i;            }        }        for(int i=n;i>=1;i--)        {            b[x++] = ans[i][state];            int t = ans[i][state];            state = state^val[t];        }        for(int i=x-1;i>0;i--)            printf("%d ",b[i]);        printf("%d\n",b[0]);    }    return 0;}









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.