이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX_M = 300;
int MAX_S;
int MIN_S;
const long long INF = 1e18;
int m;
unordered_map<int, long long> taken, untaken;
long long dpCrt[2 * MAX_M * MAX_M + 1], dpPrv[2 * MAX_M * MAX_M + 1];
void add( int x, int k ) {
swap( dpCrt, dpPrv );
if ( x > 0 ) {
for ( int r = 0; r < x; r++ ) {
deque<pair<int, int>> d;
for ( int i = 0, s = MIN_S + r; s <= MAX_S; i++, s += x ) {
if ( !d.empty() && i - d.front().second > k )
d.pop_front();
while ( !d.empty() && dpPrv[s - MIN_S] - i >= dpPrv[d.back().first - MIN_S] - d.back().second )
d.pop_back();
d.push_back( { s, i } );
dpCrt[s - MIN_S] = dpPrv[d.front().first - MIN_S] + i - d.front().second;
}
}
} else {
x = -x;
for ( int r = 0; r < x; r++ ) {
deque<pair<int, int>> d;
for ( int i = 0, s = MAX_S - r; s >= MIN_S; i++, s -= x ) {
if ( !d.empty() && i - d.front().second > k )
d.pop_front();
while ( !d.empty() && dpPrv[s - MIN_S] - i >= dpPrv[d.back().first - MIN_S] - d.back().second )
d.pop_back();
d.push_back( { s, i } );
dpCrt[s - MIN_S] = dpPrv[d.front().first - MIN_S] + i - d.front().second;
}
}
}
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s - MIN_S] = (dpCrt[s - MIN_S] <= -INF + m ? -INF : dpCrt[s - MIN_S]);
}
void substract( int x, int k ) {
swap( dpCrt, dpPrv );
if ( x < 0 ) {
x = -x;
for ( int r = 0; r < x; r++ ) {
deque<pair<int, int>> d;
for ( int i = 0, s = MIN_S + r; s <= MAX_S; i++, s += x ) {
if ( !d.empty() && i - d.front().second > k )
d.pop_front();
while ( !d.empty() && dpPrv[s - MIN_S] + i >= dpPrv[d.back().first - MIN_S] + d.back().second )
d.pop_back();
d.push_back( { s, i } );
dpCrt[s - MIN_S] = dpPrv[d.front().first - MIN_S] - i + d.front().second;
}
}
} else {
for ( int r = 0; r < x; r++ ) {
deque<pair<int, int>> d;
for ( int i = 0, s = MAX_S - r; s >= MIN_S; i++, s -= x ) {
if ( !d.empty() && i - d.front().second > k )
d.pop_front();
while ( !d.empty() && dpPrv[s - MIN_S] + i >= dpPrv[d.back().first - MIN_S] + d.back().second )
d.pop_back();
d.push_back( { s, i } );
dpCrt[s - MIN_S] = dpPrv[d.front().first - MIN_S] - i + d.front().second;
}
}
}
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s - MIN_S] = (dpCrt[s - MIN_S] <= -INF + m ? -INF : dpCrt[s - MIN_S]);
}
int main() {
long long l, u = 0, t = 0;
cin >> m >> l;
MIN_S = -m * m;
MAX_S = m * m;
for ( int i = -m; i <= m; i++ ) {
cin >> untaken[i];
u += untaken[i];
}
if ( l < 0 ) {
for ( int i = 1; i <= m; i++ )
swap( untaken[i], untaken[-i] );
l = -l;
}
long long s = 0;
for ( int i = -m; i <= 0; i++ ) {
long long x = untaken[i];
s += i * x;
taken[i] += x;
untaken[i] -= x;
t += x;
u -= x;
}
for ( int i = 1; i <= m; i++ ) {
long long x = min( untaken[i], (l - s) / i );
s += i * x;
taken[i] += x;
untaken[i] -= x;
t += x;
u -= x;
}
if ( s + m < l ) {
for ( int i = -m; i < 0; i++ ) {
long long x = min( taken[i], (l - s) / (-i) );
s -= i * x;
taken[i] -= x;
untaken[i] += x;
t -= x;
u += x;
}
}
/*cout << s << "\n";
for ( int i = -m; i <= m; i++ )
cout << i << " " << taken[i] << " " << untaken[i] << "\n";*/
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s - MIN_S] = -INF;
dpCrt[0 - MIN_S] = 0;
for ( int i = -m; i <= m; i++ ) {
add( i, min( (long long)m, untaken[i] ) );
substract( i, min( (long long)m, taken[i] ) );
}
if ( l - s > MAX_S || dpCrt[l - s - MIN_S] == -INF )
cout << "impossible\n";
else
cout << t + dpCrt[l - s - MIN_S] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |