其他分享
首页 > 其他分享> > 打卡第四天 Codeforces Round #604 (Div. 2)(掉分了,d题差一点)

打卡第四天 Codeforces Round #604 (Div. 2)(掉分了,d题差一点)

作者:互联网

A:题意:给一串字符串以abc和?组成,要求将?替换成abc,使得这个字符串相邻两个字母不等。
暴力模拟。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
    char ch = getchar(); ll x = 0, f = 1;
    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
string s;
int t;
int main()
{
    t = read();
    while (t--)
    {
        cin >> s;
        int n = s.size();
        bool flag = 1;
        up(i, 1, n)
        {
            if (s[i] == s[i - 1]&&s[i]!='?')flag = 0;
        }
        if (!flag) { printf("%d\n", -1); }
        else
        {
            if (n == 1)
            {
                if (s[0] == '?')s[0] = 'a';
            }
            else
            {
                up(i, 0, n)
                {
                    if (i == 0)
                    {
                        if (s[i] == '?')
                        {
                            if (s[i + 1] == 'b')s[i] = 'a';
                            if (s[i + 1] == 'c')s[i] = 'b';
                            if (s[i + 1] == 'a')s[i] = 'c';
                            if (s[i + 1] == '?')s[i] = 'a';
                        }
                    }
                    else
                    {
                        if (s[i - 1] != '?'&&s[i] == '?')
                        {
                            if (i + 1 < n&&s[i + 1] == '?')
                            {
                                if (s[i - 1] == 'b')s[i] = 'a';
                                if (s[i - 1] == 'c')s[i] = 'b';
                                if (s[i - 1] == 'a')s[i] = 'c';
                            }
                            else if (i + 1 < n&&s[i + 1] != '?')
                            {
                                int a = 0, b = 0, c = 0;
                                if (s[i - 1] == 'a')a++;
                                if (s[i - 1] == 'b')b++;
                                if (s[i - 1] == 'c')c++;
                                if (s[i + 1] == 'a')a++;
                                if (s[i + 1] == 'b')b++;
                                if (s[i + 1] == 'c')c++;
                                if (a == 0)s[i] = 'a';
                                if (b == 0)s[i] = 'b';
                                if (c == 0)s[i] = 'c';
                            }
                            else if (i == n-1)
                            {
                                if (s[i - 1] == 'b')s[i] = 'a';
                                if (s[i - 1] == 'c')s[i] = 'b';
                                if (s[i - 1] == 'a')s[i] = 'c';
                            }
                        }
                    }
                }
            }
            cout << s << endl;
        }
    }
    return 0;
}

B;题意:给一串permutation,遍历长度1-n,寻找每一个i,能否组成permutation。
思维,记录最左侧和最右侧,相减=len,ans++,因为这个时候1-i的所有数字必定全部在这个区间内。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
    char ch = getchar(); ll x = 0, f = 1;
    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 2e5 + 10;
int T, n;
int a[N];
int pos[N];
int main()
{
    T = read();
    while (T--)
    {
        n = read();
        upd(i, 1, n)a[i] = read(),pos[a[i]]=i;
        int lf = n + 1, rt = 0;
        string ans = "";
        upd(i, 1, n)
        {
            lf = min(lf, pos[i]);
            rt = max(rt, pos[i]);
            if (rt - lf + 1 == i)ans+='1';
            else ans += '0';
        }
        cout << ans << endl;
    }
}

C:我又写了个复杂模拟。。。
题意:太复杂了。。
我的思路是,贪心。最后一行取拿不到牌子的,第一行取金牌,这样剩下的凑银牌和铜牌即可。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
    char ch = getchar(); ll x = 0, f = 1;
    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
int n;
int t;
const int N = 4e5 + 10;
int p[N];
int st[N];
int cnt[N];
int main()
{
    t = read();
    while (t--)
    {
        n = read();
        int last = -1;
        int top = 0;
        upd(i, 1, n)
        {
            p[i] = read();
            if (top == 0)
            {
                top++;
                st[top] = p[i];
                cnt[top]=1;
            }
            else {
                if (st[top] != p[i])
                {
                    st[++top] = p[i];
                    cnt[top] = 1;
                }
                else
                {
                    cnt[top]++;
                }
            }
        }
        int half = n / 2;
        if (top < 4)
        {
            printf("%d %d %d\n", 0, 0, 0);
        }
        else
        {
            int left = n - cnt[top];
            
                int one = cnt[1];
                int pos = top;
                for (int i = top - 1; left > half&&i; i--)
                {
                    left -= cnt[i];
                    pos = i;
                }
                int ans = left;
                left -= one;
                int twsum = 0;
                int pos2 = pos;
                for (int i = pos-1; twsum <= one&&i; i--)
                {
                    twsum += cnt[i];
                    pos2 = i;
                }
                left -= twsum;
                if (left > one)
                {
                    printf("%d %d %d\n", one, left, twsum);
                }
                else
                {
                    printf("%d %d %d\n", 0, 0, 0);
                }
        }
    }
    return 0;
}

标签:604,ch,const,int,Codeforces,long,打卡,include,define
来源: https://www.cnblogs.com/LORDXX/p/12004965.html