• 2022杭电多校6 L - Loop


    input:

    2
    7 3
    1 4 2 1 4 2 4
    5 2
    4 3 5 4 5

    output:

    4 4 2 4 2 1 1
    5 4 5 4 3

     题目大意:给我们一个序列a,问经过k次操作后通过该序列能形成的序列最大字典序是多少?,操作说明:在序列中指定一段序列[l,r],让al+1~ar往前移动一步,把原来的al放在现在ar的位置。

    解题思路:对于每一次操作我们可以贪心地从前往后找,找到第一个满足ai

    上代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. const int N=3E5+10;
    7. int a[N],st[N],b[N],ans[N];
    8. int n,k;
    9. bool cmp(int x,int y)
    10. {
    11. return x>y;
    12. }
    13. int main()
    14. {
    15. int t;
    16. cin>>t;
    17. while(t--)
    18. {
    19. cin>>n>>k;
    20. for(int i=1;i<=n;i++)
    21. cin>>a[i];
    22. int tt=0,tb=0;
    23. for(int i=1;i<=n;i++)
    24. {
    25. if(tb>=k)
    26. {
    27. st[++tt]=a[i];
    28. continue;
    29. }
    30. //维护一个单调递减栈,至于为什么是递减栈,主要看下面单调栈更新操作
    31. while(tt&&(tb//要想让字典序尽可能地大,应该将a[i]前面比a[i]小的元素尽可能放到a[i]后面来,留在a[i]前面的应该是大于等于a[i]的数
    32. b[++tb]=st[tt--];
    33. st[++tt]=a[i];
    34. }
    35. sort(b+1,b+tb+1,cmp);//b数组中记录的是我们可以通过操作移动到后面的数字
    36. int cnt=0;
    37. int i=1,j=1;
    38. for(;;)
    39. {
    40. if(i>tb||j>tt)
    41. break;
    42. if(b[i]>st[j])//序列的前面尽可能放大数,保证字典序尽可能大
    43. ans[++cnt]=b[i++];
    44. else
    45. ans[++cnt]=st[j++];
    46. }
    47. while(i<=tb)
    48. ans[++cnt]=b[i++];
    49. while(j<=tt)
    50. ans[++cnt]=st[j++];
    51. for(int i=1;i<=n;i++)
    52. {
    53. cout<
    54. if(i
    55. cout<<" ";
    56. else
    57. cout<
    58. }
    59. }
    60. return 0;
    61. }

     

  • 相关阅读:
    用Tinyproxy搭建自己的proxy server
    重学设计模式-适配器模式-spring当中的应用
    盘点6款装机必备软件
    【C++详解】——模板初阶
    算法入门教程(六、试探)
    UEditorPlus v2.7.0发布 开放独立文档,附件样式优化
    2022开源大数据热力报告总结
    爬虫解析——Xpath
    Camera Metadata跨进程传递
    Unity数据加密☀️ 三、加密DLL供Unity使用
  • 原文地址:https://blog.csdn.net/weixin_54562114/article/details/126180861