• CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)


    A. Indirect Sort

    题目链接:Problem - A - Codeforces

    样例输入:

    1. 7
    2. 3
    3. 1 2 3
    4. 3
    5. 1 3 2
    6. 7
    7. 5 3 4 7 6 2 1
    8. 7
    9. 7 6 5 4 3 2 1
    10. 5
    11. 2 1 4 5 3
    12. 5
    13. 2 1 3 4 5
    14. 7
    15. 1 2 6 7 4 3 5

    样例输出:

    1. Yes
    2. Yes
    3. No
    4. No
    5. No
    6. No
    7. Yes

    题意:一开始给定一个长度为n的1~n的排列,n>=3,我们可以对这个排列进行操作,每次操作选择三个位置i,j,k,满足1<=iak那么就令ai=ai+aj,否则交换aj和ak。问我们能否通过这种操作使得最后n个数呈现非递降序列。

    分析:我们能够发现,如果a[i]是a[i~n]中的最小值,那么我们可以任意选择a[(i+1)~n]中的任意元素进行交换,肯定能够将其按照非递减顺序进行排序,但是我们发现a[i]的值不会被改变,这个时候如果a[i]是全局最小值,而且i!=1,那么前面的数是不能对a[i]的位置产生影响的,又没办法减少一个数的值,所以一定不可能使得最后呈现非递降顺序。所以我们只需要判断第一个位置是不是全局最小值即可

    代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. using namespace std;
    9. const int N=2e5+10;
    10. int a[N];
    11. int main()
    12. {
    13. int T;
    14. cin>>T;
    15. while(T--)
    16. {
    17. int n;
    18. scanf("%d",&n);
    19. for(int i=1;i<=n;i++)
    20. scanf("%d",&a[i]);
    21. if(a[1]==1) puts("Yes");
    22. else puts("No");
    23. }
    24. return 0;
    25. }

    B. Maximum Substring

    题目链接:Problem - B - Codeforces

    样例输入: 

    1. 6
    2. 5
    3. 11100
    4. 7
    5. 1100110
    6. 6
    7. 011110
    8. 7
    9. 1001010
    10. 4
    11. 1000
    12. 1
    13. 0

    样例输出:

    1. 9
    2. 12
    3. 16
    4. 12
    5. 9
    6. 1

    题意:给定一个长度为n的01串,让我们求出来这个串中的子串的贡献的最大值。一个子串的贡献值计算方式如下:

    先求出这个子串中的字符0的个数cnt0和字符1的个数cnt1

    如果cnt0=0,那么贡献就是cnt1*cnt1

    如果cnt1=0,那么贡献就是cnt0*cnt0

    否则,贡献为cnt0*cnt1

    分析:这个就很容易贪心了,由于cnt0和cnt1会随着区间长度的增大而增大,所以如果子串中同时含有1和0的话我们会尽可能地增大区间长度,答案贡献就是按照第三种方式进行计算,那么我们可以先计算一下整个字符串的字符0的个数和字符1的个数。然后直接利用这个计算一下贡献。但是还有两种情况我们需要考虑一下:

    一种就是选取最长连续0的子串计算贡献

    另一种就是选取最长连续1的子串计算贡献

    最后这三种情况取一下最大值即可。

    代码:

    1. #include<iostream>
    2. #include<algorithm>
    3. #include<cstring>
    4. #include<cstdio>
    5. #include<cmath>
    6. #include<vector>
    7. #include<queue>
    8. using namespace std;
    9. const int N=2e5+10;
    10. char s[N];
    11. int main()
    12. {
    13. int T;
    14. cin>>T;
    15. while(T--)
    16. {
    17. int n;
    18. scanf("%d",&n);
    19. scanf("%s",s+1);
    20. int cnt0=0,cnt1=0;
    21. for(int i=1;i<=n;i++)
    22. {
    23. if(s[i]=='0') cnt0++;
    24. else cnt1++;
    25. }
    26. long long ans=1ll*cnt0*cnt1;
    27. if(!cnt0) ans=1ll*cnt1*cnt1;
    28. else if(!cnt1) ans=1ll*cnt0*cnt0;
    29. int mx0=0,mx1=0;
    30. for(int i=2;i<=n;i++)
    31. {
    32. int id=1;
    33. while(i<=n&&s[i]==s[i-1]) i++,id++;
    34. if(s[i-1]=='0') mx0=max(mx0,id);
    35. else mx1=max(mx1,id);
    36. }
    37. ans=max(ans,max(1ll*mx0*mx0,1ll*mx1*mx1));
    38. printf("%lld\n",ans);
    39. }
    40. return 0;
    41. }

    C. Complementary XOR

    题目链接:Problem - C - Codeforces

     样例输入:

    1. 5
    2. 3
    3. 010
    4. 101
    5. 2
    6. 11
    7. 10
    8. 4
    9. 1000
    10. 0011
    11. 2
    12. 10
    13. 10
    14. 3
    15. 111
    16. 111

    样例输出:

    1. YES
    2. 1
    3. 2 2
    4. NO
    5. NO
    6. YES
    7. 2
    8. 1 2
    9. 2 2
    10. YES
    11. 2
    12. 1 1
    13. 2 3

    题意:给定两个长度为n的01串a和b,每次操作我们可以选择一个区间[l,r],将a串中位于该区间内的数字反转,b串中不在该区间内的数字反转。问能否通过该操作使得a串和b串全部变成0串,如果能就输出操作方案,否则输出-1.

    分析:首先我们不难发现,只有当a串和b串完全相反或者完全相同时才能使得经过操作后全部变成0串。如果a串和b串一开始完全相同的话那么我们可以先对整个区间进行一次操作,那么a串和b串就会变成完全相反。然后我们每次对a串中为1的位置进行操作即可每次选定一个区间操作后,a串和b串要么完全相同,要么完全相反,那么当a串变为全0后,b串要么全0,要么全1,如果要是全0的话那么我们就不需要额外操作,否则选定区间[1,1],[2,n],[1,n]分别进行一次操作即可使得b数组变为全0。

    代码:

    1. #include<iostream>
    2. #include<algorithm>
    3. #include<cstring>
    4. #include<cstdio>
    5. #include<cmath>
    6. #include<vector>
    7. #include<queue>
    8. using namespace std;
    9. const int N=2e5+10;
    10. char s1[N],s2[N];
    11. int l[N],r[N];
    12. int main()
    13. {
    14. int T;
    15. cin>>T;
    16. while(T--)
    17. {
    18. int n;
    19. scanf("%d",&n);
    20. scanf("%s%s",s1+1,s2+1);
    21. bool flag=true;
    22. int tt=0;
    23. if(s1[1]==s2[1])
    24. {
    25. l[++tt]=1,r[tt]=n;
    26. for(int i=1;i<=n;i++)
    27. s1[i]='0'+((s1[i]-'0')^1);
    28. }
    29. for(int i=2;i<=n;i++)
    30. if(s1[i]==s2[i])
    31. {
    32. flag=false;
    33. break;
    34. }
    35. if(!flag)
    36. {
    37. puts("NO");
    38. continue;
    39. }
    40. int cnt10=0,cnt20=0;
    41. for(int i=1;i<=n;i++)
    42. {
    43. if(s1[i]=='0') cnt10++;
    44. else cnt20++;
    45. }
    46. puts("YES");
    47. int cnt=0;//记录s11的个数
    48. for(int i=1;i<=n;i++)
    49. if(s1[i]=='1')
    50. l[++tt]=i,r[tt]=i,cnt++;
    51. if(cnt&1)
    52. {
    53. printf("%d\n",tt);
    54. for(int i=1;i<=tt;i++)
    55. printf("%d %d\n",l[i],r[i]);
    56. }
    57. else
    58. {
    59. l[++tt]=1,r[tt]=1;
    60. l[++tt]=2,r[tt]=n;
    61. l[++tt]=1,r[tt]=n;
    62. printf("%d\n",tt);
    63. for(int i=1;i<=tt;i++)
    64. printf("%d %d\n",l[i],r[i]);
    65. }
    66. }
    67. return 0;
    68. }

    D. Count GCD

    题目链接:Problem - D - Codeforces

    样例输入: 

    1. 5
    2. 3 5
    3. 4 2 1
    4. 2 1
    5. 1 1
    6. 5 50
    7. 2 3 5 2 3
    8. 4 1000000000
    9. 60 30 1 1
    10. 2 1000000000
    11. 1000000000 2

    样例输出:

    1. 3
    2. 1
    3. 0
    4. 595458194
    5. 200000000

    题意:给你一个长度为n的数组a,每个元素的值都是大于等于1小于等于m的,现在让我们构造一个数组b,使得对于任意的1<=i<=n都满足gcd(b1,b2,……,bi)=ai,问这样的b数组的方案数是多少,其中b数组中的元素也都是大于等于1小于等于m的,结果对998244353取模。

    分析:由gcd(a,b,c)=gcd(gcd(a,b),c),我们可以知道a[i+1]=gcd(b1,b2,……,b[i+1])=gcd(gcd(b1,b2,……,bi),b[i+1])=gcd(ai,b[i+1]),其中b[i+1]<=m,那么就等价于求解满足等式a[i+1]=gcd(a[i],b[i+1])的b[i+1]的个数,也就是满足等式gcd(a[i]/a[i+1],b[i+1]/a[i+1])=1的b[i+1]/a[i+1]的个数,那么这个我们显然可以直接通过容斥(a[i]/a[i+1])的因子来进行求解。不会容斥原理的小伙伴可以看下这里:容斥原理_AC__dream的博客-CSDN博客_容斥原理csdn

    但是需要注意的一点就是如果a[i+1]不是a[i]的因子时我们直接退出循环即可,否则会超时,因为实际上我们每次计算的gcd是非递增的,但是如果不考虑a[i+1]和a[i]的关系,那么我们计算的复杂度就不会一直减小,那么就会超时。

    细节见代码:

    1. #include<iostream>
    2. #include<algorithm>
    3. #include<cstring>
    4. #include<cstdio>
    5. #include<cmath>
    6. #include<vector>
    7. #include<queue>
    8. using namespace std;
    9. const int N=2e5+10,mod=998244353;
    10. long long a[N];
    11. int log_2[N];
    12. int lowbit(int x)
    13. {
    14. return x&-x;
    15. }
    16. int main()
    17. {
    18. int T;
    19. cin>>T;
    20. for(int i=0;(1<<i)<N;i++) log_2[1<<i]=i;//预处理一下以2为底的对数数组
    21. while(T--)
    22. {
    23. int n,m;
    24. scanf("%d%d",&n,&m);
    25. for(int i=1;i<=n;i++)
    26. scanf("%lld",&a[i]);
    27. long long ans=1;
    28. for(int i=2;i<=n;i++)
    29. {
    30. long long tm=m/a[i];//上界
    31. long long ta=a[i-1]/a[i];
    32. if(ta*a[i]!=a[i-1])
    33. {
    34. ans=0;//无解
    35. break;
    36. }
    37. long long tans=0;
    38. vector<int>p;//存储质因子
    39. for(int j=2;j*j<=ta;j++)
    40. {
    41. if(ta%j==0)
    42. {
    43. p.push_back(j);
    44. while(ta%j==0) ta/=j;
    45. }
    46. }
    47. if(ta>1) p.push_back(ta);
    48. int tt=p.size();
    49. for(int j=0;j<1<<tt;j++)
    50. {
    51. int sign=1;
    52. long long t=1;
    53. int jj=j;
    54. while(jj)
    55. {
    56. sign*=-1;
    57. t*=p[log_2[lowbit(jj)]];
    58. jj-=lowbit(jj);
    59. if(t>tm) break;
    60. }
    61. tans+=sign*(tm/t);
    62. }
    63. ans=ans*tans%mod;
    64. }
    65. printf("%lld\n",ans);
    66. }
    67. return 0;
    68. }

  • 相关阅读:
    MATLAB环境下基于深度学习的JPEG图像去块(Image Deblocking)
    【算法leetcode】2341. 数组能形成多少数对(多种语言双百)
    sudo 问题 不是 sudoers --chatGPT
    多精度 simulator 中的 RL:一篇 14 年 ICRA 的古早论文
    【好书推荐】计算机网络:自顶向下方法(第七版)
    167. 两数之和 II - 输入有序数组、Leetcode的Python实现
    TuGraph安装与简单使用
    青少年护眼台灯哪个牌子好?国家认可的护眼台灯品牌
    扩容ASM共享存储
    分析 NFT 项目的 5 个指标
  • 原文地址:https://blog.csdn.net/AC__dream/article/details/127795390