总分:200pts
被好多人单调队列了。
| #A. 斐波那契分解 | #B. 走方格 | #C. zyb露营 | #D. 小 y 的函数 |
|---|---|---|---|
| 100 | 100 | 0 | 0 |
暴力地减,因为有结论:任何正整数都可以等于不重复的斐波那契数之和。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=1e18,K=1e5+5;
int t;
long long n,f[K],tot,ans[K],top;
void calc(){
f[1]=1,f[2]=2;
for(int i=3;f[i-1]<=N;tot=i,i++) f[i]=f[i-1]+f[i-2];
}
void solve(){
cin>>n;
for(int i=tot;i>=1&&n;i--){
if(n>=f[i]){
n-=f[i],ans[++top]=i;
}
}
while(top) cout<<ans[top--]<<' ';
cout<<'\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
calc();
cin>>t;
while(t--) solve();
return 0;
}打表可以发现:。实际上表示的是每一步可以选择 ,分开来考虑。
#include<bits/stdc++.h>
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define per(x,a,b) for(int x=a;x>=b;x--)
using namespace std;
typedef long long LL;
const LL mod=998244353;
const int N=1e6+5;
int t,n,m,k;
LL js[N],g[N];
LL quick_pow(LL a,LL b){
LL res=1;
while(b){
if(b&1) res=res*a%mod;
a=a*a%mod,b>>=1;
}
return res;
}
void calc(){
js[0]=g[0]=1;
rep(i,1,N-5){
js[i]=js[i-1]*i%mod;
g[i]=g[i-1]*quick_pow(i,mod-2)%mod;
}
}
LL C(int n,int m){
return js[n]*g[m]%mod*g[n-m]%mod;
}
void solve(){
cin>>n>>m>>k;
cout<<C(k,n)*C(k,m)%mod<<'\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
calc();
cin>>t;
while(t--) solve();
return 0;
}P11503 [NordicOI 2018] Nordic Camping
老师的做法:
我的神奇做法:
首先可以 维护出每个点作为右下角的最大正方形:。
然后再类似扫描线+线段树+大根堆就完了。
#include<bits/stdc++.h>
#define ls(p) (p<<1)
#define rs(p) ((p<<1)|1)
#define mid ((l+r)>>1)
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define per(x,a,b) for(int x=a;x>=b;x--)
using namespace std;
const int N=2005,INF=1e9;
int n,m,tot,T;
char a[N][N];
int l[N][N],h[N][N],ans[N][N];
priority_queue<pair<int,int>>q[N];
int maxn[N<<2],tag[N<<2];
void pushup(int p){
maxn[p]=max(maxn[ls(p)],maxn[rs(p)]);
}
void build(int p,int l,int r){
maxn[p]=tag[p]=0;
if(l==r) return;
build(ls(p),l,mid);
build(rs(p),mid+1,r);
}
void add_tag(int p,int d){
maxn[p]=max(maxn[p],d);
tag[p]=max(tag[p],d);
}
void pushdown(int p){
if(tag[p]!=0){
add_tag(ls(p),tag[p]);
add_tag(rs(p),tag[p]);
tag[p]=0;
}
}
void update(int L,int R,int p,int l,int r,int d){
if(L<=l&&r<=R){
add_tag(p,d);
return;
}
pushdown(p);
if(L<=mid) update(L,R,ls(p),l,mid,d);
if(mid<R) update(L,R,rs(p),mid+1,r,d);
pushup(p);
}
int query(int L,int R,int p,int l,int r){
if(L<=l&&r<=R) return maxn[p];
pushdown(p);
int res=0;
if(L<=mid) res=max(res,query(L,R,ls(p),l,mid));
if(mid<R) res=max(res,query(L,R,rs(p),mid+1,r));
return res;
}
int main(){
freopen("water.in","r",stdin);
freopen("water.out","w",stdout);
cin>>n>>m;
rep(i,1,n) rep(j,1,m) cin>>a[i][j];
rep(i,1,n) rep(j,1,m){
if(a[i][j]!='#') h[i][j]=h[i-1][j]+1;
}
rep(i,1,n){
// build(1,1,m);
// rep(j,1,m) update(i,i,1,1,m,-h[i][j]);
// rep(j,1,m){
// if(a[i][j]=='#') continue;
// int x=1,y=j;
// while(x<y){
// int mi=(x+y+1)>>1;
// if(-query(i-mi+1,i,1,1,m)>=mi) x=mi;
// else y=mi-1;
// }
// l[i][j]=x;
// }
rep(j,1,m){
if(a[i][j]=='#') continue;
l[i][j]=min({l[i][j-1],l[i-1][j],l[i-1][j-1]})+1;
}
}
//rep(i,1,n) rep(j,1,m) cout<<l[i][j]<<(j==m?'\n':' ');
per(j,m,1){
build(1,1,n);
rep(i,1,n){
if(a[i][j]=='#') continue;
update(i-l[i][j]+1,i,1,1,n,l[i][j]);
}
rep(i,1,n){
if(a[i][j]=='#') continue;
while(!q[i].empty()&&q[i].top().second>j) q[i].pop();
int k=query(i,i,1,1,n);
q[i].push({k*k,j-k+1});
ans[i][j]=q[i].top().first;
}
}
cin>>T;
while(T--){
int x,y;
cin>>x>>y;
cout<<ans[x][y]<<'\n';
}
return 0;
}
诈骗题。把 看作横坐标, 看作纵坐标,那么要求的就是一个斜率的东西。可以发现,横坐标相邻的两个点的斜率是最大值。所以转化为维护 。用线段树即可。
#include<bits/stdc++.h>
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
#define mid ((l+r)>>1)
using namespace std;
const int N=1e5+5;
int n,q,a[N];
int maxn[N<<2];
void pushup(int p){
maxn[p]=max(maxn[ls(p)],maxn[rs(p)]);
}
void build(int p,int l,int r){
if(l==r){
maxn[p]=abs(a[l+1]-a[l]);
return;
}
build(ls(p),l,mid);
build(rs(p),mid+1,r);
pushup(p);
}
void update(int X,int p,int l,int r,int d){
if(l==r){
maxn[p]=d;
return;
}
if(X<=mid) update(X,ls(p),l,mid,d);
else update(X,rs(p),mid+1,r,d);
pushup(p);
}
int query(int L,int R,int p,int l,int r){
if(L<=l&&r<=R) return maxn[p];
int res=0;
if(L<=mid) res=max(res,query(L,R,ls(p),l,mid));
if(mid<R) res=max(res,query(L,R,rs(p),mid+1,r));
return res;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
build(1,1,n-1);
cin>>q;
while(q--){
int opt,x,y;
cin>>opt>>x>>y;
if(opt) cout<<query(x,y-1,1,1,n-1)<<'\n';
else{
a[x]=y;
if(x>1) update(x-1,1,1,n-1,abs(a[x-1]-a[x]));
if(x<n) update(x,1,1,n-1,abs(a[x]-a[x+1]));
}
}
return 0;
}
