zoukankan      html  css  js  c++  java
  • hdu 3512 Perfect matching (midhard)

    http://acm.hdu.edu.cn/showproblem.php?pid=3512

      贪心是我的短柄啊!WA了十几次,最终迎来了AC。。。囧!

      这题是要找到每种匹配情况中两权值的积的最大值中最小的那种情况。先排序,然后从绝对值大到小,将异号的两个权值乘起来,然后再处理剩下的同号的那些。

    代码如下:

    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <vector>
     5 
     6 using namespace std;
     7 typedef __int64 ll;
     8 typedef vector<ll> vll;
     9 
    10 vll rec1, rec2;
    11 
    12 bool cmp(ll _a, ll _b){
    13     return _a > _b;
    14 }
    15 
    16 int main() {
    17     int n;
    18     ll x;
    19 
    20 //freopen("in", "r",stdin);
    21     while (~scanf("%d", &n)) {
    22         rec1.clear();
    23         rec2.clear();
    24         for (int i = 0; i < n; i++){
    25             scanf("%I64d", &x);
    26             rec1.push_back(x);
    27         }
    28         sort(rec1.begin(), rec1.end());
    29         for (int i = 0; i < n; i++){
    30             scanf("%I64d", &x);
    31             rec2.push_back(x);
    32         }
    33         sort(rec2.begin(), rec2.end(), cmp);
    34 
    35         vll::iterator i1 = rec1.begin(), i2 = rec2.begin();
    36         ll ans = -((ll)1 << 63);
    37 
    38         while (i1 != rec1.end() && (*i1) < 0 && (*i2) > 0) i1++, i2++;
    39         reverse(rec1.begin(), i1);
    40         while (i1 != rec1.end() && ((*i1) <= 0 || (*i2) >= 0)) i1++, i2++;
    41         reverse(i1, rec1.end());
    42 
    43         i1 = rec1.begin();
    44         i2 = rec2.begin();
    45         while (i1 != rec1.end() && i2 != rec2.end()){
    46             ans = max(ans, (*i1) * (*i2));
    47             i1++; i2++;
    48         }
    49 
    50         printf("%I64d\n", ans);
    51     }
    52 
    53     return 0;
    54 }

      yuan神博客的解释比较详细,那个图很直观,如果有不懂的可以去那里看!

    ——written by Lyon

  • 相关阅读:
    vue中select设置默认选中
    验证码
    JS图片src转义
    int main(int argc, char** argv) 以及CommandLineParser
    Visual Studio2013 配置opencv3.3.0 x64系统
    ubuntu16.04 下安装 visual studio code 以及利用 g++ 运行 c++程序
    第三次作业
    第二次作业
    作业一
    第四次作业
  • 原文地址:https://www.cnblogs.com/LyonLys/p/hdu_3512_Lyon.html
Copyright © 2011-2022 走看看