T1 屠龙宝刀点击就送
树状数组求逆序对+大特判
#include <algorithm> #include <cctype> #include <cstdio> #define N 205 using namespace std; inline void Read(int &x) { register char ch=getchar(); for(x=0;!isdigit(ch);ch=getchar()); for(;isdigit(ch);x=x*10+ch-'0',ch=getchar()); } int Maxna,Maxnb,tag[N],sum,n,k,a[N],b[N],c[N],all,pos[N],flag; inline int lowbit(int x) {return x&(-x);} inline int max(int a,int b) {return a>b?a:b;} inline int query(int x) { int ret=0; for(;x;x-=lowbit(x)) ret+=tag[x]; return ret; } inline void modify(int x,int y) {for(;x<=n;x+=lowbit(x)) tag[x]+=y;} int Main() { Read(n);Read(k); for(int i=1;i<=n;++i) { Read(a[i]); if(!a[i]) pos[++all]=i; else flag=i,Maxna=max(Maxna,a[i]); c[i]=a[i]; } for(int i=1;i<=k;++i) Read(b[i]),Maxnb=max(Maxnb,b[i]); if(all==n&&k!=1) {printf("Yes ");return 0;} if(pos[1]>flag) { if(k==1&&Maxnb<Maxna) {printf("Yes ");return 0;} if(k==1&&Maxnb>Maxna) {printf("No ");return 0;} else {printf("Yes ");return 0;} } sort(b+1,b+1+k); for(int i=1;i<=all;++i) a[pos[i]]=b[i],c[pos[i]]=b[i]; sort(c+1,c+1+n); int Size=unique(c+1,c+1+n)-c-1; for(int i=n;i>=1;--i) { int rank=lower_bound(c+1,c+1+Size,a[i])-c; if(rank-1) sum+=query(rank-1); modify(rank,1); } if(k>1) { if(sum>=0) printf("Yes "); else printf("No "); } else if(sum>0) printf("Yes "); else printf("No "); return 0; } int sb=Main(); int main() {;}
T2 屠龙宝刀点击就送
暴力dfs
#include <cstdlib> #include <cctype> #include <cstdio> #define N 1005 inline void Read(int &x) { register char ch=getchar(); for(x=0;!isdigit(ch);ch=getchar()); for(;isdigit(ch);x=x*10+ch-'0',ch=getchar()); } bool use[N]; int n,a[N],b[N],pos[N],all,ans[N]; bool check() { int x=0,y=0,z=0; for(int i=1;i<=n;++i) { if(ans[i]==a[i]) x++; if(ans[i]==b[i]) y++; } if((x==n-1)&&(y==n-1)) return 1; else return 0; } void dfs(int num) { if(num==all+1) { if(check()) {for(int i=1;i<=n;++i) printf("%d ",ans[i]);exit(0);} return; } for(int i=1;i<=n;++i) { if(!use[i]) { use[i]=1; ans[pos[num]]=i; dfs(num+1); use[i]=0; } } } int main() { Read(n); for(int i=1;i<=n;++i) Read(a[i]); for(int i=1;i<=n;++i) { Read(b[i]); if(a[i]==b[i]) ans[i]=a[i],use[a[i]]=1; else pos[++all]=i; } dfs(1); return 0; }
T3 屠龙宝刀点击就送
正解dp。。自动弃疗
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstring> #include <cstdio> #define N 1505 int pos[27][N],num[27],n,q; char str[N],c; int main() { scanf("%d",&n);char ch=getchar(); for(int i=1;i<=n;++i) scanf("%c",&str[i]),pos[str[i]-'a'][++num[str[i]-'a']]=i; scanf("%d",&q); for(int x,f,ans;q--;) { f=ans=0;scanf("%d %c",&x,&c); if(x==n-num[c-'a']||n==num[c-'a']) {printf("%d ",n);continue;} if(num[c-'a']==1) {printf("%d ",1+x);continue;} for(int i=2;i<=num[c-'a'];++i) { if(x<=0) break; if(pos[c-'a'][i]-pos[c-'a'][i-1]-1<=x) ans+=pos[c-'a'][i]-pos[c-'a'][i-1]-1,x-=pos[c-'a'][i]-pos[c-'a'][i-1]-1,f=i; else x--,ans++; } ans+x+f>=n?printf("%d ",n):printf("%d ",ans+x+f); } return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstring> #include <cstdio> #define N 1505 int x,f[27][N],n,q; char str[N],c; bool check(int l) { int p=c-'a'; for(int i=1;i<=n-l+1;++i) if(f[p][i+l-1]-f[p][i-1]>=l-x) return false; return true; } int main() { scanf("%d",&n);char ch=getchar(); for(int i=1;i<=n;++i) { for(int j=0;j<26;++j) f[j][i]=f[j][i-1]; scanf("%c",&str[i]); ++f[str[i]-'a'][i]; } scanf("%d",&q); for(int l,r,mid,ans;q--;) { scanf("%d %c",&x,&c); for(l=0,r=n+1;l<=r;) { mid=(l+r)>>1; if(check(mid)) r=mid-1; else l=mid+1; } printf("%d ",l-1); } return 0; }
/* 第一题3次 第二题1次 第三题6次 前两题没看题解。。 自我感觉良好。。 哎 毕竟菜鸡的自我安慰 */