• AC修炼计划(AtCoder Regular Contest 180) A~C


    A - ABA and BAB

    A - ABA and BAB (atcoder.jp)

    这道题我一开始想复杂了,一直在想怎么dp,没注意到其实是个很简单的规律题。

    我们可以发现我们住需要统计一下类似ABABA这样不同字母相互交替的所有子段的长度,而每个字段的的情况有(长度+1)/2种,最后所有字段情况的乘积就是最终答案。

    1. #pragma GCC optimize(3) //O2优化开启
    2. #include
    3. using namespace std;
    4. #define int long long
    5. typedef long long ll;
    6. typedef unsigned long long ull;
    7. typedef pair<int,int> PII;
    8. // const int mod=1e9+7;
    9. const int MX=0x3f3f3f3f3f3f3f3f;
    10. //inline int read() //快读
    11. //{
    12. // int xr=0,F=1; char cr;
    13. // while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
    14. // while(cr>='0'&&cr<='9')
    15. // xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
    16. // return xr*F;
    17. //}
    18. //void write(int x) //快写
    19. //{
    20. // if(x<0) putchar('-'),x=-x;
    21. // if(x>9) write(x/10); putchar(x%10+'0');
    22. //}
    23. // 比 unordered_map 更快的哈希表
    24. // #include
    25. // using namespace __gnu_pbds;
    26. // const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
    27. // struct chash {
    28. // int operator()(int x) const { return x ^ RANDOM; }
    29. // };
    30. // typedef gp_hash_table hash_t;
    31. constexpr ll mod = 1e9 + 7; //此处为自动取模的数
    32. class modint{
    33. ll num;
    34. public:
    35. modint(ll num = 0) :num(num % mod){}
    36. ll val() const {
    37. return num;
    38. }
    39. modint pow(ll other) {
    40. modint res(1), temp = *this;
    41. while(other) {
    42. if(other & 1) res = res * temp;
    43. temp = temp * temp;
    44. other >>= 1;
    45. }
    46. return res;
    47. }
    48. constexpr ll norm(ll num) const {
    49. if (num < 0) num += mod;
    50. if (num >= mod) num -= mod;
    51. return num;
    52. }
    53. modint inv(){ return pow(mod - 2); }
    54. modint operator+(modint other){ return modint(num + other.num); }
    55. modint operator-(){ return { -num }; }
    56. modint operator-(modint other){ return modint(-other + *this); }
    57. modint operator*(modint other){ return modint(num * other.num); }
    58. modint operator/(modint other){ return *this * other.inv(); }
    59. modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
    60. modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
    61. modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
    62. modint &operator/=(modint other) { return *this *= other.inv(); }
    63. friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
    64. friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
    65. };
    66. int n;
    67. string s;
    68. void icealsoheat(){
    69. cin>>n;
    70. cin>>s;
    71. s=" "+s;
    72. int res=1;
    73. modint ans=1;
    74. for(int i=2;i<=n;i++){
    75. if(s[i]!=s[i-1]){
    76. res++;
    77. }
    78. else{
    79. if(res>=3){
    80. ans*=(res+1)/2;
    81. }
    82. res=1;
    83. }
    84. }
    85. if(res>=3){
    86. ans*=(res+1)/2;
    87. }
    88. cout<
    89. }
    90. signed main(){
    91. ios::sync_with_stdio(false); //int128不能用快读!!!!!!
    92. cin.tie();
    93. cout.tie();
    94. int _yq;
    95. _yq=1;
    96. // cin>>_yq;
    97. while(_yq--){
    98. icealsoheat();
    99. }
    100. }
    101. //
    102. //⠀⠀⠀ ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
    103. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
    104. //⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
    105. //⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
    106. //⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
    107. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
    108. //⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
    109. //⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
    110. //⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
    111. //⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
    112. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
    113. //⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
    114. //

    B - Improve Inversions

    B - Improve Inversions (atcoder.jp)

    这题确实不好想,但是get到点儿了就会觉得其实也不难。

    我们需要尽可能的把逆序对最大化,从样例三我们可以发现,我们不妨确立左边一个要交换的下标i,然后找下标大于等于i+k,数值从大到小的找小于ai的数字,并依次与i的数字进行交换。为了尽可能减少替换的影响,我们按数值从小到大的次序去找这个下标i,从而参与上述的交换。因为我们是按从小到大的顺序的,所以小的数字参与交换并不会影响后面大的数字该交换的逆序对。

    1. #pragma GCC optimize(3) //O2优化开启
    2. #include
    3. using namespace std;
    4. #define int long long
    5. typedef long long ll;
    6. typedef unsigned long long ull;
    7. typedef pair<int,int> PII;
    8. // const int mod=1e9+7;
    9. const int MX=0x3f3f3f3f3f3f3f3f;
    10. //inline int read() //快读
    11. //{
    12. // int xr=0,F=1; char cr;
    13. // while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
    14. // while(cr>='0'&&cr<='9')
    15. // xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
    16. // return xr*F;
    17. //}
    18. //void write(int x) //快写
    19. //{
    20. // if(x<0) putchar('-'),x=-x;
    21. // if(x>9) write(x/10); putchar(x%10+'0');
    22. //}
    23. // 比 unordered_map 更快的哈希表
    24. // #include
    25. // using namespace __gnu_pbds;
    26. // const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
    27. // struct chash {
    28. // int operator()(int x) const { return x ^ RANDOM; }
    29. // };
    30. // typedef gp_hash_table hash_t;
    31. // constexpr ll mod = 1e9 + 7; //此处为自动取模的数
    32. // class modint{
    33. // ll num;
    34. // public:
    35. // modint(ll num = 0) :num(num % mod){}
    36. // ll val() const {
    37. // return num;
    38. // }
    39. // modint pow(ll other) {
    40. // modint res(1), temp = *this;
    41. // while(other) {
    42. // if(other & 1) res = res * temp;
    43. // temp = temp * temp;
    44. // other >>= 1;
    45. // }
    46. // return res;
    47. // }
    48. // constexpr ll norm(ll num) const {
    49. // if (num < 0) num += mod;
    50. // if (num >= mod) num -= mod;
    51. // return num;
    52. // }
    53. // modint inv(){ return pow(mod - 2); }
    54. // modint operator+(modint other){ return modint(num + other.num); }
    55. // modint operator-(){ return { -num }; }
    56. // modint operator-(modint other){ return modint(-other + *this); }
    57. // modint operator*(modint other){ return modint(num * other.num); }
    58. // modint operator/(modint other){ return *this * other.inv(); }
    59. // modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
    60. // modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
    61. // modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
    62. // modint &operator/=(modint other) { return *this *= other.inv(); }
    63. // friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
    64. // friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
    65. // };
    66. int n,k;
    67. int a[200005];
    68. int p[200005];
    69. vectorans;
    70. void icealsoheat(){
    71. cin>>n>>k;
    72. int bns=0;
    73. for(int i=1;i<=n;i++)cin>>a[i],p[a[i]]=i;
    74. for(int i=1;i<=n;i++){
    75. int id=p[i];
    76. int x=i;
    77. for(int j=i-1;j>=1;j--){
    78. if(p[j]>=id+k){
    79. // cout<
    80. ans.push_back({p[x],p[j]});
    81. a[id]=j;
    82. a[p[j]]=x;
    83. swap(p[x],p[j]);
    84. x=j;
    85. }
    86. }
    87. }
    88. cout<size()<<"\n";
    89. for(auto [i,j]:ans){
    90. cout<" "<"\n";
    91. }
    92. // for(int i=1;i<=n;i++){
    93. // cout<
    94. // }
    95. }
    96. signed main(){
    97. ios::sync_with_stdio(false); //int128不能用快读!!!!!!
    98. cin.tie();
    99. cout.tie();
    100. int _yq;
    101. _yq=1;
    102. // cin>>_yq;
    103. while(_yq--){
    104. icealsoheat();
    105. }
    106. }
    107. //
    108. //⠀⠀⠀ ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
    109. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
    110. //⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
    111. //⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
    112. //⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
    113. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
    114. //⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
    115. //⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
    116. //⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
    117. //⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
    118. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
    119. //⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
    120. //

    C - Subsequence and Prefix Sum

    C - Subsequence and Prefix Sum (atcoder.jp)

    一道非常巧妙的dp题,他的状态转移非常的奇妙。

    我们考虑前i位的数字对后面数字的贡献值。可以分成两种情况。

    1.第i位数字没有被选中

    2.第i位数字被选中

    当第i位数字被选中时,每一个位数i它所能合成的状态数字都对后面i+1到n的数字有相应的贡献。而这里面0的情况比较特殊,如果第i位的合成数字是0,其实不会改变下一个选中的数字。

    这里面有一种情况比较特殊

    例如1 -1 5 5 .........

    这里我们会发现,我们选择1和-1后,选择第3个5和第4个5的情况是重复的,所以我们要想办法将它去重。

    1. #pragma GCC optimize(3) //O2优化开启
    2. #include
    3. using namespace std;
    4. #define int long long
    5. typedef long long ll;
    6. typedef unsigned long long ull;
    7. typedef pair<int,int> PII;
    8. // const int mod=1e9+7;
    9. const int MX=0x3f3f3f3f3f3f3f3f;
    10. //inline int read() //快读
    11. //{
    12. // int xr=0,F=1; char cr;
    13. // while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
    14. // while(cr>='0'&&cr<='9')
    15. // xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
    16. // return xr*F;
    17. //}
    18. //void write(int x) //快写
    19. //{
    20. // if(x<0) putchar('-'),x=-x;
    21. // if(x>9) write(x/10); putchar(x%10+'0');
    22. //}
    23. // 比 unordered_map 更快的哈希表
    24. // #include
    25. // using namespace __gnu_pbds;
    26. // const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
    27. // struct chash {
    28. // int operator()(int x) const { return x ^ RANDOM; }
    29. // };
    30. // typedef gp_hash_table hash_t;
    31. constexpr ll mod = 1e9 + 7; //此处为自动取模的数
    32. class modint{
    33. ll num;
    34. public:
    35. modint(ll num = 0) :num(num % mod){}
    36. ll val() const {
    37. return num;
    38. }
    39. modint pow(ll other) {
    40. modint res(1), temp = *this;
    41. while(other) {
    42. if(other & 1) res = res * temp;
    43. temp = temp * temp;
    44. other >>= 1;
    45. }
    46. return res;
    47. }
    48. constexpr ll norm(ll num) const {
    49. if (num < 0) num += mod;
    50. if (num >= mod) num -= mod;
    51. return num;
    52. }
    53. modint inv(){ return pow(mod - 2); }
    54. modint operator+(modint other){ return modint(num + other.num); }
    55. modint operator-(){ return { -num }; }
    56. modint operator-(modint other){ return modint(-other + *this); }
    57. modint operator*(modint other){ return modint(num * other.num); }
    58. modint operator/(modint other){ return *this * other.inv(); }
    59. modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
    60. modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
    61. modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
    62. modint &operator/=(modint other) { return *this *= other.inv(); }
    63. friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
    64. friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
    65. };
    66. int n,k;
    67. int a[500005];
    68. modint dp[105][5005];
    69. modint sum[5005];
    70. void icealsoheat(){
    71. cin>>n;
    72. for(int i=0;i<=20;i++)if(i!=10)sum[i]=1;
    73. for(int i=1;i<=n;i++)cin>>a[i];
    74. modint ans=1;
    75. for(int i=0;i
    76. dp[i][a[i]+1000]=dp[i][a[i]+1000]+sum[a[i]+10];
    77. sum[a[i]+10]=0;
    78. for(int j=0;j<=2000;j++){
    79. if(j==1000)continue;
    80. for(int o=i+1;o<=n;o++){
    81. // if(j+a[o]<0)cout<<"+++\n";
    82. if(j+a[o]<0)continue;
    83. dp[o][j+a[o]]+=dp[i][j];
    84. ans+=dp[i][j];
    85. }
    86. }
    87. for(int j=0;j<=20;j++){
    88. if(j!=10)sum[j]+=dp[i][1000];
    89. }
    90. }
    91. cout<2][1]<<"+++\n";
    92. cout<
    93. }
    94. signed main(){
    95. ios::sync_with_stdio(false); //int128不能用快读!!!!!!
    96. cin.tie();
    97. cout.tie();
    98. int _yq;
    99. _yq=1;
    100. // cin>>_yq;
    101. while(_yq--){
    102. icealsoheat();
    103. }
    104. }
    105. //
    106. //⠀⠀⠀ ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
    107. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
    108. //⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
    109. //⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
    110. //⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
    111. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
    112. //⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
    113. //⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
    114. //⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
    115. //⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
    116. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
    117. //⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
    118. //

  • 相关阅读:
    中间件(nginx,网关)对性能的影响的测试
    设计模式之状态模式
    Java学习——基本语法笔记
    el-form自定义规则后表单验证validate不生效的问题
    脊髓型颈椎病是什么?
    第8期ThreadX视频教程:应用实战,将裸机工程移植到RTOS的任务划分,驱动和应用层交互,中断DMA,C库和中间件处理等注意事项
    在农业数据集上运行VINS-fusion
    【知识点】分布式系统相关名词/概念/知识点
    Microsoft.IO.RecyclableMemoryStream源码解读
    Redis 分布式锁
  • 原文地址:https://blog.csdn.net/kyrietheshy/article/details/140312700