Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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;
}
posted on 2016-07-13 07:42  Tonix  阅读(330)  评论(0)    收藏  举报