当前位置:   article > 正文

牛客小白月赛97 (个人题解)(待补完)

牛客小白月赛97 (个人题解)(待补完)

前言:

  前天晚上写的一场牛客上比赛,虽然只写出了三道,但比起之前的成绩感觉自己明显有了一点进步了,继续努力吧,

正文:

 链接:牛客小白月赛97_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com)

A 三角形:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[100005];
  4. int main(){
  5. int n;
  6. cin>>n;
  7. for(int i=1;i<=n;i++){
  8. cin>>a[i];
  9. }
  10. sort(a+1,a+n+1);
  11. int flag=0;
  12. for(int i=1;i<=n-2;i++){
  13. if(a[i]==a[i+1]&&a[i+1]==a[i+2])flag=1;
  14. }
  15. if(flag)cout<<"YES"<<endl;
  16. else cout<<"NO"<<endl;
  17. return 0;
  18. }

签到题,我是排序后看看有没有三个连续相等的数字。更好的做法应该是用桶排看看有没有出现次数超过三的数字,其实都差不多。

B 好数组:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,x,f=1,a=0;cin>>n;
  6. for(int i=0;i<n;i++)
  7. {
  8. cin>>x;
  9. if(x==0) f=0;
  10. }
  11. if(f==0) cout<<"NO"<<endl;
  12. else cout<<"YES"<<endl;
  13. return 0;
  14. }

又一道签到题,只要包含0就不是好数组。

C 前缀平方和序列:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,x;
  4. const long long mod=1e9+7;
  5. typedef long long ll;
  6. ll quick(ll a,ll b){
  7. ll res=1;
  8. while(b){
  9. if(b%2)res=res*a%mod;
  10. a=a*a%mod;
  11. b/=2;
  12. }
  13. return res;
  14. }
  15. ll f[10005];
  16. void init(){
  17. f[0]=1;
  18. for(int i=1;i<=10005;i++){
  19. f[i]=f[i-1]*i%mod;
  20. }
  21. }
  22. ll inv(ll x){
  23. return quick(x,mod-2)%mod;
  24. }
  25. int main(){
  26. init();
  27. cin>>n>>x;
  28. ll cnt=(ll)sqrt(x);
  29. if(cnt<n){
  30. cout<<0<<endl;
  31. return 0;
  32. }
  33. //cout<<f[cnt]<<" "<<inv(f[n])<<" "<<inv(f[cnt-n])<<endl;
  34. ll ans=(f[cnt]*((inv(f[n])%mod*inv(f[cnt-n]%mod))%mod))%mod;
  35. cout<<ans<<endl;
  36. return 0;
  37. }

预处理阶乘和逆元(费马小定理),然后求组合数,注意要开long long,并且在计算过程中一定要时时取模否则会暴。

D 走一个大整数迷宫:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int a[15][15],b[15][15];
  5. bool book[15][15][10005];
  6. ll n,m,p;
  7. ll quick(ll a,ll b,ll mod){
  8. ll res=1;
  9. while(b){
  10. if(b%2)res=res*a%mod;
  11. a=a*a%mod;
  12. b/=2;
  13. }
  14. return res;
  15. }
  16. int ne[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
  17. typedef struct stu{
  18. int x;
  19. int y;
  20. int z;
  21. int c;
  22. }node;
  23. int ans=1e9+7;
  24. queue<node> q;
  25. void bfs(int x,int y,int z,int c){
  26. node k={x,y,z,c};
  27. q.push(k);
  28. while(!q.empty()){
  29. k=q.front();
  30. q.pop();
  31. if(book[k.x][k.y][k.c%(p-1)])continue;
  32. book[k.x][k.y][k.c%(p-1)]=true;
  33. if(k.x==n&&k.y==m&&k.c%(p-1)==0){
  34. ans=min(k.z,ans);
  35. break;
  36. }
  37. for(int i=0;i<4;i++){
  38. int nx=k.x+ne[i][0];
  39. int ny=k.y+ne[i][1];
  40. if(nx>=1&&nx<=n&&ny>=1&&ny<=m){
  41. //cout<<nx<<" "<<ny<<" "<<k.z+1<<" "<<k.c+a[nx][ny]<<endl;
  42. int cc=(k.c+a[nx][ny])%(p-1);
  43. node l={nx,ny,k.z+1,cc};
  44. q.push(l);
  45. }
  46. }
  47. }
  48. }
  49. int main(){
  50. cin>>n>>m>>p;
  51. for(int i=1;i<=n;i++){
  52. for(int j=1;j<=m;j++){
  53. cin>>a[i][j];
  54. }
  55. }
  56. for(int i=1;i<=n;i++){
  57. for(int j=1;j<=m;j++){
  58. cin>>b[i][j];
  59. }
  60. }
  61. bfs(1,1,0,a[1][1]);
  62. if(ans==1e9+7)cout<<-1;
  63. else cout<<ans;
  64. return 0;
  65. }

首先咱们先看c的表式方式

首先我们要知道我们最后要求的数是对(p-1)取模的数,我们完全可以在输入的时候就将图上的数直接取模。然后我们再对Cij进行简化,很容易发现p^{2^{bij}}对(p-1)取模一直都为1。

(由于p^{2^{bij}}=(p-1+1)^{2^{bij}}

可以将p^{2^{bij}}展开为1+x_{1}(p-1)^{1}+x_{2}(p-1)^{2}+...+x_{n}(p-1)^{n}

然后用bfs跑分层图,第一、二维为x,y,第三维为走到这个点时的计数器上的数对(p-1)取模的结果(这样能避免多走但计数器取模后一样的情况)。具体写法见上。如果能搜到答案就输出,不然就输出-1.

E 前缀和前缀最大值:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[100005];
  4. int pre[100005],sum[100005][101];
  5. int main(){
  6. int n;
  7. cin>>n;
  8. for(int i=1;i<=n;i++){
  9. cin>>a[i];
  10. if(a[i]<0)pre[i]=pre[i-1]-a[i];
  11. else pre[i]=pre[i-1];
  12. for(int j=1;j<=100;j++){
  13. sum[i][j]=sum[i-1][j]+(a[i]==j);
  14. }
  15. }
  16. int q;
  17. cin>>q;
  18. for(int i=1;i<=q;i++){
  19. int l,r,tmp=0;
  20. cin>>l>>r;
  21. int t=pre[r]-pre[l-1];
  22. int res=0;
  23. int cnt=0;
  24. for(int j=1;j<=100;j++){
  25. int k=sum[r][j]-sum[l-1][j];
  26. if(k!=0){
  27. if(k*j+res>=t){
  28. cnt+=(t-res)/j;
  29. break;
  30. }
  31. else cnt+=k;res+=k*j;
  32. //cout<<" "<<t<<" "<<cnt<<" "<<res<<endl;
  33. }
  34. }
  35. cout<<cnt+1<<endl;
  36. }
  37. return 0;
  38. }

这题是我看了他发的答案才写出来的,咱们首先得要知道他的A类价值数是连续的,可能又相等的值,但一定是一个区间内的所有数,考虑 A 类价值最小的方案,是从小到大排序序列 a。然后进行n 次,将序列 a 的最后一个数字移动到最前面。会发现这样操作序列 a 的 A 类价值是单调不减的,并且每次只移动一个数,增量只可能是0 或者 1。所以可以得出一个结论,A 类价值是连续的。所以我们最后只需要求他的最大A的值r和最小A值l,答案就为r-l+1。上界即为正数个数,下界就是从小到大排的第一个正数前缀到尾的距离。

F parent 树上启发式合并(待补):

看着好高级。

后记:

  我还是太菜了。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号