Codeforces round #257 (Div. 2)

Source: Internet
Author: User

Question:

A jzzhu and children ------ codeforces created

B jzzhu and sequences ------ codeforces limit B

C jzzhu and chocolate ------ codeforces 449a

D jzzhu and cities ------ codeforces 449b

E jzzhu and apples ------ codeforces 449c


Question A: N numbers each time from front to back each number-m ask which number is finally reduced to 0 (water question)


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;typedef __int64 LL;#define N 100010int n,m,ans;int main(){    int i,x,y;    scanf("%d%d",&n,&m);    for(i=1,y=-1;i<=n;i++)    {        scanf("%d",&x);        if(x/m+(x%m!=0)>=y)        {            y=x/m+(x%m!=0);            ans=i;        }    }    printf("%d\n",ans);    return 0;}


)

The recursive formula deformation Fi = fi-1-fi-2 is able to roll out the cycle of 6 and then directly calculate


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;typedef __int64 LL;#define N 100010#define mod 1000000007LL x,y,n,ans;int main(){    scanf("%I64d%I64d%I64d",&x,&y,&n);    n%=6;    switch(n)    {        case 0: ans=x-y; break;        case 1: ans=x; break;        case 2: ans=y; break;        case 3: ans=y-x; break;        case 4: ans=-x; break;        case 5: ans=-y; break;    }    printf("%I64d\n",(ans%mod+mod)%mod);    return 0;}

Question C: Cut the chocolate and only cross it in the concave area or vertically cut the chocolate into the K-knife with N * m. The minimum block size is the maximum size (question)

First, assume that K is greater than N + m-2.

Next, we will consider the Cutting Strategy. In order to maximize the minimum block size, we should try to divide it as few as possible.

That is to say, try to switch in one direction (either cross-cutting or vertical cutting)

Let's start to discuss whether the K-knife can be cut off in one direction.

Finally, assume that the K-knife cannot be switched in one direction, so both directions must be switched.

Obviously, the long side should be cut first because the number of parts to be split is the least.


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef __int64 LL;LL n,m,k;int main(){    scanf("%I64d%I64d%I64d",&n,&m,&k);    if(n+m-2<k) printf("-1\n");    else if(n%(k+1)==0||m%(k+1)==0) printf("%I64d\n",n*m/(k+1));    else    {        if(n<m) swap(n,m);        if(m-1>=k) printf("%I64d\n",max(m/(k+1)*n,n/(k+1)*m));        else if(n-1>=k) printf("%I64d\n",n/(k+1)*m);        else printf("%I64d\n",m/(k-n+2));    }    return 0;}

D: there are highways and railways between N cities, each of which is bidirectional and the length of known railways must be from 1 city to a city. Now we need to delete as many railway lines as possible, but we need to keep 1 city to every the shortest path of a city remains unchanged. You can delete a maximum of several records (as shown in the figure below)

Because more railway lines need to be deleted, the road can be taken without changing the shortest path.

From this, the strategy is obtained. Starting from the beginning of City 1, the shortest path is used to obtain the Shortest Path of the complete picture based on the principle of taking the road and taking the road. At this time, the railway line on the shortest path cannot be deleted. you can calculate the number of deleted entries.


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;typedef __int64 LL;#define N 100010int n,m,k,tot,ans;int vis[N],head[N],pre[N];LL dis[N];struct edge{    int v,w,kind,next;}ed[N*8];struct node{    int idx;    LL dis;    bool operator<(const node fa) const    {        return dis>fa.dis;    }}u,v;priority_queue<node> qu;void add(int u,int v,int w,int kind){    ed[tot].v=v;    ed[tot].w=w;    ed[tot].kind=kind;    ed[tot].next=head[u];    head[u]=tot++;}void bfs(){    int i;    u.dis=0; u.idx=1;    qu.push(u);    while(!qu.empty())    {        do        {            u=qu.top();            qu.pop();        }while(vis[u.idx]&&!qu.empty());        if(vis[u.idx]) return ;        vis[u.idx]=1;        if(pre[u.idx]!=-1) ans+=ed[pre[u.idx]].kind;        for(i=head[u.idx];~i;i=ed[i].next)        {            v.idx=ed[i].v;            v.dis=u.dis+ed[i].w;            if(!vis[v.idx])            {                if(dis[v.idx]==v.dis&&!ed[i].kind) pre[v.idx]=i;                if(!dis[v.idx]||dis[v.idx]>v.dis)                {                    pre[v.idx]=i;                    dis[v.idx]=v.dis;                    qu.push(v);                }            }        }    }}int main(){    int i,u,v,w;    memset(head,-1,sizeof(head));    memset(pre,-1,sizeof(pre));    scanf("%d%d%d",&n,&m,&k);    for(i=1;i<=m;i++)    {        scanf("%d%d%d",&u,&v,&w);        add(u,v,w,0);        add(v,u,w,0);    }    for(i=1;i<=k;i++)    {        scanf("%d%d",&v,&w);        add(1,v,w,1);    }    bfs();    printf("%d\n",k-ans);    return 0;}

Question E: numbers ranging from 1 to n are divided into groups. numbers in a group must be divided into groups. No matter how many groups can be divided and a solution can be provided)

Even numbers are the most friendly for us because all even numbers can be the prime number in the odd number of the team, especially unfriendly.

Using the above idea, we can enumerate the prime numbers (not starting from 2 or starting from 3) and find all the numbers of the factors including the prime number.

In this case, assuming that there are even numbers, you can group them directly.

If there is an odd number (but not one), we will discard an even number and then group them in two groups.

The remaining number is either the number of groups not found or the number of even groups. We can obtain the answer by grouping the even number into two groups.


#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;typedef __int64 LL;#define N 100010int n,cnt,ans;int flag[N],p[N],vis[N];vector< pair<int,int> > v;void get_prime(){    int i,j;    for(i=2;i<=n;i++)    {        if(!flag[i]) p[cnt++]=i;        for(j=0;j<cnt&&p[j]*i<=n;j++)        {            flag[i*p[j]]=1;            if(i%p[j]==0) break;        }    }}int main(){    int i,j,k;    scanf("%d",&n);    get_prime();    for(i=1;i<cnt;i++)    {        vector<int> tmp;        for(j=p[i];j<=n;j+=p[i])        {            if(!vis[j]) tmp.push_back(j);        }        k=tmp.size();        if(k==1) continue;        if(k%2==1)        {            for(vector<int>::iterator it=tmp.begin();it!=tmp.end();it++)            {                if(*it%2==0)                {                    tmp.erase(it);                    k--;                    break;                }            }        }        for(j=0;j<k;j+=2)        {            vis[tmp[j]]=1;            vis[tmp[j+1]]=1;            v.push_back(make_pair(tmp[j],tmp[j+1]));            ans++;        }    }    for(i=2,j=0;i<=n;i+=2)    {        if(!vis[i])        {            if(j)            {                v.push_back(make_pair(j,i));                ans++;                j=0;            }            else j=i;        }    }    printf("%d\n",ans);    for(i=0;i<ans;i++) printf("%d %d\n",v[i].first,v[i].second);    return 0;}

PS: I escaped from the competition yesterday. Today, I have to make up the problem. I have to spur myself to work harder !!!

Codeforces round #257 (Div. 2)

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.