類比(思維)

來源:互聯網
上載者:User

標籤:選擇   names   print   class   type   main   div   ++   scan   

codeforces 1038 c

題意:

兩個人,各有一堆數,每個人都有兩種選擇,1:把自己的最大的數加到和裡。2:丟掉對方的一個數(最大)。

求第一個人的和 - 第二個人和。

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>typedef long long ll;using namespace std;int main(){    ll n;    ll a[100010],b[100010],i,j,ans1=0,ans2=0;    scanf("%lld",&n);    for(i=1;i<=n;i++)        scanf("%lld",&a[i]);    for(i=1;i<=n;i++)        scanf("%lld",&b[i]);    sort(a+1,a+n+1);    sort(b+1,b+n+1);    int flag=1;    i=n;j=n;    while(i!=0||j!=0)    {        if(flag)        {            flag=0;            if(a[i]>b[j])            {                ans1+=a[i];                i--;            }            else                j--;        }        else        {            flag=1;            if(b[j]>a[i])            {                 ans2+=b[j];                 j--;            }            else                i--;        }    }    printf("%lld\n",ans1-ans2);}

 

類比(思維)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.