zoukankan      html  css  js  c++  java
  • 题解——Codeforces Round #508 (Div. 2) T3 (贪心)

    贪心的选取最优解

    然后相减好

    记得要开long long

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <set>
    #include <queue>
    #define int long long
    using namespace std;
    int ansa=0,ansb=0,posa=1,posb=1,n,a[1000100],b[1000100];
    bool cmp(int a,int b){
      if(a>b)
        return true;
      else
        return false;
    }
    signed main(){
      scanf("%I64d",&n);
      for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
      for(int i=1;i<=n;i++)
        scanf("%I64d",&b[i]);
      sort(a+1,a+n+1,cmp);
      sort(b+1,b+n+1,cmp);
      int whi=0;// 0 a 1 b
      while(posa<=n||posb<=n){
        if(whi==0){
          if(a[posa]>b[posb]&&posa<=n){
            ansa+=a[posa];
            posa++;
            whi=1-whi;
          }
          else if(a[posa]<=b[posb]&&posb<=n){
            posb++;
            whi=1-whi;
          }
          else if(posb<=n){
            posb++;
            whi=1-whi;
          }
          else if(posa<=n){
            ansa+=a[posa];
            posa++;
            whi=1-whi;
          }
        }
        else{
          if(b[posb]>a[posa]&&posb<=n){
            ansb+=b[posb];
            posb++;
            whi=1-whi;
          }
          else if(b[posb]<=a[posa]&&posa<=n){
            posa++;
            whi=1-whi;
          }
          else if(posa<=n){
            posa++;
            whi=1-whi;
          }
          else if(posb<=n){
            ansb+=b[posb];
            posb++;
            whi=1-whi;
          }
        }
      }
      printf("%I64d",ansa-ansb);
      return 0;
    }
  • 相关阅读:
    laravel blog 一
    dede:channelartlist currentstyle高亮显示
    dede调用导航/幻灯
    rmp 安装LNMP环境
    无极分类之子孙树、父辈树
    创始人 密码
    mysql报错显示法文解决办法
    渐变色背景
    laravel sendmail
    工作流系统webservice服务
  • 原文地址:https://www.cnblogs.com/dreagonm/p/9602510.html
Copyright © 2011-2022 走看看