1. 程式人生 > >Codeforces:Good Bye 2018(題解)

Codeforces:Good Bye 2018(題解)

 

Good Bye 2018!

題目連結:https://codeforces.com/contest/1091

A. New Year and the Christmas Ornament

題意:

給出三堆數量分別為y,b,r的東西,現在要你從三堆中各選一堆,滿足y'+1=b'且b'+1=r' (y',r',b'分別是指從中選取的個數)。

現在問最多能拿出的個數為多少。

 

題解:

我是直接模擬的= =但是有更簡單的方法。

讓y+=2,b+=1,那麼現在的最優解為min(y,b,r)*3-3。這個還是很好證明的。

 

程式碼如下:

#include <bits/stdc++.h>
using
namespace std; typedef long long ll; int n,y,b,r; int ans = 0; void Print(int x,int z,int t){ ans=max(x+z+t,ans); } int ok(int o,int p,int q){ return o<=y && p<=b && q<=r; } int main(){ cin>>y>>b>>r; if(ok(r-2,r-1,r)) Print(r-2,r-1
,r); else if(ok(b-1,b,b+1)) Print(b-1,b,b+1); else Print(y,y+1,y+2); cout<<ans; return 0; }
View Code

 

B. New Year and the Treasure Geolocation

題意:

給出兩個種類的點,每個種類的點有n個,現在要求一個目的點,滿足兩類點中,各存在一個點,它們的橫縱座標之和等於目的點的橫縱座標。

題目保證存在這個目的點。

 

題解:

我想的是排序後,最小的x加上最大的x即為目的點的橫座標,對於縱座標也同理。

因為假設x1<x2<...<xn-1<xn,現在選取的兩個點是x2,xn,那麼對於其它的選擇,肯定有個xt與x1匹配,又因x1<x2,xt<xn,就有x1+xt<x2+xn,這時不符合題意。

還有一種更為簡單的方法,直接取均值即可,因為題目保證有解,目的點的橫座標就可以為(x1+x2+...x2*n)/n。

其實兩種方式的本質都是一樣的。

 

程式碼如下:

#include <bits/stdc++.h>
using namespace  std;
typedef long long ll;
const int N = 1005;
int n;
struct node{
    int x,y;
    bool operator < (const node &A)const{
        return A.x==x ? y<A.y : x<A.x;
    }
}p1[N],p2[N];
int main(){
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d%d",&p1[i].x,&p1[i].y);
    for(int i=1;i<=n;i++) scanf("%d%d",&p2[i].x,&p2[i].y);
    sort(p1+1,p1+n+1);sort(p2+1,p2+n+1);
    cout<<p1[1].x+p2[n].x<<" "<<p1[1].y+p2[n].y;
    return 0;
}
View Code

待更新...