이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//
// main.cpp
// aliens
//
// Created by Trần Nam Khánh on 11/18/20.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct ed
{
long long a,b,minsq;
};
struct pt
{
long long r,c;
};
vector<ed>ch;
vector<pt>a,b;
vector<pair<long long,long long> >g;
long long n,m,k;
long long check(ed a,ed b,ed c)
{
//(c.b-b.b)/(b.a-c.a)
//(b.b-a.b)/(a.a-b.a)
return (b.b-a.b)*(b.a-c.a)>=(c.b-b.b)*(a.a-b.a);
}
void add(long long x,long long y,long long mi)
{
ed a;
a.a=x;
a.b=y;
a.minsq=mi;
while(ch.size()>=2&&check(ch[ch.size()-2],ch[ch.size()-1],a))
{
ch.pop_back();
}
ch.push_back(a);
}
long long dis(long long x,ed a,ed b)
{
//(b.b-a.b)/(a.a-b.a)
return (b.b-a.b)-x*(a.a-b.a);
}
pair<long long,long long> cal(long long c){
g.clear();
g.resize(n+1);
ch.clear();
g[0]={0,0};
long long l=0;
for(long long i=1;i<=n;i++)
{
long long tmp=(a[i].c-a[i].r+1)*(a[i].c-a[i].r+1)+g[i-1].first;
long long tam=max(0ll,a[i-1].c-a[i].r+1);
tmp-=tam*tam;
long long A=-2*a[i].r+2,B=tmp+2*a[i].r*a[i].c-a[i].c*a[i].c-2*a[i].c;
add(A,B,g[i-1].second);
long long X=a[i].c;
if(ch.size()&&l>=ch.size())l=ch.size()-1;
while(l+1<ch.size()&&dis(X,ch[l],ch[l+1])<0)
{
l++;
}
g[i].first=ch[l].a*a[i].c+ch[l].b+a[i].c*a[i].c+c;
g[i].second=ch[l].minsq+1;
}
return g[n];
}
long long cmp(pt a,pt b)
{
return (a.c<b.c)||(a.c==b.c&&a.r>b.r);
}
long long cal2()
{
long long lo=0,hi=m*m,ans=0;
while(lo<hi)
{
long long mid=(lo+hi+1)>>1;
pair<long long,long long>tmp=cal(mid);
if(tmp.second>=k)
{
lo=mid;
}else hi=mid-1;
}
ans=cal(lo+1).first-k*(lo+1);
return ans;
}
long long take_photos(int N,int M,int K,vector<int>r,vector<int>c) {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
n=N;m=M;k=K;
a.resize(n+1);
for(long long i=1;i<=n;i++)
{
a[i].r=r[i-1];
a[i].c=c[i-1];
if(a[i].r>a[i].c)
{
swap(a[i].r,a[i].c);
}
}
sort(a.begin()+1,a.end(),cmp);
for(long long i=1;i<=n;i++)
{
while(b.size()!=0&&(a[i].r<=b.back().r))
{
b.pop_back();
}
b.push_back(a[i]);
}
a.resize(b.size()+1);
for(long long i=0;i<b.size();i++)
{
a[i+1]=b[i];
}
a[0].r=-1;
a[0].c=-1;
n=(long long)b.size();
k=min(k,n);
return cal2();
}
/*
int main()
{
cin>>n>>m>>k;
vector<int>r(n),c(n);
for(int i=1;i<=n;i++)
{
cin>>r[i-1]>>c[i-1];
}
cout<<take_photos(n,m,k,r,c);
return 0;
}*/
/*
3 7 2
0 3
4 6
3 4
*/
컴파일 시 표준 에러 (stderr) 메시지
aliens.cpp: In function 'std::pair<long long int, long long int> cal(long long int)':
aliens.cpp:61:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<ed>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if(ch.size()&&l>=ch.size())l=ch.size()-1;
| ~^~~~~~~~~~~
aliens.cpp:62:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<ed>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(l+1<ch.size()&&dis(X,ch[l],ch[l+1])<0)
| ~~~^~~~~~~~~~
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:113:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<pt>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(long long i=0;i<b.size();i++)
| ~^~~~~~~~~
# | 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... |