• (cf)Codeforces Round #825 (Div. 2)ABC(BC详细解释)


    目录

    A. Make A Equal to B

     B. Playing with GCD

     C1. Good Subarrays (Easy Version)


    这一场wa了n发,醉了,明天再补一场

    A. Make A Equal to B

    You are given two arrays aa and bb of nn elements, each element is either 00 or 11.

    You can make operations of 22 kinds.

    • Pick an index ii and change aiai to 1−ai1−ai.
    • Rearrange the array aa however you want.

    Find the minimum number of operations required to make aa equal to bb.

    Input

    Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤4001≤t≤400) — the number of test cases. Description of the test cases follows.

    The first line of each test case contains a single integer nn (1≤n≤1001≤n≤100) — the length of the arrays aa and bb.

    The second line of each test case contains nn space-separated integers a1,a2,…,ana1,a2,…,an (aiai is 00 or 11), representing the array aa.

    The third line of each test case contains nn space-separated integers b1,b2,…,bnb1,b2,…,bn (bibi is 00 or 11), representing the array bb.

    Output

    For each test case, print the minimum number of operations required to make aa equal to bb.

     比较简单,不详细说了

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. const int N=1000010;
    4. int a[N],b[N];
    5. int main()
    6. {
    7. int t;
    8. cin>>t;
    9. int n;
    10. while(t--)
    11. {
    12. scanf("%d",&n);
    13. int cnt1=0,cnt0=0,cnt11=0,cnt00=0;
    14. for(int i=1;i<=n;i++)
    15. {
    16. scanf("%d",&a[i]);
    17. if(a[i]==1)
    18. cnt1++;
    19. else cnt0++;
    20. }
    21. for(int i=1;i<=n;i++)
    22. {
    23. scanf("%d",&b[i]);
    24. if(b[i]==1)
    25. cnt11++;
    26. else cnt00++;
    27. }
    28. int minn=abs(cnt11-cnt1)+1;
    29. int cnt=0;
    30. for(int i=1;i<=n;i++)
    31. {
    32. if(a[i]!=b[i])
    33. cnt++;
    34. }
    35. printf("%d\n",min(minn,cnt));
    36. }
    37. return 0;
    38. }

     B. Playing with GCD

    You are given an integer array aa of length nn.

    Does there exist an array bb consisting of n+1n+1 positive integers such that ai=gcd(bi,bi+1)ai=gcd(bi,bi+1) for all ii (1≤i≤n1≤i≤n)?

    Note that gcd(x,y)gcd(x,y) denotes the greatest common divisor (GCD) of integers xx and yy.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051≤t≤105). Description of the test cases follows.

    The first line of each test case contains an integer nn (1≤n≤1051≤n≤105) — the length of the array aa.

    The second line of each test case contains nn space-separated integers a1,a2,…,ana1,a2,…,an representing the array aa (1≤ai≤1041≤ai≤104).

    It is guaranteed that the sum of nn over all test cases does not exceed 105105.

    Output

    For each test case, output "YES" if such bb exists, otherwise output "NO". You can print each letter in any case (upper or lower).

    题意:给你长度为n的数组a,问你能不能构造出一个长度为n+1的序列b,使每个a[i]=gcd(b[i],b[i+1]),输出NO,YES;

    思路:因为a[i]=gcd(b[i],b[i+1]),a[i+1]=gcd(b[i+1],b[i+2]),所以b[i+1]是a[i]和a[i+1]的公倍数,同时因为b和a之间是最大公因数的关系,所以反过来说b[i+1]应该是a[i]和a[i+1]的最小公倍数。

    所以我们可以先根据这个构造一个b序列,然后反过来看b序列是不是a的最大公因数。

    需要注意一点,b[1]的值要构造为a[1],b[n+1]的值构造为a[n],因为一个数最大的因子就是他自己。

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. const int N=1000010;
    4. int a[N];
    5. int b[N];
    6. int f(int x,int y)
    7. {
    8. return x*y/__gcd(x,y);
    9. }
    10. int main()
    11. {
    12. int t;
    13. cin>>t;
    14. while(t--)
    15. {
    16. int n;
    17. scanf("%d",&n);
    18. for(int i=1;i<=n;i++)
    19. {
    20. scanf("%d",&a[i]);
    21. }
    22. b[1]=a[1];
    23. for(int i=2;i<=n;i++)
    24. {
    25. b[i]=f(a[i-1],a[i]);
    26. }
    27. b[n+1]=a[n];
    28. int i;
    29. for(i=1;i<=n;i++)
    30. {
    31. if(a[i]!=__gcd(b[i],b[i+1]))
    32. {
    33. break;
    34. }
    35. }
    36. if(i<=n)
    37. printf("NO\n");
    38. else printf("YES\n");
    39. }
    40. return 0;
    41. }

     C1. Good Subarrays (Easy Version)

    This is the easy version of this problem. In this version, we do not have queries. Note that we have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.

    An array bb of length mm is good if for all ii the ii-th element is greater than or equal to ii. In other words, bb is good if and only if bi≥ibi≥i for all ii (1≤i≤m1≤i≤m).

    You are given an array aa consisting of nn positive integers. Find the number of pairs of indices (l,r)(l,r), where 1≤l≤r≤n1≤l≤r≤n, such that the array [al,al+1,…,ar][al,al+1,…,ar] is good.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2⋅1051≤t≤2⋅105). Description of the test cases follows.

    The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105), the length of the array aa.

    The second line of each test case contains nn space-separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), representing the array aa.

    It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

    Output

    For each test case, print the number of suitable pairs of indices.

    题意: 给你一个长度为n的序列,要是一段序列里面对于每一个a[i]的值都>=i,那么这就是一个好序列,问你一个序列里面有多少个好序列。

    每一段序列的从下标1开始。

    思路:双指针法,一个指针i表示序列的开始位置,一个指针j表示  序列的结束位置-1,我们可以看出,如果序列长的时候(或者说开始位置离他远的时候)这个数是好的,那么序列短的时候它也一定是好的。

    所以我们只要让指针j一直往后判断就可以了。

    1. #include
    2. using namespace std;
    3. const int N=200010;
    4. int a[N];
    5. int main()
    6. {
    7. int t;
    8. cin>>t;
    9. while(t--)
    10. {
    11. int n;
    12. scanf("%d",&n);
    13. for(int i=1;i<=n;i++)
    14. {
    15. scanf("%d",&a[i]);
    16. }
    17. int k;
    18. for(int i=1;i<=n;i++)
    19. {
    20. k=i;
    21. if(a[i]
    22. {
    23. break;
    24. }
    25. }
    26. if(k==n&&a[n]>=n)
    27. k++;//k指向的永远是当前第一个不满足条件的数的下标,如果超过n就指向n+1。
    28. long long int ans=0;
    29. k=max(2,k);//k最小是2
    30. for(int i=1;i<=n;i++)
    31. {
    32. while(k<=n)
    33. {
    34. if(a[k]+i-1>=k)
    35. k++;
    36. else break;
    37. }
    38. ans+=k-i;
    39. if(k==i+1)//这里防止k指针跑到i前面的情况
    40. k++;
    41. }
    42. printf("%lld\n",ans);
    43. }
    44. return 0;
    45. }

  • 相关阅读:
    Aptos 中文文档
    kubernetes-Service
    AI:146-基于图像特征的法庭口供真实性分析
    USACO Training 1.3 Duel Palindromes
    LeetCode 62. 不同路径
    希望你多出去看看,别活在短视频和文字里!
    算法篇------动态规划2
    小程序为什么必须使用SSL证书?
    推荐系统(RS)
    3、宽带对称式高回退Doherty放大器ADS仿真(带版图)
  • 原文地址:https://blog.csdn.net/weixin_62848089/article/details/127639304