zoukankan      html  css  js  c++  java
  • bzoj1034题解

    【解题思路】

      广义田忌赛马的贪心模型。如果当前实力最差的马比对手实力最差的马强,则匹配;如果当前实力最强的马比对手实力最强的马强,亦匹配;若上述两点均不成立,拿己方最差的马去匹配对手最强的马。复杂度O(nlog2n)。

    【参考代码】

     1 #include <algorithm>
     2 #include <cstdio>
     3 #define REP(i,low,high) for(register int i=(low);i<=(high);++i)
     4 #define __function__(type) __attribute__((optimize("-O2"))) inline type
     5 #define __procedure__ __attribute__((optimize("-O2"))) inline void
     6 using namespace std;
     7  
     8 __function__(int) score(const int&x,const int&y) {return x==y?1:(x>y)*2;}
     9  
    10 static int n; int a[100010],b[100010];
    11  
    12 int main()
    13 {
    14     scanf("%d",&n); REP(i,1,n) scanf("%d",a+i); REP(i,1,n) scanf("%d",b+i);
    15     sort(a+1,a+n+1),sort(b+1,b+n+1); int la=1,ra=n,lb=1,rb=n,ans=0;
    16     while(la<=ra)
    17     {
    18         if(a[la]>b[lb]) {ans+=score(a[la++],b[lb++]); continue;}
    19         if(a[ra]>b[rb]) {ans+=score(a[ra--],b[rb--]); continue;}
    20         ans+=score(a[la++],b[rb--]);
    21     }
    22     printf("%d ",ans),la=1,ra=n,lb=1,rb=n,ans=0;
    23     while(la<=ra)
    24     {
    25         if(a[la]<b[lb]) {ans+=score(a[la++],b[lb++]); continue;}
    26         if(a[ra]<b[rb]) {ans+=score(a[ra--],b[rb--]); continue;}
    27         ans+=score(a[ra--],b[lb++]);
    28     }
    29     return printf("%d
    ",ans),0;
    30 }
    View Code
  • 相关阅读:
    css3中的位置移动
    css中伪元素选择器
    css中伪类选择器
    html5之属性选择器
    html5的文本属性
    html5的视频和音频使用
    常用的linux命令
    常见的http状态码
    springboot之安装和启动es
    linux版本的jdk安装
  • 原文地址:https://www.cnblogs.com/spactim/p/6538937.html
Copyright © 2011-2022 走看看