Cod sursa(job #438761)

Utilizator Bogdan.CirsteaCirstea Bogdan-Ionut Bogdan.Cirstea Data 11 aprilie 2010 00:02:47
Problema Gutui Scor 30
Compilator cpp Status done
Runda teme_upb Marime 2.08 kb
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>


struct Apple {
  int h, w;

  friend bool operator<(Apple const& a, Apple const& b) {
    return a.h < b.h or (a.h == b.h and a.w < b.w);
  }
  friend bool operator>(Apple const& a, Apple const& b) {
    return b < a;
  }

  friend std::ostream& operator<<(std::ostream& s, Apple const& v) {
    s << '(' << v.h << ", " << v.w << ')';
    return s;
  }
};


template<class T, class C, T C::*M>
struct sum {
  T operator()(T const& cur, C const& next) { return cur + next.*M; }
};


int solve(std::vector<Apple> apples, int H, int U) {


  if (U == 0) {
    return std::accumulate(apples.begin(), apples.end(), 0, sum<int, Apple, &Apple::w>());
  }

  std::sort(apples.begin(), apples.end(), std::greater<Apple>());



  int picked_weight = 0;
  std::vector<int> available_weights;

  int offset = U - H % U;
  if (offset == U) offset = 0;
  int top = offset - U;


  while ((apples.size() or available_weights.size()) and top <= H) {

    if (available_weights.size()) {
      picked_weight += available_weights.front();
      std::pop_heap(available_weights.begin(), available_weights.end());
      available_weights.pop_back();
      top += U;
    }
    else {
      top += U * std::max(1, (apples.back().h - top) / U);
    }

    while (apples.size() and apples.back().h <= top) {
      available_weights.push_back(apples.back().w);
      std::push_heap(available_weights.begin(), available_weights.end());
      apples.pop_back();
    }
  }

  return picked_weight;
}




int main() {

	FILE* fd1;
	FILE* fd2;
	
	fd1=fopen("gutui.out", "w");
	fd2=fopen("gutui.in", "r");

	int nomberApples, height, step;
	fscanf(fd2, "%i %i %i", &nomberApples, &height, &step);

	Apple data[nomberApples]; 
	int i;
	for (i = 0; i < nomberApples; i++)
		fscanf(fd2, "%i %i ", &(data[i].h), &(data[i].w));

 	 std::vector<Apple> v (data, data + nomberApples); // ce e asta?
  	int actual = solve(v, height, step);
	fprintf(fd1, "%i ", actual);

	fclose(fd2);	
	fclose(fd1);
}