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

待更新...

猜你喜欢

转载自www.cnblogs.com/heyuhhh/p/10217337.html
今日推荐