이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <stack>
#include <iomanip>
#include <bitset>
#include <map>
#include <cassert>
#include <array>
#include <queue>
#include <cstring>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define pqueue priority_queue
#define pb(x) push_back(x)
// #define endl '\n'
#define all(x) x.begin(), x.end()
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
const int inf = 1e9 + 228;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-14;
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("inputik.txt", "r", stdin);
// freopen("outputik.txt", "w", stdout);
}
vpii kek;
int n, m;
int ans = 0;
int all_ans = -inf*inf;
int cl = 0, cr = -1;
multiset<int> fir, sec;
int get(int l, int r){
while(cr < r){
cr++;
// cout << " + " << kek[cr].second << endl;
sec.insert(kek[cr].second);
ans += kek[cr].second;
if(sec.size() > m-2){
ans -= *sec.begin();
fir.insert(*sec.begin());
sec.erase(sec.begin());
}
}
while(cl > l){
cl--;
// cout << " + " << kek[cl].second << endl;
sec.insert(kek[cl].second);
ans += kek[cl].second;
if(sec.size() > m-2){
ans -= *sec.begin();
fir.insert(*sec.begin());
sec.erase(sec.begin());
}
}
while(cr > r){
auto it = sec.find(kek[cr].second);
// cout << " - " << kek[cr].second << endl;
if(it == sec.end()){
fir.erase(fir.find(kek[cr].second));
} else{
ans -= *it;
sec.erase(it);
if(fir.size()){
sec.insert(*(--fir.end()));
ans += *(--fir.end());
fir.erase(--fir.end());
}
}
cr--;
}
while(cl < l){
// cout << " - " << kek[cl].second << endl;
auto it = sec.find(kek[cl].second);
if(it == sec.end()){
fir.erase(fir.find(kek[cl].second));
} else{
ans -= *it;
sec.erase(it);
if(fir.size()){
sec.insert(*(--fir.end()));
ans += *(--fir.end());
fir.erase(--fir.end());
}
}
cl++;
}
return ans;
}
void calc(int l, int r, int tl, int tr){
if(l > r)
return;
int mid = (l + r)/2;
int res = -inf*inf, pos = tr;
for(int i=max(mid+m-1, tl); i<=tr; i++){
if(res <= get(mid+1, i-1) + kek[i].second + kek[mid].second - (kek[i].first - kek[mid].first)*2){
res = ans + kek[i].second + kek[mid].second - (kek[i].first - kek[mid].first)*2;
pos = i;
}
}
all_ans = max(all_ans, res);
calc(l, mid-1, tl, pos);
calc(mid+1, r, pos, tr);
}
void solve(){
cin >> n >> m;
kek.resize(n);
for(int i=0; i<n; i++){
int a, b;
cin >> a >> b;
kek[i] = make_pair(b, a);
}
sort(all(kek));
calc(0, n-1, 0, n-1);
cout << all_ans << endl;
}
/*
5 3
2 1
4 2
6 4
8 8
10 16
======
8 4
112103441 501365808
659752417 137957977
86280801 257419447
902409188 565237611
965602301 689654312
104535476 646977261
945132881 114821749
198700181 915994879
*/
signed main(){
fast_io();
// srand(time(NULL));
cout << fixed << setprecision(10);
int q = 1;
// cin >> q;
while(q--)
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'long long int get(long long int, long long int)':
cake3.cpp:65:23: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
65 | if(sec.size() > m-2){
| ~~~~~~~~~~~^~~~~
cake3.cpp:76:23: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
76 | if(sec.size() > m-2){
| ~~~~~~~~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |