Problem Link
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
int n, m;
ll a[101], b[101], c[101], d[101];
ll calculateCoconutsProduced(ll time)
{
ll coconutsProduced = 0;
for(int i = 0; i < n; i++)
{
coconutsProduced += (time - a[i] >= 0) ? (time - a[i]) / b[i] + 1 : 0;
}
return coconutsProduced;
}
ll calculateCoconutsConsumed(ll time)
{
ll coconutsConsumed = 0;
for(int i = 0; i < m; i++)
{
coconutsConsumed += (time - c[i] >= 0) ? (time - c[i]) / d[i] + 1 : 0;
}
return coconutsConsumed;
}
void solve(ll s, ll e)
{
ll endTime = e;
while(s < e)
{
ll mid = s + (e - s + 1) / 2;
ll coconutsProduced = calculateCoconutsProduced(mid);
ll coconutsConsumed = calculateCoconutsConsumed(endTime - mid);
if(coconutsProduced > coconutsConsumed)
{
e = mid - 1;
}
else
{
s = mid;
}
}
cout << s << endl;
}
int main()
{
ll t;
cin >> t >> n;
for(int i = 0; i < n; i++)
{
cin >> a[i] >> b[i];
}
cin >> m;
for(int i = 0; i < m; i++)
{
cin >> c[i] >> d[i];
}
solve(0, t);
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
int n, m;
ll a[101], b[101], c[101], d[101];
ll calculateCoconutsProduced(ll time)
{
ll coconutsProduced = 0;
for(int i = 0; i < n; i++)
{
coconutsProduced += (time - a[i] >= 0) ? (time - a[i]) / b[i] + 1 : 0;
}
return coconutsProduced;
}
ll calculateCoconutsConsumed(ll time)
{
ll coconutsConsumed = 0;
for(int i = 0; i < m; i++)
{
coconutsConsumed += (time - c[i] >= 0) ? (time - c[i]) / d[i] + 1 : 0;
}
return coconutsConsumed;
}
void solve(ll s, ll e)
{
ll endTime = e;
while(s < e)
{
ll mid = s + (e - s + 1) / 2;
ll coconutsProduced = calculateCoconutsProduced(mid);
ll coconutsConsumed = calculateCoconutsConsumed(endTime - mid);
if(coconutsProduced > coconutsConsumed)
{
e = mid - 1;
}
else
{
s = mid;
}
}
cout << s << endl;
}
int main()
{
ll t;
cin >> t >> n;
for(int i = 0; i < n; i++)
{
cin >> a[i] >> b[i];
}
cin >> m;
for(int i = 0; i < m; i++)
{
cin >> c[i] >> d[i];
}
solve(0, t);
return 0;
}
Comments
Post a Comment