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

A - ABA and BAB

A - ABA and BAB (atcoder.jp)

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

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

#pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
// const int mod=1e9+7;
const int MX=0x3f3f3f3f3f3f3f3f; 
//inline int read()                     //快读
//{
//    int xr=0,F=1; char cr;
//    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
//    while(cr>='0'&&cr<='9')
//        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
//    return xr*F;
//}
//void write(int x)                     //快写
//{
//    if(x<0) putchar('-'),x=-x;
//    if(x>9) write(x/10); putchar(x%10+'0');
//}
// 比 unordered_map 更快的哈希表
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
// struct chash {
//     int operator()(int x) const { return x ^ RANDOM; }
// };
// typedef gp_hash_table<int, int, chash> hash_t;
constexpr ll mod = 1e9 + 7;                                //此处为自动取模的数
class modint{
    ll num;
public:
    modint(ll num = 0) :num(num % mod){}
 
    ll val() const {
        return num;
    }
 
    modint pow(ll other) {
        modint res(1), temp = *this;
        while(other) {
            if(other & 1) res = res * temp;
            temp = temp * temp;
            other >>= 1;
        }
        return res;
    }
 
    constexpr ll norm(ll num) const {
        if (num < 0) num += mod;
        if (num >= mod) num -= mod;
        return num;
    }
 
    modint inv(){ return pow(mod - 2); }
    modint operator+(modint other){ return modint(num + other.num); }
    modint operator-(){ return { -num }; }
    modint operator-(modint other){ return modint(-other + *this); }
    modint operator*(modint other){ return modint(num * other.num); }
    modint operator/(modint other){ return *this * other.inv(); }
    modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
    modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
    modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
    modint &operator/=(modint other) { return *this *= other.inv(); }
    friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
    friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
};

int n;
string s;
void icealsoheat(){
    
    cin>>n;
    cin>>s;
    s=" "+s;
    int res=1;
    modint ans=1;
    for(int i=2;i<=n;i++){
        if(s[i]!=s[i-1]){
            res++;
        }
        else{
            if(res>=3){
                ans*=(res+1)/2;
            }
            res=1;
        }

    }
    if(res>=3){
        ans*=(res+1)/2;
    }

    cout<<ans;
}
signed main(){
    ios::sync_with_stdio(false);          //int128不能用快读!!!!!!
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}
//
//⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
//⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
//⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
//⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
//⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
//⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
//⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
//⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
//

B - Improve Inversions

B - Improve Inversions (atcoder.jp)

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

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

#pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
// const int mod=1e9+7;
const int MX=0x3f3f3f3f3f3f3f3f; 
//inline int read()                     //快读
//{
//    int xr=0,F=1; char cr;
//    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
//    while(cr>='0'&&cr<='9')
//        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
//    return xr*F;
//}
//void write(int x)                     //快写
//{
//    if(x<0) putchar('-'),x=-x;
//    if(x>9) write(x/10); putchar(x%10+'0');
//}
// 比 unordered_map 更快的哈希表
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
// struct chash {
//     int operator()(int x) const { return x ^ RANDOM; }
// };
// typedef gp_hash_table<int, int, chash> hash_t;
// constexpr ll mod = 1e9 + 7;                                //此处为自动取模的数
// class modint{
//     ll num;
// public:
//     modint(ll num = 0) :num(num % mod){}
 
//     ll val() const {
//         return num;
//     }
 
//     modint pow(ll other) {
//         modint res(1), temp = *this;
//         while(other) {
//             if(other & 1) res = res * temp;
//             temp = temp * temp;
//             other >>= 1;
//         }
//         return res;
//     }
 
//     constexpr ll norm(ll num) const {
//         if (num < 0) num += mod;
//         if (num >= mod) num -= mod;
//         return num;
//     }
 
//     modint inv(){ return pow(mod - 2); }
//     modint operator+(modint other){ return modint(num + other.num); }
//     modint operator-(){ return { -num }; }
//     modint operator-(modint other){ return modint(-other + *this); }
//     modint operator*(modint other){ return modint(num * other.num); }
//     modint operator/(modint other){ return *this * other.inv(); }
//     modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
//     modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
//     modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
//     modint &operator/=(modint other) { return *this *= other.inv(); }
//     friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
//     friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
// };

int n,k;
int a[200005];
int p[200005];
vector<PII>ans;
void icealsoheat(){
    
    cin>>n>>k;
    int bns=0;
    for(int i=1;i<=n;i++)cin>>a[i],p[a[i]]=i;

    for(int i=1;i<=n;i++){

        int id=p[i];
        int x=i;
        for(int j=i-1;j>=1;j--){

            if(p[j]>=id+k){
                // cout<<p[x]<<":::"<<p[j]<<"\n";
                ans.push_back({p[x],p[j]});
                a[id]=j;
                a[p[j]]=x;
                swap(p[x],p[j]);
                x=j;

            }

        }

    }

    cout<<ans.size()<<"\n";
    for(auto [i,j]:ans){
        cout<<i<<" "<<j<<"\n";
    }

    // for(int i=1;i<=n;i++){
    //     cout<<a[i]<<" ";
    // }

}
signed main(){
    ios::sync_with_stdio(false);          //int128不能用快读!!!!!!
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}
//
//⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
//⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
//⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
//⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
//⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
//⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
//⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
//⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
//

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的情况是重复的,所以我们要想办法将它去重。

#pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
// const int mod=1e9+7;
const int MX=0x3f3f3f3f3f3f3f3f; 
//inline int read()                     //快读
//{
//    int xr=0,F=1; char cr;
//    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
//    while(cr>='0'&&cr<='9')
//        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
//    return xr*F;
//}
//void write(int x)                     //快写
//{
//    if(x<0) putchar('-'),x=-x;
//    if(x>9) write(x/10); putchar(x%10+'0');
//}
// 比 unordered_map 更快的哈希表
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
// struct chash {
//     int operator()(int x) const { return x ^ RANDOM; }
// };
// typedef gp_hash_table<int, int, chash> hash_t;
constexpr ll mod = 1e9 + 7;                                //此处为自动取模的数
class modint{
    ll num;
public:
    modint(ll num = 0) :num(num % mod){}
 
    ll val() const {
        return num;
    }
 
    modint pow(ll other) {
        modint res(1), temp = *this;
        while(other) {
            if(other & 1) res = res * temp;
            temp = temp * temp;
            other >>= 1;
        }
        return res;
    }
 
    constexpr ll norm(ll num) const {
        if (num < 0) num += mod;
        if (num >= mod) num -= mod;
        return num;
    }
 
    modint inv(){ return pow(mod - 2); }
    modint operator+(modint other){ return modint(num + other.num); }
    modint operator-(){ return { -num }; }
    modint operator-(modint other){ return modint(-other + *this); }
    modint operator*(modint other){ return modint(num * other.num); }
    modint operator/(modint other){ return *this * other.inv(); }
    modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
    modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
    modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
    modint &operator/=(modint other) { return *this *= other.inv(); }
    friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
    friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
};

int n,k;
int a[500005];
modint dp[105][5005];
modint sum[5005];
void icealsoheat(){
    
    cin>>n;
    for(int i=0;i<=20;i++)if(i!=10)sum[i]=1;
    for(int i=1;i<=n;i++)cin>>a[i];

    modint ans=1;
    for(int i=0;i<n;i++){

        dp[i][a[i]+1000]=dp[i][a[i]+1000]+sum[a[i]+10];
        sum[a[i]+10]=0;

        for(int j=0;j<=2000;j++){
            if(j==1000)continue;

            for(int o=i+1;o<=n;o++){
                // if(j+a[o]<0)cout<<"+++\n";
                if(j+a[o]<0)continue;
                
                dp[o][j+a[o]]+=dp[i][j];

                ans+=dp[i][j];

            }

        }

        for(int j=0;j<=20;j++){
            if(j!=10)sum[j]+=dp[i][1000];
        }

    }
    cout<<dp[2][1]<<"+++\n";

    cout<<ans;

}
signed main(){
    ios::sync_with_stdio(false);          //int128不能用快读!!!!!!
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}
//
//⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
//⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
//⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
//⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
//⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
//⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
//⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
//⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
//

相关推荐

  1. AC修炼计划(AtCoder Regular Contest 179)A~C

    2024-07-11 09:46:01       19 阅读
  2. AC修炼计划( AtCoder Regular Contest 178)A~C

    2024-07-11 09:46:01       20 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 09:46:01       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 09:46:01       56 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 09:46:01       46 阅读
  4. Python语言-面向对象

    2024-07-11 09:46:01       57 阅读

热门阅读

  1. ORACLE 数据库ADG切换

    2024-07-11 09:46:01       16 阅读
  2. Memcached介绍和详解

    2024-07-11 09:46:01       17 阅读
  3. js实现打印

    2024-07-11 09:46:01       21 阅读
  4. dlib简介

    2024-07-11 09:46:01       22 阅读
  5. PDF文件在线处理工具(侧重数理及论文)

    2024-07-11 09:46:01       15 阅读
  6. 20.js获取页面卷去的距离以及滚到到指定位置

    2024-07-11 09:46:01       21 阅读
  7. 【人脸识别、Python实现】PyQt5人脸识别管理系统

    2024-07-11 09:46:01       19 阅读
  8. Flutter EasyRefresh:介绍与使用指南

    2024-07-11 09:46:01       21 阅读
  9. Perl编译器架构:前端与后端的精细分工

    2024-07-11 09:46:01       21 阅读
  10. Golang 高频面试题 && 答案

    2024-07-11 09:46:01       23 阅读
  11. Spring Boot常用注解类

    2024-07-11 09:46:01       21 阅读
  12. Perl伪哈希探秘:深入理解Perl中的高级数据结构

    2024-07-11 09:46:01       20 阅读