C题,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 5005 using namespace std; int num[maxn]; int rmq(int l,int r) { int ans=1<<30,tmp=l; for(int i=l;i<=r;i++) { if(ans>num[i]) { ans=num[i]; tmp=i; } } return tmp; } int get(int l,int r,int h) { if(l>r) return 0; if(l==r) { if(num[l]==h) return 0; else return 1; } int tmp=rmq(l,r); int ans=0; ans=min(r-l+1,get(l,tmp-1,num[tmp])+get(tmp+1,r,num[tmp])+num[tmp]-h); return ans; } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&num[i]); int ans=get(0,n-1,0); printf("%d ",ans); }
刚刚开始想到一种很暴力但还行的方法,不过在和TK讨论的过程中引出了一个更好的算法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; long long n,m,k; long long check(long long x) { long long ans=0; for(int i=1;i<=n;i++) { ans+=min(x/i,m); if(min(x/i,m)*i==x) ans--; } return ans; } int main() { scanf("%I64d%I64d%I64d",&n,&m,&k); long long l=1,r=n*m+3; long long ans=0; while(l<r) { long long mid=(l+r)>>1; long long a=check(mid); long long b=check(mid+1); if(a<k&&b>=k) { ans=mid; break; } else if(b<k)l=mid; else if(a>=k)r=mid; } cout<<ans<<endl; }