Codeforces 589B Layer Cake(两次排序)

来源:互联网 发布:美国反恐战争数据图 编辑:程序博客网 时间:2024/06/01 20:53

题目地址: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 debuusing 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;}



0 0
原创粉丝点击