• Codeforces Round #813 (Div. 2)


    好久没打cf了,,,这个假期太摆烂了呜呜呜,应该会掉大分

    (不知道为啥没rated,也没给我发消息啊???)

    似乎是手速场,可惜跟不上节奏了

    A. Wonderful Permutation

    给出一个permutation和一个k,每次交换可以使得任意两数互换,使得前k个数和最小的做少操作次数是多少。

    思路:前k个数之和最小肯定是1加到k,map维护一下,仅交换不在这个范围内的即可。

    AC Code:

    1. #include
    2. typedef long long ll;
    3. const int N=1e5+5;
    4. int t,n,k;
    5. int a[N];
    6. std::map<int,int>mp;
    7. int main(){
    8. std::ios::sync_with_stdio(false);
    9. std::cin.tie(0);
    10. std::cout.tie(0);
    11. std::cin>>t;
    12. while(t--){
    13. std::cin>>n>>k;
    14. mp.clear();
    15. for(int i=1;i<=k;i++){
    16. mp[i]++;
    17. }
    18. int ans=0;
    19. for(int i=1;i<=n;i++){
    20. std::cin>>a[i];
    21. if(i<=k&&!mp[a[i]]) ans++;
    22. }
    23. std::cout<'\n';
    24. }
    25. return 0;
    26. }

     B. Woeful Permutation

    给出一个n,构造一个permutation,使得每个i和p[i]的最小公因数之和最大。

    思路:首先某个数一定与相邻的数互质,满足这个条件可以使得最小公因数最大,那么最大化利用每个数的方法是交换相邻两数,注意奇数个数时首先从最后开始交换。

    AC Code:

    1. #include
    2. typedef long long ll;
    3. const int N=1e5+5;
    4. int t,n;
    5. int ans[N];
    6. int main(){
    7. std::ios::sync_with_stdio(false);
    8. std::cin.tie(0);
    9. std::cout.tie(0);
    10. std::cin>>t;
    11. while(t--){
    12. std::cin>>n;
    13. for(int i=1;i<=n;i++){
    14. ans[i]=i;
    15. }
    16. if(n&1){
    17. for(int i=2;i<=n;i+=2){
    18. std::swap(ans[i],ans[i+1]);
    19. }
    20. }
    21. else{
    22. for(int i=1;i<=n;i+=2){
    23. std::swap(ans[i],ans[i+1]);
    24. }
    25. }
    26. for(int i=1;i<=n;i++){
    27. std::cout<" \n"[i==n];
    28. }
    29. }
    30. return 0;
    31. }

     os:假期躺平,后果就是B题还会WA两次

    C. Sort Zero

    给出一个数组a,每次操作可以选择一个数x并使数组中所有值为x的元素变成0,最少需要几次操作使得数组非递减排列。

     思路:贪心即可。遇到递减的一对数,就把前面的都变为0,,注意标记前面变为0的数,后面不要重复操作了。

    考虑贪心的可行性,只要有递减的一对数,想将其变成合法序列,必须前一个数变小,那只能是0,前面大于0的数也必须相应变为0,而后面等于该数的位置也会变成0,一系列也就只能全为0了。注意代码写法,map相互赋值是O(n)的。其实好像也不难想,,是我菜了

    AC Code:

    1. #include
    2. typedef long long ll;
    3. const int N=1e5+5;
    4. int t,n,ans;
    5. int a[N];
    6. std::map<int,int>mp;
    7. int main(){
    8. std::ios::sync_with_stdio(false);
    9. std::cin.tie(0);
    10. std::cout.tie(0);
    11. std::cin>>t;
    12. while(t--){
    13. std::cin>>n;
    14. ans=0;
    15. for(int i=1;i<=n;i++){
    16. std::cin>>a[i];
    17. }
    18. mp.clear();
    19. int pos=-1;
    20. if(a[1]) mp[a[1]]=1;
    21. for(int i=2;i<=n;i++){
    22. if(a[i]-1]){
    23. ans=mp.size();
    24. pos=i;
    25. }
    26. if(anssize()&&mp[a[i]]&&mp[a[i]]
    27. ans=mp.size();
    28. }
    29. if(!mp[a[i]]&&a[i]) mp[a[i]]=i;
    30. }
    31. std::cout<'\n';
    32. }
    33. return 0;
    34. }

    os:还因为mapT了一发,寄

    D. Empty Graph

    给出一个数组a,有k次操作,每次操作可以令a数组中某个数变为x,n个点的无向图,l点到r点的距离是区间l到r在数组a中的最小值。问在图中最长边的值。

    思路:学习大佬的思路首先有个结论是显然的,区间越长,最小值不会较大。这样要求最大的值,所以有三条路线是最优的:1、l->r;2、l->1->r(a数组中最小值在1~l中,且2*d[1,n]n->r(a数组中最小值在r~n中,且2*d[1,n]

    AC Code:

    1. #include
    2. typedef long long ll;
    3. const int N=1e5+5;
    4. int t,n,k;
    5. int a[N],b[N];
    6. bool check(int x){
    7. int nk=k;
    8. for(int i=1;i<=n;i++){
    9. if(a[i]*2
    10. b[i]=1e9;
    11. nk--;
    12. if(nk<0) return false;
    13. }
    14. else b[i]=a[i];
    15. }
    16. if(nk>=2) return true;
    17. for(int i=2;i<=n;i++){
    18. if(std::min(b[i],b[i-1])>=x) return true;
    19. if(std::max(b[i],b[i-1])>=x&&nk>=1) return true;
    20. }
    21. return false;
    22. }
    23. int main(){
    24. std::ios::sync_with_stdio(false);
    25. std::cin.tie(0);
    26. std::cout.tie(0);
    27. std::cin>>t;
    28. while(t--){
    29. std::cin>>n>>k;
    30. for(int i=1;i<=n;i++){
    31. std::cin>>a[i];
    32. }
    33. int l=1,r=1e9;
    34. while(l
    35. int mid=l+r+1>>1;
    36. if(check(mid)) l=mid;
    37. else r=mid-1;
    38. }
    39. std::cout<'\n';
    40. }
    41. return 0;
    42. }

  • 相关阅读:
    【项目】若依框架如何实现批量导入,并解析出表中内容返回给前端? - poi依赖
    OneAuth 2022.11.23版本更新内容
    upload-labs靶场通关指南(5-8关)
    你好,面试官 | 终于上岸了,你会哪些 JVM 调优参数?呆住了。。。
    【Mybatis】常见面试题:处理表与表之间的关系:多对一,一对多
    2023年中国背光显示面板分类、市场规模及企业分析[图]
    大华同轴电缆低时延监控方案300ms
    CISSP通关学习笔记:共计 9 个章节(已完结)
    七、栈与队列(stack and queue)
    运动项目记录
  • 原文地址:https://blog.csdn.net/m0_62289613/article/details/126326593