이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define vec vector
#define sz(x) (int)x.size()
#define m_p make_pair
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
auto rng=bind(uniform_int_distribution<int>(1,1e9),mt19937(time(0)));
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
int brute(vec<int> a){
int n=sz(a);
vec<vec<int>>dp(n,vec<int>(n,-2*n));
vec<ll>pref(n,0);
for(int i=0;i<n;i++) pref[i]=(i?pref[i-1]:0)+a[i];
auto get=[&](int l,int r){
return pref[r]-(l?pref[l-1]:0);
};
int ans=0;
for(int i=0;i<n;i++){
umax(dp[0][i],1);
for(int j=0;j<=i;j++){
for(int k=i+1;k<n;k++){
if(get(i+1,k)>=get(j,i)){
umax(dp[i+1][k],dp[j][i]+1);
}
}
// cout<<i<<' '<<j<<endl;
umax(ans,dp[j][i]);
}
}
return ans;
}
int stupid(vec<int> a){
int n=sz(a);
vec<ll>pref(n,0);
for(int i=0;i<n;i++) pref[i]=(i?pref[i-1]:0)+a[i];
auto get=[&](int l,int r){
return pref[r]-(l?pref[l-1]:0);
};
int ans=0;
vec<pil>dp(n,m_p(-2*n,-2*n));
for(int i=0;i<n;i++){
dp[i]=m_p(1,get(0,i));
for(int j=0;j<i;j++){
if(get(j+1,i)>=dp[j].s){
umax(dp[i],m_p(dp[j].f+1,get(j+1,i)));
}
}
umax(ans,dp[i].f);
}
return ans;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin>>n;
// cout<<n<<endl;
vec<int>a(n);
vec<ll>pref(n);
for(auto &z : a) cin>>z;
// cout<<endl;
// cout<<brute(a);
// return 0;
for(int i=0;i<n;i++){
pref[i]=(i?pref[i-1]:0)+a[i];
}
auto get=[&](int l,int r){
return pref[r]-pref[l-1];
};
ll lst=a[0],s=0;
int cnt=1;
int w=0;
for(int i=1;i<n;i++){
s+=a[i];
if(s>=lst){
int j=w;
while(1){
if(pref[i]-pref[j]<pref[j]-pref[w]+lst) break;
j++;
}
j--;
cnt++;
lst=pref[i]-pref[j];
w=i;
s=0;
}
}
// assert(stupid(a)==brute(a));
cout<<stupid(a);
// assert(cnt==brute(a));
//// cout<<cnt<<endl;
// }
return 0;
}
/*
10
2 1 2 2 1 1 3 2 5 5
*/
컴파일 시 표준 에러 (stderr) 메시지
segments.cpp: In function 'int main()':
segments.cpp:74:10: warning: variable 'get' set but not used [-Wunused-but-set-variable]
74 | auto get=[&](int l,int r){
| ^~~
# | 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... |