이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
struct piece
{
long long V;
long long C;
};
bool operator < (piece A, piece B)
{
return A.C < B.C;
}
long long res = -1'000'000'000'000'000'000LL;
int N, M;
vector<piece> P;
int L;
int R; //exclusive endpoints
multiset<long long> high_values;
multiset<long long> low_values;
long long high_values_sum;
void insert_to_set(long long insert_value)
{
high_values.insert(insert_value);
high_values_sum += insert_value;
if(high_values.size() > M-2)
{
high_values_sum -= *high_values.begin();
low_values.insert(*high_values.begin());
high_values.erase(high_values.find(*high_values.begin()));
}
}
void expand_left()
{
L--;
insert_to_set(P[L+1].V);
}
void expand_right()
{
R++;
insert_to_set(P[R-1].V);
}
void contract_left()
{
long long remove_value = P[L+1].V;
L++;
if(low_values.find(remove_value) != low_values.end())
{
low_values.erase(low_values.find(remove_value));
}
else
{
high_values.erase(high_values.find(remove_value));
high_values_sum -= remove_value;
if(high_values.size() < M-2 && low_values.size() >= 1)
{
high_values_sum += *low_values.rbegin();
high_values.insert(*low_values.rbegin());
low_values.erase(low_values.find(*low_values.rbegin()));
}
}
}
void contract_right()
{
// cerr << "contract right\n";
// cerr << "original R = " << R << '\n';
long long remove_value = P[R-1].V;
R--;
if(low_values.find(remove_value) != low_values.end())
{
// cerr << "case 1 entered\n";
low_values.erase(low_values.find(remove_value));
// cerr << "case 1 successful\n";
}
else
{
// cerr << "case 2 entered\n";
// cerr << remove_value << '\n';
// for(long long h: high_values) cerr << h << ' ';
// cerr << '\n';
high_values.erase(high_values.find(remove_value));
high_values_sum -= remove_value;
// cerr << "X\n";
if(high_values.size() < M-2 && low_values.size() >= 1)
{
high_values_sum += *low_values.rbegin();
high_values.insert(*low_values.rbegin());
low_values.erase(low_values.find(*low_values.rbegin()));
}
// cerr << "case 2 successful\n";
}
}
long long get_curr_score()
{
// cerr << "\n\n";
// cerr << L << ' ' << R << ' ' << ' ' << P[L].V + high_values_sum + P[R].V - 2*(P[R].C - P[L].C) << '\n';
// cerr << P[L].V << ' ' << P[R].V << ' ' << P[L].C << ' ' << P[R].C << '\n';
// cerr << "H: ";
// for(long long h: high_values) cerr << h << ' ';
// cerr << "\n";
return P[L].V + high_values_sum + P[R].V - 2*(P[R].C - P[L].C);
}
void solve(int l1, int l2, int r1, int r2)
{
int l = (l1+l2)/2;
// cerr << "\n\n\n";
// cerr << "solve " << l1 << ' ' << l2 << ' ' << r1 << ' ' << r2 << '\n';
// cerr << "l = " << l << '\n';
while(l < L)
expand_left();
// cerr << "A\n";
while(R < max(r1, l+M-1))
expand_right();
// cerr << "B\n";
while(L < l)
contract_left();
// cerr << "C\n";
// cerr << r1 << ' ' << l << ' ' << M << ' ' << l+M-1 << ' ' << R << '\n';
while(max(r1, l+M-1) < R)
contract_right();
// cerr << "D\n";
// cerr << "L, R = " << L << ' ' << R << '\n';
int bestR = R;
long long best_score = get_curr_score();
while(R+1 <= r2)
{
// cerr << "! ";
// cerr << R << ' ' << r2 << '\n';
expand_right();
if(get_curr_score() > best_score)
{
best_score = get_curr_score();
bestR = R;
}
}
// cerr << l << ' ' << bestR << ' ' << best_score << '\n';
res = max(res, best_score);
if(l1 <= l-1)
solve(l1, l-1, r1, bestR);
if(l+1 <= l2)
solve(l+1, l2, bestR, r2);
}
int main()
{ ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> N >> M;
P = vector<piece>(N);
for(int i = 0; i < N; i++) cin >> P[i].V >> P[i].C;
sort(P.begin(), P.end());
L = 0;
R = 1;
solve(0, N-M, M-1, N-1);
cout << res << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'void insert_to_set(long long int)':
cake3.cpp:34:27: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | if(high_values.size() > M-2)
| ~~~~~~~~~~~~~~~~~~~^~~~~
cake3.cpp: In function 'void contract_left()':
cake3.cpp:67:31: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
67 | if(high_values.size() < M-2 && low_values.size() >= 1)
| ~~~~~~~~~~~~~~~~~~~^~~~~
cake3.cpp: In function 'void contract_right()':
cake3.cpp:98:31: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
98 | if(high_values.size() < M-2 && low_values.size() >= 1)
| ~~~~~~~~~~~~~~~~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |