Cod sursa(job #1601026)

Utilizator andreey_047Andrei Maxim andreey_047 Data 15 februarie 2016 17:47:19
Problema Garaj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 100008;

int N,M,cnt;
long long timp,s[nmax];

struct el{
 int t,nr;
}a[nmax];

inline bool cmp(const el &A, const el &B){
 return A.t < B.t;
}

inline bool Ok(long long T){
 long long i,k=M;

    for(i = 1; i <= N && k > 0; ++i)
        k=k-(a[i].nr)*1LL*(T / (2 * a[i].t));

  if(k<=0)
    return true;

  return false;
}

class Parser {
 public:
  Parser(const char *path) {
    in.open(path);
    in.read(buffer, kSize);
    cursor = 0;
  }
  Parser& operator>>(int &x) {
    x = 0;
    while (buffer[cursor] < '0')
      advance();
    while (buffer[cursor] >= '0') {
      x = x * 10 + buffer[cursor] - '0';
      advance();
    }
    return *this;
  }
 private:
  void advance() {
    if (++cursor == kSize) {
      in.read(buffer, kSize);
      cursor = 0;
    }
  }
  static const int kSize = 500000;
  char buffer[kSize];
  int cursor;
  ifstream in;
};

inline void Calc(){
 long long st,dr,mij;
 int h,i;
  st = 1; dr = 2000000000000;
  while(st <= dr){
    mij = (st + dr)/2;
    if(Ok(mij)){
        timp = mij;
        dr = mij-1;
    }
    else st = mij+1;

  }
  for(i = 1; i <= N; ++i)
       s[i] = (timp/(a[i].t*2))*1LL*a[i].nr;
    sort(s+1,s+N+1);
    i = N; h = M;
    while(h>0&&i)h-=s[i],++cnt,--i;
}

int main(){
    int i;
    Parser fin("garaj.in");
    ofstream fout("garaj.out");
    fin >> N >> M;
    for(i = 1; i <= N; ++i)
        fin >> a[i].nr >> a[i].t;

    sort(a+1,a+N+1,cmp);
    Calc();
    fout << timp <<' '<<cnt <<'\n';
    fout.close();
    return 0;
}