• 22.10.30补卡 22CCPC桂林站M题


    Problem - M - Codeforces

    M. Youth Finale

    题意是给一个n长度的数组a, 和m长度的字符串str, 先输出a的逆序数, 然后根据str的操作每次更新a的所有逆序数, str[i]为S时a数组左移一位, str[i]为R时, a数组变为当前a数组的倒序

    题解:

    树状数组先求出逆序数, 用双端队列存储数组a, 然后根据str执行操作

    S: 记录一下当前S的方向, 每执行一次R的操作会使得S反向, 正向即队头出队再进入队尾, 反向则队尾出队再进入队头,

    R: 使得当前的方向反向, 记当前逆序数为ans, 队列方向转换(逆序数变为顺序数), 则有ans=\sum_{i=1}^{n-1}-ans得到顺序数为当前反向队列的逆序数

    值得一提的是这题可以不用队列, 用数组(记录一个pos%m)或者vector(利用库函数pop_back)实现S操作, R用reverse函数即可, 甚至不需要记录方向

    关于S操作, 我们还需要维护ans的值

    首先要知道, 因为数组a严格要求从1~n出现, 那么队头的逆序数=队头元素的值.

    我们可以观察到, 当队头出队再进入队尾的过程中ans的变化: 数组中大于队头的元素逆序数++(ans+=n-队头的值+1   ps:这个+1是因为不包括队头自己本身 ), 而因为队头出队再进入队尾, 队头的逆序数(即小于队头的元素)归零了(ans-=队头的值)

    整理成公式得: ans+=n-(队头的值-1)-队头的值

    (反向把队头改为队尾即可)

    搞明白之后这题就能ac啦! 是不是很简单

    1. /*
    2. ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
    3. ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
    4. ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
    5. ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
    6. ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
    7. ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
    8. ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
    9. ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
    10. ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
    11. ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
    12. ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
    13. ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
    14. ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
    15. ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
    16. ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
    17. ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
    18. ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
    19. ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
    20. ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
    21. ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
    22. ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
    23. ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
    24. ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
    25. ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
    26. ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
    27. ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
    28. ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
    29. ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
    30. ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
    31. ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
    32. */
    33. #include
    34. #include
    35. #include
    36. #include
    37. #include
    38. #include
    39. #include
    40. #include
    41. #include
    42. #include
    43. #include
    44. #include
    45. #include
    46. #include
    47. using namespace std;
    48. #define ll long long
    49. #define endl "\n"
    50. #define R cin>>
    51. #define S second
    52. #define F first
    53. #define ln cout<
    54. #define rep(i, a, b) for (ll i = (a); i <= (b); i++)
    55. #define repr(i, a, b) for (ll i = (a); i < (b); i++)
    56. #define rrep(i, a, b) for (ll i = (b); i >= (a); i--)
    57. #define rrepr(i, a, b) for (ll i = (b); i > (a); i--)
    58. #define min(a,b) ((a)<(b)?(a):(b))
    59. #define max(a,b) ((a)>(b)?(a):(b))
    60. #define yes cout<<"YES"<
    61. #define no cout<<"NO"<
    62. #define debug cout<<"here!"<
    63. ll cnt,n,m,t,ans,ant;
    64. const int N=3e5+10;
    65. const int INF=0x3f3f3f3f;
    66. const ll llINF=0x3f3f3f3f3f3f3f3f;
    67. ll arr[N],b[N];
    68. // vector>v;
    69. dequeq;
    70. string str;
    71. inline ll read()
    72. {
    73. char c = getchar();int x = 0,s = 1;
    74. while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}//是符号
    75. while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}//是数字
    76. return x*s;
    77. }
    78. ll lowbit(ll x)
    79. {
    80. return x&(-x);
    81. }
    82. ll getsum(ll x)
    83. {
    84. ll sum=0;
    85. while(x>0)
    86. {
    87. sum+=arr[x];
    88. x-=lowbit(x);
    89. }
    90. return sum;
    91. }
    92. void insert(ll x,ll y)
    93. {
    94. while(x<=n)
    95. {
    96. arr[x]+=y;
    97. x+=lowbit(x);
    98. }
    99. }
    100. void solve()
    101. {
    102. R n>>m;
    103. ll mmax=0;
    104. repr(i,1,n) mmax+=i;
    105. rep(i,1,n)
    106. {
    107. R cnt;
    108. // mmax=max(cnt,mmax);
    109. q.push_back(cnt);
    110. insert(cnt,1);
    111. ant=i-getsum(cnt);
    112. ans+=ant;
    113. }
    114. cout<
    115. R str;
    116. ant=1;
    117. repr(i,0,m)
    118. {
    119. if(str[i]=='S'&&ant)
    120. {
    121. cnt=q.front();
    122. ans+=n+1-cnt*2;
    123. q.pop_front();
    124. q.push_back(cnt);
    125. }else if(str[i]=='S'&&ant==0)
    126. {
    127. cnt=q.back();
    128. ans+=n+1-cnt*2;
    129. q.pop_back();
    130. q.push_front(cnt);
    131. }
    132. // if(str[i]=='S'&&ant==0)
    133. // {
    134. // auto tp=v[0];
    135. // reverse(v.begin(),v.end());
    136. // v.pop_back();
    137. // reverse(v.begin(),v.end());
    138. // v.push_back(tp);
    139. // ans+=n-tp.first-tp.first+1;
    140. // }
    141. // else if(str[i]=='S'&&ant)
    142. // {
    143. // auto tp=v[0];
    144. // reverse(v.begin(),v.end());
    145. // v.pop_back();
    146. // reverse(v.begin(),v.end());
    147. // v.push_back(tp);
    148. // ans+=n-tp.first-tp.first+1;
    149. // }
    150. else
    151. {
    152. if(ant) ant=0;
    153. else ant=1;
    154. // reverse(v.begin(),v.end());
    155. ans=mmax-ans;
    156. }
    157. //求逆序数
    158. cout<10;
    159. }
    160. return;
    161. }
    162. int main()
    163. {
    164. ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    165. //所有输入用cin
    166. //所有输出用cout
    167. // cin>>t;
    168. // while(t--)
    169. solve();
    170. return 0;
    171. }

  • 相关阅读:
    在印度,越来越多的女性游戏开发者参与其中
    分布式锁使用
    蓝桥杯冲刺_二分(正在补题)
    这次彻底学明白了:四种机器学习超参数搜索方法太详细了
    html5里引用vue.js,实现自定义组件,子父组件相互传参
    看完阿里最新产500页微服务架构笔记,感觉我格局太小
    acwing算法提高之图论--最小生成树的典型应用
    如何编写一个投票功能的智能合约
    MnasNet学习笔记
    FFT专题:IFFT后信号如何重建
  • 原文地址:https://blog.csdn.net/Tanya_xiaomai/article/details/127621346