题面:https://www.cnblogs.com/Juve/articles/11396238.html
下面开始一句话题解:
A:爬山:
二分答案,check即可
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define int long long using namespace std; const int mod=1e9+7; int n,d,a,b,l,r,mid,ans; bool check(int m){ int p=abs(m-a),q=abs(m-b),x,y; if(p%d==0) x=p/d; else x=p/d+1; if(q%d==0) y=q/d; else y=q/d+1; if(x+y<=n-1) return 1; return 0; } signed main(){ scanf("%lld%lld%lld%lld",&n,&d,&a,&b); if(d==0){ printf("%lld ",max(a,b)); return 0; } l=min(a,b)-n*d,r=max(a,b)+n*d; ans=max(a,b); while(l<=r){ mid=(l+r)>>1; if(check(mid)) ans=max(ans,mid),l=mid+1; else r=mid-1; } printf("%lld ",ans); return 0; }
B:学数数:
离散化,单调栈,前缀和
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #define int long long using namespace std; const int MAXN=1e5+5; int n,q,a[MAXN],b[MAXN],k,cnt,sum[MAXN]; int sta[MAXN],top=0,l[MAXN],r[MAXN]; char op[5]; signed main(){ scanf("%lld%lld",&n,&q); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); memcpy(b,a,sizeof(a)); sort(b+1,b+n+1); cnt=unique(b+1,b+n+1)-b-1; for(int i=1;i<=n;i++){ while(top!=0&&a[sta[top]]<=a[i]) top--; if(top==0) l[i]=1; else l[i]=sta[top]+1; sta[++top]=i; } top=0; for(int i=n;i>=1;i--){ while(top!=0&&a[sta[top]]<a[i]) top--; if(top==0) r[i]=n; else r[i]=sta[top]-1; sta[++top]=i; } for(int i=1;i<=n;i++){ int pos=lower_bound(b+1,b+cnt+1,a[i])-b; sum[pos]+=(i-l[i]+1)*(r[i]-i+1); } for(int i=2;i<=n;i++) sum[i]+=sum[i-1]; while(q--){ scanf("%s%lld",op,&k); if(op[0]=='='){ int x=upper_bound(b+1,b+cnt+1,k)-b-1; int y=lower_bound(b+1,b+cnt+1,k)-b; if(x==y) printf("%lld ",sum[x]-sum[x-1]); else puts("0"); }else if(op[0]=='<'){ int x=lower_bound(b+1,b+cnt+1,k)-b-1; printf("%lld ",sum[x]); }else{ int x=upper_bound(b+1,b+cnt+1,k)-b-1; printf("%lld ",sum[cnt]-sum[x]); } } return 0; }
C:七十和十七
推一个公式即可,加上快速幂
#include<cstdio> #include<iostream> #include<algorithm> #define int long long using namespace std; const int MAXN=1e5+5; const int mod=1e9+7; int f[MAXN],n,bit[MAXN]; int q_pow(int a,int b,int p){ int res=1; while(b){ if(b&1) res=1ll*res*a%p; a=1ll*a*a%p; b>>=1; } return res%mod; } signed main(){ scanf("%lld",&n); bit[0]=1; for(int i=1;i<=n;i++) bit[i]=(bit[i-1]*2)%mod; for(int i=1;i<=n;i++) f[i]=(f[i-1]+(bit[i-1]-1)%mod*q_pow(i,mod-2,mod)%mod)%mod; printf("%lld ",f[n]); return 0; }