#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 200000 + 10;
const llong INF = 1e18;
int n, m;
struct Dish
{
int time;
llong limit;
int reward;
int idx;
bool type;
};
Dish a[MAXN];
Dish b[MAXN];
llong prefixA[MAXN];
llong prefixB[MAXN];
bool isNext[MAXN];
bool isActive[MAXN];
llong dp[MAXN];
int globalRow;
llong findValue(int col)
{
if (isNext[col])
{
return findValue(col + 1) + (isActive[col] ? b[col].reward : 0);
}
return dp[col];
}
void fix(int col)
{
assert(col <= m);
llong curr = dp[col];
llong next = findValue(col + 1) + (isActive[col] ? b[col].reward : 0);
if (curr > next) isNext[col] = false;
else isNext[col] = true;
}
void applyUpdate(int to, int val)
{
for (int i = 1 ; i <= to ; ++i)
{
dp[i] += val;
}
}
std::vector <int> activateAt[MAXN];
void solve()
{
for (int i = 1 ; i <= n ; ++i)
{
prefixA[i] = prefixA[i - 1] + a[i].time;
}
for (int i = 1 ; i <= m ; ++i)
{
prefixB[i] = prefixB[i - 1] + b[i].time;
}
for (int i = 1 ; i <= m ; ++i)
{
int l = 0, r = n + 2, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (prefixA[mid - 1] + prefixB[i] <= b[i].limit) l = mid;
else r = mid;
}
activateAt[l].push_back(i);
// std::cout << "here: " << i << ' ' << l << '\n';
}
globalRow = n + 1;
std::fill(isNext + 1, isNext + m + 1, true);
std::sort(activateAt[n + 1].begin(), activateAt[n + 1].end(), std::greater <int> ());
for (const int &idx : activateAt[globalRow])
{
isActive[idx] = true;
fix(idx);
}
for (globalRow = n ; globalRow >= 1 ; --globalRow)
{
for (const int &idx : activateAt[globalRow])
{
dp[idx] = findValue(idx);
}
int l = 0, r = m + 1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (prefixA[globalRow] + prefixB[mid - 1] <= a[globalRow].limit) l = mid;
else r = mid;
}
if (l > 0)
{
dp[l] = findValue(l);
}
for (const int &idx : activateAt[globalRow])
{
isActive[idx] = true;
}
if (l > 0) activateAt[globalRow].push_back(l);
applyUpdate(l, a[globalRow].reward);
std::sort(activateAt[globalRow].begin(), activateAt[globalRow].end(), std::greater <int> ());
for (const int &idx : activateAt[globalRow])
{
// std::cout << "fix: " << fix << '\n';
fix(idx);
if (idx > 1) fix(idx - 1);
}
// std::cout << "dp after: " << globalRow << '\n';
// for (int i = 1 ; i <= m ; ++i)
// {
// std::cout << findValue(i) << ' ';
// }
// std::cout << '\n';
// std::cout << "active\n";
// for (int i = 1 ; i <= m ; ++i)
// {
// std::cout << isActive [i] << ' ';
// }
// std::cout << '\n';
// std::cout << "next\n";
// for (int i = 1 ; i <= m ; ++i)
// {
// std::cout << isNext[i] << ' ';
// }
// std::cout << '\n';
}
globalRow++;
std::cout << findValue(1) << '\n';
}
void input()
{
std::cin >> n >> m;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> a[i].time >> a[i].limit >> a[i].reward;
a[i].idx = i;
a[i].type = false;
}
for (int i = 1 ; i <= m ; ++i)
{
std::cin >> b[i].time >> b[i].limit >> b[i].reward;
b[i].idx = i;
a[i].type = true;
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
10102 ms |
42068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10884 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10884 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10884 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10884 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10884 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
10102 ms |
42068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
10102 ms |
42068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |