이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (k+k)
#define sag (k+k+1)
#define orta ((bas+son)/2)
#define coc g[node][i]
#define SURE 3.5
#define mod 1000000007
#define inf 1000000000000000009
#define N 1000005
using namespace std;
typedef long long ll;
typedef pair < int , int > ii;
int n, m, k, bas = 1, son, amk, a[N], b[N], ne[N], say[4*N];
ll seg[4*N], of, cvp, opt, ans = -inf;
ii xx[N];
map < int , int > h, x;
map < int , int > :: iterator it;
void bitir(){
printf("%lld\n", ans);
exit(0);
}
void up(int k, int bas, int son, int x, int y){
if(bas == son){
seg[k] += 1ll*y*ne[bas];
say[k] += y;
return;
}
if(x <= orta)
up(sol, bas, orta, x, y);
else
up(sag, orta + 1, son, x, y);
seg[k] = seg[sol] + seg[sag];
say[k] = say[sol] + say[sag];
}
ll qu(int k, int bas, int son, int x){
if(bas == son)
return 1ll*x*ne[bas];
if(say[sag] >= x)
return qu(sag, orta + 1, son, x);
return seg[sag] + qu(sol, bas, orta, x - say[sag]);
}
void getir(int bs, int sn){
while(bas < bs){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
up(1, 1, k, a[bas], -1);
bas++;
}
while(bas > bs){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
bas--;
up(1, 1, k, a[bas], 1);
}
while(son > sn){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
up(1, 1, k, a[son], -1);
son--;
}
while(son < sn){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
son++;
up(1, 1, k, a[son], 1);
}
}
void f(int bas, int son, int optl, int optr){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
// amk++;if(amk > 50)exit(0);
if(bas > son)return;
int optm = optr;
opt = -inf;
for(int i = m + orta - 1; i <= optr; i++){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
getir(orta + 1, i - 1);
of = qu(1, 1, k, m - 2);
cvp = of + 1ll*ne[a[orta]] + 1ll*ne[a[i]] - 2ll*(b[i] - b[orta]);
// if(cvp == 152)cout << "geldi amk " << orta << " " << i << " " << of << " = " << cvp << endl;
if(cvp > opt){
opt = cvp;
optm = i;
}
}
// cout << bas << " " << son << " " << orta << " " << optl << " " << optr << " = " << opt << " " << optm << endl;
ans = max(ans, opt);
f(bas, orta - 1, optl, optm);
f(orta + 1, son, optm, optr);
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%d %d",&n ,&m);
for(int i = 1; i <= n; i++){
scanf("%d %d",&xx[i].nd ,&xx[i].st);
h[xx[i].nd]++;
}
for(it = h.begin(); it != h.end(); it++){
x[it->st] = ++k;
ne[k] = it->st;
// cout << k << " " << it->st << endl;
}
sort(xx + 1, xx + n + 1);
for(int i = 1; i <= n; i++){
a[i] = x[xx[i].nd];
b[i] = xx[i].st;
// cout << a[i] << " " << b[i] << endl;;
}
// cout << endl;
// getir(2, 4);
// cout << qu(1, 1, k, 3) << endl;
// cout << qu(1, 1, k, 2) << endl;
// cout << qu(1, 1, k, 1) << endl;
// cout << qu(1, 1, k, 0) << endl;
f(1, n, m, n);
printf("%lld\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'int main()':
cake3.cpp:101:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n ,&m);
~~~~~^~~~~~~~~~~~~~~~
cake3.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&xx[i].nd ,&xx[i].st);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |