首页 > 其他分享 >Codeforces Round #785 (Div. 2) A-C

Codeforces Round #785 (Div. 2) A-C

时间:2022-12-22 07:55:23浏览次数:43  
标签:785 字符 rep Codeforces long str Div dp define

Problem A

题意问给一个长度为2的小写字符串,字符串从ab开始,然后第一个位置和第二个位置上的字符不能相等,问按照这个方式排序,给出的字符串是第几个

然后这道题首先分情况讨论,首先题意可知每一个字符打头的有25个,然后如果当前第二个字符大于第一个字符,证明有skip 1,所以-1,否则 + 1

不难

#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (400000 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].nxt)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;

inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}


int n,m;
int a[limit];
int sum[limit];
void solve(){
    string str;
    cin>>str;
    n = str.size();
    str = " " + str;
    rep(i,1,n){
        a[i] = str[i] - 'a' + 1;
        sum[i] = sum[i - 1] + a[i];
//        cout<<a[i]<<" ";
    }
    if(n == 1){
        cout<<"Bob"<<" "<<str[1] - 'a' + 1<<endl;
        return;
    }else if(n & 1){
        int maxx = max(sum[n] - sum[1], sum[n - 1]);
        cout<<"Alice"<<" "<<abs((sum[n] - maxx) - maxx)<<endl;
    }else{
        cout<<"Alice"<<" "<<sum[n]<<endl;
    }

};
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
        solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
    return 0;
}
AC Code

Problem B

给一个字符串,然后要求任意一个子段内的最多出现的字符频率和最少出现的字符频率不能大于1,这个字符指的是在这个字符串出现过的

那么这个问题其实很好搞,观察样例,我们发现这个违反规则的样例一般是出现在两个相同字符打头的区间的,刚开始想的是找这样的区间

所以可以用一个数组维护当前index左边第一个和当前位置相同的位置,或者干脆把所有下标按照当前字符存一下,然后跑一下判断,看看所有字符是不是出现在了区间里面,如果不是,说明肯定不行,输出no

#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (400000 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].nxt)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;

inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}


int n,m;
int a[limit];
int sum[limit][30];
vector<int>g[limit];
void solve(){
    string str;
    cin>>str;
    n = str.size();

    auto s = set<char>{str.begin(), str.end()};
    str = " " + str;
    rep(i,0,26)g[i].clear();
    rep(i,1,n){

        rep(j,0,25){

            sum[i][j] = sum[i - 1][j] + (str[i] - 'a' == j);
        }
    }

    map<char, int>mp;
    rep(i,1,n){
        mp[str[i]]++;
        g[str[i] - 'a'].push_back(i);
    }
    rep(i,0,25){

        if(g[i].size() <= 1)continue;
        rep(j, 1, g[i].size() - 1){
            int l = g[i][j - 1];
            int r = g[i][j];
            set<char>st;
            rep(h,l + 1, r){
                st.insert(str[h]);
            }
            if(st.size() != mp.size()){
//                cout<<str<<" "<<l<<" "<<r<<endl;
                cout<<"No"<<endl;
                return;
            }

        }
    }
    cout<<"Yes"<<endl;

};
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
        solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
    return 0;
}
AC Code

 

Problem C

这个问题就是问一个数字只用自身为回文的数字表示,有多少种方式分解

这个问题,一眼想到了著名入门题“简单的整数划分问题”,然后跑了下前40000个数字,发现只有498个自身为回文的数字(暗藏玄只因),所以可以开下

处理手法类似,但是记得这个dp一定要预处理,或者记忆化,每次跑dp的话会tle

#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (400000 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].nxt)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;

inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}


int n,m;
int a[limit];
bool isPalindrome(int x){
    int tmp = x;
    int sum = 0;
    while(tmp){
        sum = sum * 10 + tmp % 10;
        tmp /= 10;
    }
    return sum == x;
}
int tot;
ll dp[40005][501];
const ll mod = 1e9 + 7;
void init(){
    n = 4e4;
    tot = 0;
    rep(i,1,n){
        if(isPalindrome(i)){
            a[++tot] = i;
        }
    }
    rep(i,1,tot){
        dp[0][i] = 1;
    }
    rep(i,1,n){
        rep(j,1,tot){
            if(i - a[j] >= 0){
                dp[i][j] = (dp[i][j - 1] + dp[i - a[j]][j]) % mod;
            }else{
                dp[i][j] = dp[i][j - 1] % mod;
            }
        }
    }
}

void solve(){
    cin>>n;

    cout<<(1ll * dp[n][tot]) % mod<<endl;
};
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    init();
    int kase;
    cin>>kase;
    while (kase--)
        solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
    return 0;
}
AC Code

D有点头绪,但是因为前两题莫名wa2所以没时间做了,回去研究下

标签:785,字符,rep,Codeforces,long,str,Div,dp,define
From: https://www.cnblogs.com/tiany7/p/16997543.html

相关文章

  • Codeforces Round #840 (Div. 2)
    A题意:给定n个整数,可以交换任意两个数二进制上的某一位。求任意操作次数后数组中最大值与最小值的最大差。核心思路:这个思路还是很显然的大胆的猜结论,贪心的考虑每一个......
  • Ceiling Division in Python
    ToperformceilingdivisioninPython,youcandefineyourownfunctionandutilizethefloordivisionoperator //.>>>defceiling_division(x,y):...ret......
  • 785--D
    D.LostArithmeticProgression思路0和-1的情况都挺好分析的。然后A和B的公差必须是lcm==C的公差,最后答案的计数,是先把中间填满了,然后看C的左右两边最大可以拓展多少代......
  • UVA 725 Division
    原题Vjudge题目大意给你个\(N\)判断有没有两个整数满足\(\frac{A}{B}=N\),并且\(A和B\)的各位数字刚好构成\(0\sim9\)的一个排列解题思路这题乍一看挺难的,但是范围很......
  • [Codeforces Round #836 (Div. 2)C
    C这一次呢,题目要求让a[1]=x,a[n]=1,我们需要找到一个最小的序列使得每一个a[i]都可以被它的下标整除,没找到就是输出-1我第一次是想着先让a[1]=x,a[n]=1,然后在中间一个一......
  • 论文解读:Sequence to Sequence Mixture Model for Diverse Machine Translation
    论文解读:SequencetoSequenceMixtureModelforDiverseMachineTranslation  机器翻译是自然语言处理中比较热门的研究任务,在深度学习背景下,通过神经网络搭建的机器翻......
  • Your branch and ‘origin/master‘ have diverged
    开发中遇到拉取最新远程仓库代码的时候就出现下面错误,说我的本地和远程不一致。1Yourbranchand'origin/master'havediverged,2andhave1and4differentcommi......
  • [css] before和:after实现div下方的小三角
    <divclass="pop-box">aaa</div>.pop-box{position:absolute;width:250px;height:260px;background-color:#fff;border:1pxsolid#ccc;bord......
  • Codeforces Global Round 20--F1
    F1.ArrayShuffling结论交换的次数=点数-循环圈的数量所以只需要构造尽量多的循环圈,圈上的各个元素都是不相同的就可以了。代码#include<bits/stdc++.h>usingname......
  • Codeforces Round #840 (Div. 2) and Enigma 2022 - Cybros LNMIIT(持续更新)
    Preface有史以来打的最爆炸的一场,只写了AB一个原因是最近由于得了新冠导致疏于锻炼,脑子和手都有点不在状态另一个原因就是没去想C去开一眼感觉很naive的D(事实确实很naiv......