• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LyonLys
愿意在角落唱沙哑的歌 再大声也都是给你 请用心听 不要说话 Contact me via E-mail: lyon.lys@gmail.com
博客园    首页    新随笔    联系   管理    订阅  订阅

hdu 3512 Perfect matching (mid-hard)

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

posted @ 2012-09-26 03:45  LyonLys  阅读(249)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3