Codeforces Round #252 (Div. 2) B. Valera and Fruits

OpenSoucre發表於2014-06-09
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

int main(){
    int n,v;
    cin >> n >>v;
    map<int,int> fruit;
    for(int i = 0 ; i < n; ++ i ){
        int a,b;
        cin >>a >> b;
        if(fruit.find(a)!=fruit.end()) fruit[a]+=b;
        else fruit.insert(make_pair(a,b));
    }

    int pre_a = 0, left_b = 0, collect_fruit = 0;
    for(map<int,int>::iterator iter = fruit.begin(); iter!=fruit.end(); ++ iter){
        if(iter->first - pre_a > 1){
            collect_fruit += (left_b > v)? v : left_b;
            left_b  = 0;
        }
        if(left_b >= v){
            collect_fruit +=v;
            left_b = iter->second;
        }else{
            collect_fruit +=left_b;
            if(iter->second >= (v-left_b)){
                collect_fruit +=v-left_b;
                left_b = iter->second-v+left_b;
            }else{
                collect_fruit += iter->second;
                left_b = 0;
            }
        }
        pre_a = iter->first;
    }
    collect_fruit+=(left_b > v) ? v : left_b;
    cout<<collect_fruit<<endl;
}

 

相關文章