Skip to content

Commit

Permalink
pairup
Browse files Browse the repository at this point in the history
  • Loading branch information
megargayu committed Apr 28, 2022
1 parent 40f1e48 commit c1651e3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
50 changes: 50 additions & 0 deletions contests/silver/2017/usopen/pairup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;

#define MAX_N 1e5

struct Cow
{
int time, num;
};

vector<Cow> cows(MAX_N);
int main()
{
ifstream fin("pairup.in");
ofstream fout("pairup.out");

int N;
fin >> N;

for (int i = 0; i < N; ++i)
fin >> cows[i].num >> cows[i].time;

sort(cows.begin(), cows.begin() + N, [](const Cow &a, const Cow &b)
{ return a.time < b.time; });

int p1 = 0, p2 = N - 1, maxPair = 0;
while (p1 <= p2)
{
maxPair = max(maxPair, cows[p1].time + cows[p2].time);

// skip the rest of calculations between p1 and p2, as they will always
// be the same as the current sum
if (cows[p1].num < cows[p2].num)
{
cows[p2].num -= cows[p1].num;
++p1;
}
else if (cows[p2].num < cows[p1].num)
{
cows[p1].num -= cows[p2].num;
--p2;
}
else ++p1, --p2;
}


fout << maxPair << '\n';

return 0;
}
4 changes: 4 additions & 0 deletions contests/silver/2017/usopen/pairup.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
1 8
2 5
1 2
1 change: 1 addition & 0 deletions contests/silver/2017/usopen/pairup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10

0 comments on commit c1651e3

Please sign in to comment.