[Coursera]演算法基礎_Week8_二分與貪心_Q2

yiliu_cr發表於2015-12-14
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
struct Day {
	int C, Y;
	Day(int C_,int Y_):C(C_),Y(Y_){}
};
vector<Day> v;

int main() {
	int N, S;
	cin >> N >> S;
	for (int i = 1; i <= N; i++) {
		int Ci, Yi;
		cin >> Ci >> Yi;
		v.push_back(Day(Ci, Yi));
	}
	long int res = 0;
	vector<Day>::iterator i = v.end();
	while (i != v.begin()) {
		i--;
		vector<Day>::iterator j = i;
		if (j != v.begin()) {
			j--;
			if (i->C > j->C + S)
				res += (j->C + S)*i->Y;
			else
				res += i->C*i->Y;
		}
		else
			res += i->C*i->Y;
	}
	cout << res << endl;
	return 0;
}

相關文章