码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank "Fair Rations"

时间:2016-07-13 08:01:36      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

Another fun Greedy problem to work on: we simply go from first to second last person, as long someone is odd, we distribute bread to her and her next.

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<int> B(N);
    for(int B_i = 0;B_i < N;B_i++){
       cin >> B[B_i];
    }
    
    int cnt = 0;
    for(int i = 0; i < N - 1; i ++)
    {
        if(B[i] % 2)
        {
            B[i] ++;
            B[i+1]++;
            cnt += 2;
        }
    }
    
    if(B.back() % 2) 
        cout << "NO" << endl;
    else
        cout << cnt << endl;

    return 0;
}

HackerRank "Fair Rations"

标签:

原文地址:http://www.cnblogs.com/tonix/p/5665562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!