持续更新。。。纪念一下我的高分暴力。。。(好丢人啊qwq)
NOI2014 动物园
80pts
用倍增暴力跳nxt数组
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 1000010
#define MOD 1000000007
#define ull unsigned long long
using namespace std;
int T,len,ans;
int nxt[MAXN],fp[22][MAXN];
char s[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
ans=1;
memset(nxt,0,sizeof(nxt));
scanf("%s",s+1);
len=strlen(s+1);
int p=0;
for(int i=2;i<=len;i++)
{
while(p&&s[p+1]!=s[i]) p=nxt[p];
if(s[p+1]==s[i]) p++;
nxt[i]=p;
}
for(int i=2;i<=len;i++) fp[0][i]=nxt[i];
for(int k=1;k<=21;k++)
for(int i=2;i<=len;i++)
fp[k][i]=fp[k-1][fp[k-1][i]];
for(int i=2;i<=len;i++)
{
int now=i;
for(int j=21;j>=0;j--)
if(fp[j][now]*2>i)
now=fp[j][now];
int cur_ans=0;
for(int j=21;j>=0;j--)
if(fp[j][now])
cur_ans+=(1<<j),now=fp[j][now];
ans=1ll*ans*(cur_ans+1)%MOD;
}
printf("%d
",ans);
}
return 0;
}
NOI2016 优秀的拆分
95pts
hash直接水,(n^2)判断字符串是否相同
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 1000010
#define ull unsigned long long
using namespace std;
int T,n,m;
int base[MAXN],kkk[MAXN];
long long ans;
char s[MAXN];
const int prime=233;
inline void init()
{
base[0]=1;
for(int i=1;i<=n;i++)
{
base[i]=base[i-1]*prime;
kkk[i]=kkk[i-1]*prime+s[i]-'a';
}
}
inline ull query(int l,int r){return kkk[r]-kkk[l-1]*base[r-l+1];}
inline bool check(int l,int r)
{
if((r-l+1)&1) return false;
int mid=(l+r)>>1;
if(query(l,mid)==query(mid+1,r)) return true;
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
ans=0;
scanf("%s",s+1);
n=strlen(s+1);
init();
for(int i=1;i<=n;i++)
{
int st=0,en=0;
for(int j=1;j<=i;j++)
if(check(j,i)) en++;
for(int j=i+1;j<=n;j++)
if(check(i+1,j)) st++;
ans+=1ll*en*st;
}
printf("%lld
",ans);
}
return 0;
}
CTS2019 无处安放
前三个可以手算:
//test1
1 0 0 1
1 0 2 1
1 10 0 1
1 18 0 0
1 34 0 1
1 34 2 1
1 26 0 0
1 40 0 1
//test2
1 2 0 1
1 2 5 0
1 10 5 1
1 13 3 0
1 16 8 0
1 13 7 1
1 16 7 0
1 0 0 0
1 6 7 1
1 6 0 0
1 13 0 1
1 10 0 1
//test3
1 0 5 1
1 3 5 0
1 5 0 0
1 9 9 0
1 5 5 0
1 10 4 1
1 0 0 0
1 5 8 0
1 8 3 1
1 12 6 1
1 3 8 0
1 6 5 0
1 5 3 1
1 12 3 1
1 6 8 1
1 9 4 0
//test4贪心代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define MAXN 100010
using namespace std;
int type,k,n,m;
bool done[MAXN];
vector<int>vec;
struct Node{int id,x,y,ansx,ansy,op,dir;}node[MAXN];
inline bool cmp1(struct Node a,struct Node b)
{
if(a.y==b.y) return a.x>b.x;
return a.y>b.y;
}
inline bool cmp2(struct Node a,struct Node b){return a.id<b.id;}
int main()
{
freopen("nowhere4.in","r",stdin);
freopen("nowhere4.out","w",stdout);
scanf("%d%d",&type,&k);
scanf("%d%d",&n,&m);
for(int i=1;i<=k;i++)
{
scanf("%d%d",&node[i].x,&node[i].y);
if(node[i].x>node[i].y) swap(node[i].x,node[i].y),node[i].dir=1;
node[i].id=i;
}
int now=0;
sort(&node[1],&node[k+1],cmp1);
// for(int i=1;i<=100;i++) printf("%d %d
",node[i].x,node[i].y);
for(int i=1;i<=k;i++)
{
if(node[i].y!=4) break;
done[i]=1;
node[i].op=1;
node[i].ansx=now,node[i].ansy=0;
now+=node[i].x;
if(node[i].x==4&&node[i].y==4) vec.push_back(i);
}
// cout<<now<<endl;
int cur1=now,cur2=now,cnt=0;
for(int i=1;i<=k;i++)
{
if(node[i].x==3&&node[i].y==3)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=0;
cur1+=node[i].x;
cnt++;
if(cnt==448) break;
}
}
// printf("cur1=%d
",cur1);
cnt=0;
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==3)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur2,node[i].ansy=3;
cur2+=node[i].y;
node[i].dir=1;
cnt++;
if(cnt==448) break;
}
}
// printf("cur2=%d
",cur2);
now=cur1;
cur1=now,cur2=now;int cur3=now;
cnt=0;
for(int i=1;i<=k;i++)
{
if(node[i].x==3&&node[i].y==3&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=0;
cur1+=node[i].x;
cnt++;
if(cnt==350) break;
}
}
cnt=0;
// printf("cur1=%d
",cur1);
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==1&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur2,node[i].ansy=3;
cur2+=3;
cnt++;
if(cnt==350) break;
}
}
cnt=0;
// printf("cur2=%d
",cur2);
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==2&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur3+1,node[i].ansy=3;
node[i].dir=1;
cur3+=3;
cnt++;
if(cnt==350) break;
}
}
now=cur1;
cur1=now,cur2=0;
for(int i=1;i<=k;i++)
{
if(node[i].x==2&&node[i].y==3&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=(cur2==0?0:2);
node[i].dir=1;
if(cur2==1)cur1+=3;
cur2^=1;
}
}
now=cur1,cur1=now,cur2=0;
for(int i=1;i<=k;i++)
{
if(node[i].x==2&&node[i].y==2&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=(cur2==0?0:2);
if(cur2==1)cur1+=2;
cur2^=1;
}
}
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==2&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=2;
node[i].dir=1;
break;
}
}
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==2&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=3;
node[i].dir=1;
break;
}
}
now=cur1+2;
cur1=now,cur2=0;
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==2&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=(cur2==0?0:2);
if(cur2==1)cur1+=1;
cur2^=1;
}
}
now=cur1;
// cout<<now<<endl;
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==1&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=2;
node[i].dir=1;
break;
}
}
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==1&&done[i]==0)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur1,node[i].ansy=3;
node[i].dir=1;
break;
}
}
now=cur1+1;
cur1=now,cur2=0;
// cout<<now<<endl;
for(int i=1;i<=k;i++)
{
if(node[i].x==1&&node[i].y==1&&done[i]==0)
{
// cout<<i<<endl;
done[i]=1;
node[i].op=1;
node[i].ansx=cur1;
node[i].ansy=cur2;
cur2++;
if(cur2==4)cur1++;
if(cur1>=7518) break;
cur2%=4;
}
}
cur1=0,cur2=0,cur3=0,cnt=0;
node[vec[0]].op=node[vec[1]].op=node[vec[2]].op=0;
for(int i=1;i<=k;i++)
{
if(done[i]==0&&node[i].x==1&&node[i].y==1)
{
done[i]=1;
node[i].op=1;
node[i].ansx=cur2,node[i].ansy=cur3;
cur3++;
if(cur3==4)cur2++,cur3=0;
}
}
// for(int i=1;i<=k;i++)
// {
// if(node[i].op==0)
// printf("%d %d
",node[i].x,node[i].y);
// }
sort(&node[1],&node[k+1],cmp2);
for(int i=1;i<=k;i++)
{
// if(node[i].x==1&&node[i].y==3)
if(node[i].op==0) printf("0
");
else printf("%d %d %d %d
",node[i].op,node[i].ansx,node[i].ansy,node[i].dir);
}
return 0;
}
CTS2019 珍珠
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAXN 4010
#define mod 998244353
using namespace std;
int d,n,m,ans;
int dp[MAXN][MAXN];
inline int fpow(int x,int y)
{
int cur_ans=1;
while(y)
{
if(y&1) cur_ans=1ll*cur_ans*x%mod;
x=1ll*x*x%mod;
y>>=1;
}
return cur_ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d%d%d",&d,&n,&m);
if(m<=1)
{
printf("%d
",fpow(d,n)%mod);
return 0;
}
dp[0][0]=1;
for(int i=0;i<n;i++)
for(int j=0;j<=d;j++)
{
if(j+1<=d) dp[i+1][j+1]=(dp[i+1][j+1]+1ll*dp[i][j]*(d-j)%mod)%mod;
if(j-1>=0) dp[i+1][j-1]=(dp[i+1][j-1]+1ll*dp[i][j]*j%mod)%mod;
}
for(int i=0;i<=d;i++)
{
if(n-i>=2*m)
ans=(ans+dp[n][i])%mod;
}
printf("%d
",ans);
return 0;
}
NOI2016 国王饮水记
53pts
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define MAXN 100010
using namespace std;
int n,k,p;
double a[MAXN],tmp[MAXN];
inline bool cmp(int x,int y){return x>y;}
namespace subtask1
{
inline void solve()
{
double cur_ans=a[1],ans=0;
double cur=a[1];
sort(&a[1],&a[n+1],cmp);
for(int i=1;i<=n;i++)
{
if(a[i]==cur) break;
cur_ans+=a[i];
ans=max(ans,cur_ans/(i+1));
}
printf("%lf
",ans);
}
}
namespace subtask2
{
inline void solve()
{
double cur_ans=a[1],ans=0;
double cur=a[1];
sort(&a[1],&a[n+1]);
int i;
for(i=1;i<=n;i++)
if(a[i]==cur) break;
// printf("i=%d
",i);
while(i<=n)
{
if(a[i]<a[1]) continue;
a[1]=(a[1]+a[i])/2;
i++;
}
printf("%lf
",a[1]);
}
}
namespace subtask3
{
inline void solve()
{
vector<double>vec;
for(int i=2;i<=n;i++)
if(a[i]>a[1])
vec.push_back(a[i]);
sort(vec.begin(),vec.end());
for(int i=vec.size()-k;i<vec.size();i++)
if(vec[i]>a[1])
a[1]=(a[1]+vec[i])/2;
printf("%lf
",a[1]);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
// freopen("ce.out","w",stdout);
#endif
int cnt=0;
scanf("%d%d%d",&n,&k,&p);
for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
for(int i=2;i<=n;i++)
if(a[i]>a[1])
cnt++;
if(k==1) subtask1::solve();
else if(k>=cnt) subtask2::solve();
else subtask3::solve();
return 0;
}
根据dfs序生成树:
inline void dfs(int cur)
{
if(cur>=n){return;}
int x=a1[cur],y=a1[cur+1],z=fa[x];
fa[y]=x;
e[x].push_back(y);
dfs(cur+1);
e[x].pop_back();
for(;z;z=fa[z])
{
fa[y]=z;
e[z].push_back(y);
dfs(cur+1);
e[z].pop_back();
}
}