-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
3 | ||
1 8 | ||
2 5 | ||
1 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
10 |