Codeforces Round #Pi (Div. 2) (A,B,C,D)

Source: Internet
Author: User

Question A:
Title Address: Lineland Mail

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>Using namespace Std;typedef long Long ll;constintinf=0x3f3f3f3f; const double pi= ACOs (-1.0); const Double esp=1E-6; constintmaxn=1e5+Ten; struct node{intIdintValueintMax;intMin;}Q[MAXN];intCMP (struct node a,struct node B) {returnA.value<b.value;}intCMP1 (struct node a,struct node B) {returnA.id<b.id;}intMain () {intN,i; while(~SCANF ("%d", &n)) { for(i=1; i<=n;i++) {scanf ("%d",&Q[i]. value);Q[i]. id=i;Q[q[i]. id]. max=0;Q[q[i]. id]. min=0; }Sort(Q+1,Q+n+1, CMP); for(i=1; i<=n;i++) {if(i==1){Q[i]. min=ABS(Q[1]. value-Q[2]. value);Q[i]. max=ABS(Q[n]. value-Q[1]. value); }Else if(i==n) {Q[i]. min=ABS(Q[n]. value-Q[n-1]. value);Q[i]. max=ABS(Q[n]. value-Q[1]. value); }Else{Q[i]. Min=min (ABS(Q[i]. value-Q[i-1]. Value),ABS(Q[i+1]. value-Q[i]. value));Q[i]. Max=max (ABS(Q[i]. value-Q[1]. Value),ABS(Q[n]. value-Q[i]. value)); }        }Sort(Q+1,Q+1+N,CMP); for(i=1; i<=n;i++) {printf("%d %d\ n",Q[i]. Min,Q[i].        MAX); }    }return 0;}

Question B:
Title Address: Berland National Library

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;Const intmaxn= the;Const intmaxn1=1e6+Ten;Charstr[Ten];intAintVIS[MAXN1];intMain () {intNintCnt1,cnt2; while(~scanf("%d", &n)) {memset(Vis,0,sizeof(VIS)); Cnt1=cnt2=0; for(intI=0; i<n;i++) {scanf('%s%d ', &str,&a);if(str[0]==' + ') {cnt1++; vis[a]=1;                Cnt2=max (Cnt1,cnt2); }Else{if(!vis[a])                    {cnt2++; }Else{vis[a]=0;                    cnt1--; }                }            }printf("%d\n", Cnt2); }return 0;}

Question C:
Title Address: Geometric progression
Test instructions: The number of geometric series of a sequence in which the k is the male ratio and the number of items is 3.
Idea: Here I used a map to open a dp[i][j] array, record the length of I with the number of J-ending numbers, so we can easily draw dp[i][j]+=dp[i-1][j/k],dp[1][j]=1. Because I use X%K maintenance in the program, so I want to push backwards. Because the data problem map does not open map

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;Const intmaxn=200010; LL A[MAXN];intMain () { map<LL,LL>dp[4];intN,k,i; LL ans; while(~scanf("%d%d", &n,&k)) {ans=0; for(i=0;i<4; i++) dp[i].clear (); for(i=1; i<=n;i++)scanf("%lld", &a[i]); for(i=1; i<=n;i++) {if(a[i]%k==0) {dp[3][a[i]]+=dp[2][A[I]/K]; dp[2][a[i]]+=dp[1][A[I]/K]; } dp[1][a[i]]++; } map<LL,LL>:: Iterator it; for(it=dp[3].begin (); it!=dp[3].end (); it++) ans+=it->second;printf("%lld\n", ans); }return 0;}

Question d:
Title Address: one-dimensional Battle Ships
Test instructions: Given an interval, each time a point is removed, it is required to be put down in the remaining blanks, K is a range (a interval cannot be absent). If it can be put down, then output-1, otherwise the output in the place of the first few points can not be put down.
Idea: the interval [l,r] removes one point x at a time, and the remaining interval is [l,x-1],[x+1,r]. Then find the number of ships that can be lowered for each interval: (interval length + 1)/(length of the boat + 1) {Because the ship is absent from the ship, so divided by the length of the boat is +1}. Two-point answer to find the first few points let the number of ships less than K.

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;Const intmaxn=200010;intX[MAXN];intX1[MAXN];intN,k,a,m;intCntintFind (intm) { for(intI=1; i<=m;i++) X1[i]=x[i]; Sort (x1+1, x1+m+1);intlen=0; Cnt=0; for(intI=1; i<=m;i++) {cnt+= (X1[i]-len)/(A +1);    Len=x1[i]; } cnt+= (n+1-len)/(A +1);if(cnt>=k)return 1;Else        return 0;}intMain () {scanf(" %d%d%d", &n,&k,&a);scanf("%d", &m); for(intI=1; i<=m;i++)scanf("%d", &x[i]);intL=1, r=m;intMidintRes=inf; while(L&LT;=R) {mid= (l+r) >>1;if(Find (mid)) l=mid+1;Else{r=mid-1;        Res=min (Res,mid); }    }//printf ("mid==%d\n", mid);    //printf ("l==%d\n", L);    if(l>m)puts("-1");Else        printf("%d\n", res);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #Pi (Div. 2) (A,B,C,D)

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.