码迷,mamicode.com
首页 > 编程语言 > 详细

Codeforces 589B Layer Cake(两次排序)

时间:2016-08-10 21:12:44      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

题目地址:http://codeforces.com/problemset/problem/589/B

思路:设长<=宽,将各物体按长从小到大排序。从后向前枚举长,同时将宽加入并排序。则对于位置 j 的宽,有 num-j 物体宽大于它(由于长从小到大枚举,保证新加入物体的长不大于当前物体的长),则体积 a[i].r*b[j]*(num-j) 每次取最大即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const int maxn=4000+50;
typedef long long LL;
struct Node
{
    int r,c;
};
int b[maxn];
Node a[maxn];
int cmp(Node a,Node b)
{
    if(a.r==b.r) return a.c<b.c;
    else return a.r<b.r;
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        a[i].r=min(x,y);
        a[i].c=max(x,y);
    }
    sort(a,a+n,cmp);
    LL ans=0;
    int num=0,ansr,ansc;
    for(int i=n-1; i>=0; i--)
    {
        b[num++]=a[i].c;
        sort(b,b+num);
        for(int j=0; j<num; j++)
        {
            LL tmp=(LL)a[i].r*b[j]*(num-j);
            if(tmp>ans)
            {
                ans=tmp;
                ansr=a[i].r;
                ansc=b[j];
            }
        }
    }
    printf("%I64d\n",ans);
    printf("%d %d\n",ansr,ansc);
    return 0;
}



Codeforces 589B Layer Cake(两次排序)

标签:

原文地址:http://blog.csdn.net/wang2147483647/article/details/52175501

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!