题解 AT4993 【[AGC034C] Tests】
Rainybunny · · 题解
更好的阅读体验 owo~
\mathcal{Description}
Link.
给定非负整数序列
所有输入均
\mathcal{Solution}
显然二分
假设
所以问题抽象为:有
仅需钦定
考虑贪心,不难证明:至多有一个
\mathcal{Code}
/* Clearink */
#include <cstdio>
#include <algorithm>
#define rep( i, l, r ) for ( int i = l, repEnd##i = r; i <= repEnd##i; ++i )
#define per( i, r, l ) for ( int i = r, repEnd##i = l; i >= repEnd##i; --i )
inline int rint () {
int x = 0, f = 1; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x * f;
}
typedef long long LL;
const int MAXN = 1e5;
int n, x, b[MAXN + 5], l[MAXN + 5], r[MAXN + 5], ord[MAXN + 5];
LL full[MAXN + 5];
inline LL contr ( const int i, const int s ) {
return s <= b[i] ? 1ll * s * l[i]
: 1ll * l[i] * b[i] + 1ll * ( s - b[i] ) * r[i];
}
inline void init () {
std::sort ( ord + 1, ord + n + 1, []( const int i, const int j ) {
return contr ( i, x ) > contr ( j, x );
} );
rep ( i, 1, n ) full[i] = full[i - 1] + contr ( ord[i], x );
}
inline LL calc ( const LL scr ) {
/*
* let's come up with a greedy algorithm!
* */
int fcnt = scr / x, rest = scr % x;
LL ret = 0;
rep ( i, 1, n ) { // score on exam <ord[i]> is <rest>.
LL cur = contr ( ord[i], rest );
if ( i > fcnt ) cur += full[fcnt];
else cur += full[fcnt + 1] - contr ( ord[i], x );
ret = cur > ret ? cur : ret;
}
return ret;
}
int main () {
n = rint (), x = rint ();
LL sum = 0, sb = 0;
rep ( i, 1, n ) {
ord[i] = i;
b[i] = rint (), l[i] = rint (), r[i] = rint ();
sum += 1ll * b[i] * l[i], sb += b[i];
}
init ();
LL lef = 0, rig = sb;
while ( lef < rig ) {
LL mid = lef + rig >> 1;
if ( calc ( mid ) >= sum ) rig = mid;
else lef = mid + 1;
}
printf ( "%lld\n", lef );
return 0;
}