hihocoder #1499 : A Box of Coins 貪心

描述

Little Hi has a box which consists of 2xN cells as illustrated below.

+----+----+----+----+----+----+
| A1 | A2 | A3 | A4 | .. | AN |
+----+----+----+----+----+----+
| B1 | B2 | B3 | B4 | .. | BN |
+----+----+----+----+----+----+

There are some coins in each cell. For the first row the amounts of coins are A1, A2, ... AN and for the second row the amounts are B1, B2, ... BN.

Each second Little Hi can pick one coin from a cell and put it into an adjacent cell. (Two cells are adjacent if they share a common side. For example, A1 and A2 are adjacent; A1 and B1 are adjacent; A1 and B2 are not adjacent.)

Now Little Hi wants that each cell has equal number of coins by moving the coins. He wants to know the minimum number of seconds he needs to accomplish it.  

輸入

The first line contains an integer, N. 2 <= N <= 100000  

Then follows N lines. Each line contains 2 integers Ai and Bi. (0 <= Ai, Bi <= 2000000000)  

It is guaranteed that the total amount of coins in the box is no more than 2000000000 and is a multiple of 2N.

輸出

The minimum number of seconds.

樣例輸入
2
3 4
6 7
樣例輸出
4

.

題意 給出兩行 A B 格子 裏面放有一定數量的硬幣

要求使得最後每個格子的硬幣數量一樣(輸入保證合法)

移動每次只允許硬幣在上下左右相鄰的格子之間移動

求最少步數

貪心:

從最左邊考慮,如果上下之間有一個有盈餘,必定是相互補充最優。

其次 如果上下兩個都有富餘,則都往右邊輸送

再者 如果上下之間都缺乏,則都向左借

模擬即可  .

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll n,q;
ll A[2][100050];
int main()
{
     int n;
     cin>>n;
     ll tol=0;
     for(int i=1;i<=n;i++)
     {
         scanf("%lld%lld",&A[0][i],&A[1][i]);
        tol+=A[0][i]+A[1][i];
     }
     tol/=2*n;
     ll sum=0;
     ll nn=n;
     n=tol;
     for(int i=1;i<=nn;i++)
     {
         if (A[0][i]>n)        //superfluous
         {
             ll res=A[0][i]-n;
             if (A[1][i]<n&&res>0)
             {
                 ll tmp=min(res,(n-A[1][i]));
                 A[0][i]-=tmp;
                 sum+=tmp;
                 res-=tmp;
                 A[1][i]+=tmp;
             }
             if (res>0)
             {
                 A[0][i+1]+=res;
                 sum+=res;
                 A[0][i]=n;
             }
         }
         if (A[1][i]>n)
         {
             ll res=A[1][i]-n;
             if (A[0][i]<n&&res>0)
             {
                 ll tmp=min(res,(n-A[0][i]));
                 A[1][i]-=tmp;
                 sum+=tmp;
                 res-=tmp;
                 A[0][i]+=tmp;

             }
              if (res>0)
             {
                 A[1][i+1]+=res;
                 sum+=res;
                 A[1][i]=n;
             }
         }
         if (A[0][i]<n)
         {
             ll need=n-A[0][i];
             if (need>0)
             {
                 A[0][i+1]-=need;
                 sum+=need;
                 A[0][i]=n;
             }
         }
         if (A[1][i]<n)
         {
             ll need=n-A[1][i];
             if (need>0)
             {
                 A[1][i+1]-=need;
                 sum+=need;
                 A[1][i]=n;
             }
         }

     }
     printf("%lld\n",sum);

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章