【Codeforces Round #550 (Div. 3)】 A B C D E F G

71 篇文章 0 订阅
6 篇文章 0 订阅

A

A. Diverse Strings

题意 给你n个字符串 问你所有字符串排序以后是否满足相邻 a和z不相邻

那么暴力排序 暴力check即可

 

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
char str[105];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    scanf("%d",&n);
    while(n--)
    {
        bool flag = true;
        scanf("%s",str+1);
        int len = strlen(str+1);
        sort(str+1,str+1+len);
        for(int i = 2;i<=len;++i)
            if(str[i]!=str[i-1]+1) flag = false;
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

B

B. Parity Alternated Deletions

题意 给你包含n个元素的数组 你能先删一个数 这个数如果是偶 你下个就要删奇 如果这个数是奇数 你下个就要删偶数

问你能删完所有的满足剩下的最小值是多少

做法 我们只要按题意模拟删两个元素的过程 用两个优先队列贪心的去删即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 2025;
int arr[MAX_N];
priority_queue<int > q,Q,q_,Q_;
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,sum = 0,sum_=0,ans = 0x3f3f3f3f;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i)
    {
        scanf("%d",&arr[i]);
        sum += arr[i],sum_+=arr[i];
        if(arr[i]&1) q.push(arr[i]),Q.push(arr[i]);
        else q_.push(arr[i]),Q_.push(arr[i]);
    }
    while(!q.empty())
    {
        int now = q.top();
        q.pop();
        sum-=now;
        if(q_.empty()) break;
        now = q_.top();
        q_.pop();
        sum-=now;
    }
    while(!Q_.empty())
    {
        int now = Q_.top();
        Q_.pop();
        sum_-=now;
        if(Q.empty()) break;
        now = Q.top();
        Q.pop();
        sum_-=now;
    }
    printf("%d\n",min(sum,sum_));
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

C

C. Two Shuffled Sequences

题意 给你包含n个元素的数组 其中有一段严格上升的序列 有一段严格下降的序列 混在一起 

让你输出

做法 我们把所有元素出现次数统计一下 对原数组排个序

一旦有一个元素出现次数大于等于3次那么就直接是false因为两边都塞不下

然后出现次数为一次放上升 出现次数为两次的放下降即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 2e5+5;
int arr[MAX_N],cnt[MAX_N];
vector<int > ans,ans_;
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    bool flag = true;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
    sort(arr+1,arr+1+n);
    for(int i = 1;i<=n;++i)
    {
        cnt[arr[i]]++;
        if(cnt[arr[i]]>1) ans.push_back(arr[i]);
        else ans_.push_back(arr[i]);
        if(cnt[arr[i]]>2)
        {
            flag = false;
            break;
        }
    }
    if(!flag)
    {
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    printf("%d\n",ans.size());
    int sz =ans.size();
    for(int i = 0;i<sz;i++)
        i==sz-1?printf("%d",ans[i]):printf("%d ",ans[i]);
    printf("\n");
    int sz_=ans_.size();
    printf("%d\n",sz_);
    for(int i = sz_-1;i>=0;i--)
        i==0?printf("%d",ans_[i]):printf("%d ",ans_[i]);
    printf("\n");
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

D

D. Equalize Them All

题意 给你包含n个元素的数组 你可以执行两个操作 都是可以把一个元素变成和选择的相邻的元素一样的大小

问你最小操作次数 我们只要统计哪个元素出现的最多 然后把这个元素下标存进一个vector 每次向左向右扫 把扫过的打个标记即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 2e5+5;
int arr[MAX_N],cnt[MAX_N];
bool flag[MAX_N];
vector<int > ans;
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,maxx = -1,xb,ck;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]),cnt[arr[i]]++;
    if(cnt[arr[1]]==n)
    {
        printf("0\n");
        return 0;
    }
    for(int i = 1;i<=n;i++)
    {
        if(cnt[arr[i]]>maxx)
        {
            maxx = cnt[arr[i]];
            ck = arr[i];
        }
    }
    for(int i = 1;i<=n;++i)
        if(arr[i]==ck) ans.push_back(i);
    printf("%d\n",n - maxx);
    int sz = ans.size();
    for(int i = 0;i<sz;++i)
    {
        xb = ans[i]-1;
        flag[ans[i]] = true;
        while(xb>=1)
        {
            if(flag[xb]) break;
            if(arr[xb]>ck) printf("2 %d %d\n",xb,xb+1);
            else if(arr[xb]<ck) printf("1 %d %d\n",xb,xb+1);
            flag[xb] = true;
            xb--;
        }
        xb = ans[i]+1;
        while(xb<=n)
        {
            if(flag[xb]) break;
            if(arr[xb]>ck) printf("2 %d %d\n",xb,xb-1);
            else if(arr[xb]<ck) printf("1 %d %d\n",xb,xb-1);
            flag[xb] = true;
            xb++;
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

E

E. Median String

题意 给你两个字符串  保证字符串直接差奇数个 问你排中间的字符串是多少

做法 从一开始往后扫 如果后面的第二个字符串比第一个字符串小 那么当前就要借一个1 给后面 然后当前取两个tmp中位数

如果中位数%2非0 那么还要借一个1给后面 到当前这位 前面有几个1 就要加上几个1*26 然后再继续操作

但是最后一位可能会超过26 所以我们写个check函数解决进位即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 2e5+5;
char str[MAX_N],str_[MAX_N],str__[MAX_N],ans[MAX_N];
void check(int i)
{
    if(ans[i]=='z')
    {
        ans[i] = 'a';
        check(i-1);
    }
    else ans[i]+=1;
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,xb = 1;
    scanf("%d",&n);
    scanf("%s",str+1);
    scanf("%s",str_+1);
    str[n+1] = 'z';
    str_[n+1] = 'z';
    for(int i = 1;i<=n;++i)
        if(str[i]==str_[i]) printf("%c",str[i]),xb++;
        else break;
    bool flag = false,flag_ = false;
    for(int i = xb;i<=n;++i)
    {
        int tmp = str_[i]-'a'+1,tmp_=str[i]-'a'+1;
        if(flag_)
        {
            tmp+=26;
            flag_ = false;
        }
        if(flag)
        {
            tmp+=26;
            flag = false;
        }
        if(str_[i+1]<str[i+1])
        {
            tmp--;
            flag = true;
        }
        if((tmp+tmp_)%2!=0) flag_ = true;
        ans[i] = (tmp+tmp_)/2+'a'-1;
        if(((tmp+tmp_)/2-1)>=26)
        {
            ans[i]=(tmp+tmp_)/2+'a'-1-26;
            check(i-1);
        }
    }
    for(int i = xb;i<=n;++i) printf("%c",ans[i]);
    printf("\n");
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

F

F. Graph Without Long Directed Paths

题意 给你n个点 m条边 保证联通 问你能否构造一张有向图(不一定联通)使得路径长度不超过2 

我们很容易想到从一个点开始染色 和他相连的点染相反颜色 然后再继续递归下去染色

但是要判断一下 这个点染色和下个点染色是否相反 如果那个点已经有颜色和自己是一样的 那么肯定是NO

例如这组数据

3 3
1 2
2 3
3 1

实际上是不能染色的 因为一定会有一条2的路径 所以你把相同颜色出现的时候全局flag为false 输出NO即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 2e5+5;
int col[MAX_N],a[MAX_N],b[MAX_N];
vector<int> G[MAX_N];
bool flag;
void dfs(int x,int fa,int v)
{
    int sz = G[x].size();
    col[x] = v;
    for(int i = 0;i<sz;++i)
    {
        int to = G[x][i];
        if(to==fa) continue;
        if(col[to]==-1) dfs(to,x,1-v);
        else if(col[to] == col[x]) flag = false;
    }
    return ;
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m;
    flag = true;
    scanf("%d%d",&n,&m);
    memset(col,-1,sizeof(col));
    for(int i = 1;i<=m;++i)
    {
        scanf("%d%d",&a[i],&b[i]);
        G[a[i]].push_back(b[i]);
        G[b[i]].push_back(a[i]);
    }
    dfs(1,-1,0);
    if(!flag)
    {
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    for(int i = 1;i<=m;++i)
    {
        if(col[a[i]]==1) printf("1");
        else printf("0");
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


G

G. Two Merged Sequences

题意 和C题差不多 但是是在一个严格上升的序列中插入严格递减的序列

做法 我们按照贪心 定义一个inc 代表当前上升序列最大值 Dec 当前下降序列最小值 

如果你这个值可以既放上升序列 下降序列 那么我们特判一下下一个是否比当前这个大 如果大那么这个可以当上升

否则我就让他当下降 例如这组数据

6
1 2 50 3 51 5

当到50的时候 如果3比50大那么让50当下降序列 否则让50当上升序列

贪心的去完成即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 2e5+5;
int arr[MAX_N],inc,Dec;
bool add[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    scanf("%d",&n);
    memset(add,false,sizeof(add));
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
    inc = _inf,Dec = inf;
    bool flag = true;
    for(int i = 1;i<=n;++i)
    {
        if(arr[i]<=inc&&arr[i]>=Dec)
        {
            flag = false;
            break;
        }
        else if(arr[i]>inc&&arr[i]<Dec)
        {
            if(i==n) add[i] = true;
            else if(arr[i+1]>arr[i]) add[i] = true,inc = arr[i];
            else Dec = arr[i];
        }
        else if(arr[i]>inc) add[i] = true,inc = arr[i];
        else Dec = arr[i];
    }
    if(!flag) return 0*puts("NO");
    printf("YES\n");
    for(int i = 1;i<=n;++i) add[i]==true?printf("0 "):printf("1 ");
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值