zoukankan      html  css  js  c++  java
  • codeforce 589B枚举

    2017-08-25 12:00:53

    writer:pprp

    很简单的枚举,但是我调试了很长时间,出现各种各样的问题

    /*
    theme:cf 589B
    writer:pprp
    declare:枚举
    date:2017/8/25
    */
    
    #include <bits/stdc++.h>
    
    using namespace std;
    const int N = 4040;
    typedef long long ll;
    ll ans = -1, record_w = -1, record_h = -1;
    
    class rect
    {
    public:
        int w;
        int h;
    
        bool operator <(const rect & r2)
        {
            return w < r2.w;
        }
    
    };
    
    rect rec[N];
    
    
    int main()
    {
        int n;
        scanf("%d",&n);
    
        //input section
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%d%d",&rec[i].w, &rec[i].h);
            //w is bigger than h
            if(rec[i].w > rec[i].h)    //w > h??
                swap(rec[i].w,rec[i].h);
        }
        //sort the w
        sort(rec,rec + n);
    
        //define a vector to store the height
        vector<int> hh;
    
        //从小到大枚举w的长度
        for(int i = 0 ; i < n ; i++)
        {
            hh.clear();
            //将宽度高于w的对象的h储存在vector中
            for(int j = i  ; j < n ; j++)
                hh.push_back(rec[j].h);//一开始这里写成i了粗心犯的错
    
            //对高度进行排序
            sort(hh.begin(), hh.end());
    
            //记录当前高度
            int len = hh.size();
            
            //枚举当前w的情况下,采用不同的h的最佳解
            for(int j = 0 ; j < hh.size() ; j++, len--)
            {
                ll cmp = (ll)rec[i].w * hh[j] * len;    //wrong before: (ll)(rec[i].w * hh[j] * len) 这样就会越界,这个错误是调试出来的,如果都是ll就会溢出
                if(cmp > ans)
                {
                    ans = cmp;
                    record_h = hh[j];
                    record_w = rec[i].w;
                }
            }
        }
        cout << ans << endl;
        cout << record_w << " " << record_h << endl;
    
        return 0;
    }
  • 相关阅读:
    unity,C#,游戏面试笔试真题
    鼠标实现物体的移动
    UnityScript基础
    CocosCreator上的游戏(调试)发布到微信小程序
    Unity之与Web的交互
    unity之Layer作用
    unity_数据结构(常见数据结构及适用场景)
    unity之初级工程师
    虚拟机中安装GHO文件配置说明
    windows下tomcat的安装配置
  • 原文地址:https://www.cnblogs.com/pprp/p/7427344.html
Copyright © 2011-2022 走看看