Codeforces Round #712 (Div. 2) (A~C,E)

比赛链接:这里
A:
题意:让我们在原字符串的基础上再插入一个字母a,并且要求插入后的字符串不能是回文串。题解:我们可以这样想,如果它原先是回文串,那么我们插在第一个或者最后一个个都会使它变为不是回文串。如果它原先不是回文串,我们有可能会插入一个a后使它变为回文串,但只有唯一的位置,所以我们只需要判断在第一个和最后一个插入的情况即可。

#include<bits/stdc++.h>
#define ll long long
#define pr pair<ll,ll>
#define ios ios::sync_with_stdio(false)
#define CRL(a) memset(a,0,sizeof a)

using namespace std;

const int maxn = 1e5 + 5;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		string s;
		cin >> s;
		string s1, s2;
		s1 = 'a' + s;
		s2 = s1;
		reverse(s1.begin(), s1.end());
		if(s1!=s2) 
		{
			cout << "YES" << endl;
			cout << s2 << endl;
			continue;
		}
		s1 = s + 'a';
		s2 = s1;
		reverse(s1.begin(), s1.end());
		if(s1!=s2) 
		{
			cout << "YES" << endl;
			cout << s2 << endl;
			continue;
		}
		cout<<"NO"<<endl;
	}
}

B:
题意:给我们两个字符串a和b,里面只包含0和1,对于字符串a,我们可以选它的一个前缀,要求这个前缀里的0和1数量相等,然后使这个前缀中的0变为1,1变为0,问我们a字符串能否变为b字符串。
题解:我们需要注意的是,每次0变1,1变0以后,它的前缀的0和1的数量还是想等的,所以我们可以用一个前缀和来存前缀0和1的数量,然后用cnt来判断这个位置交换了几次,然后倒着扫描一边即可。

#include<bits/stdc++.h>
#define ll long long
#define pr pair<ll,ll>
#define ios ios::sync_with_stdio(false)
#define CRL(a) memset(a,0,sizeof a)

using namespace std;

const int maxn = 3e5 + 5;
int x[maxn];
int y[maxn];
int a[maxn];
int b[maxn];
int main()
{
    ios;
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        string s, ss;
        cin >> s >> ss;
        for (int i = 1;i<=n;i++)
        {
            a[i] = s[i - 1] - '0';
        }
        for (int i = 1;i<=n;i++)
        {
            b[i] = ss[i - 1] - '0';
        }
        int cnt = 0;
        int flag = 1;
        x[0] = 0;
        y[0] = 0;
        for (int i = 1; i <= n;i++)
        {
            if(a[i]==0)
            {
                x[i] = x[i-1] + 1;
            }
            else
                x[i] = x[i-1];
            if(a[i]==1)
            {
                y[i] = y[i-1] + 1;
            }
            else
                y[i] = y[i - 1];
        }
        for (int i = n; i >= 1;i--)
        {
            int c = a[i] ^ (cnt % 2);
            int d = b[i];
            if(c!=d&&x[i]==y[i])
            {
                cnt++;
            }
            else if(c!=d&&x[i]!=y[i])
            {
               // cout << i << endl;
                flag = 0;
                break;
            }
        }
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
}

C:
题意:给我们一个只包含0和1的字符串,问我们能否构造两个合法的括号序列a和b,满足这样的性质。
若s[i] = 1 a[i] = b[i]
若s[i] = 0 a[i] != b[i]
题解:很明显,字符串的第一个和最后一个字符一定要是1,否则无论如何都不会构造出这个括号序列,而且里面的0的数量必须得是偶数,否则没有另一个交换的和它配对,弄明白这规则后,构造就很简单了,1我们只需要对半分即可,我们可以使第偶数个0为(,奇数为),然后下面反过来,反过来后第一个0和前面的一个1配对,最后一个0和后面的一个1配对,这样得到的也是合法的。

#include<bits/stdc++.h>
#define ll long long
#define pr pair<ll,ll>
#define ios ios::sync_with_stdio(false)
#define CRL(a) memset(a,0,sizeof a)

using namespace std;

const int maxn = 2e5 + 5;
char s[maxn];

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        cin >> s + 1;
        if(s[1]!='1'||s[n]!='1')
        {
            cout << "NO" << endl;
            continue;
        }
        int ans = 0, ans1 = 0;
        for (int i = 1; i <= n;i++)
        {
            if(s[i]=='0')
                ans++;
            else
                ans1++;
        }
        if(ans%2||ans1%2)
        {
            cout << "NO" << endl;
            continue;
        }
        cout << "YES" << endl;
        int a = 0, b = 0;
        for (int i = 1; i <= n;i++)
        {
            if(s[i]=='0')
            {
                a++;
                if(a%2==1)
                    cout << "(";
                else
                    cout << ")";
            }
            else 
            {
                b++;
                if(b<=ans1/2)
                    cout << "(";
                else
                    cout << ")";
            }
        }
        cout << endl;
        a = 0, b = 0;
        for (int i = 1; i <= n;i++)
        {
            if(s[i]=='0')
            {
                a++;
                if(a%2==1)
                    cout << ")";
                else
                    cout << "(";
            }
            else 
            {
                b++;
                if(b<=ans1/2)
                    cout << "(";
                else
                    cout << ")";
            }
        }
        cout << endl;
    }
}

E:
题意:给你n个城市,你从i到j的花费为max(ci, aj - ai),让我们求最小的花费。
题解:这个题目与从哪儿开始出发无关,最后我们都是要在每个城市走一次,我们可得,最终的花费一定要比所有的ci加起来要大,我们可以先把这个ci存起来,这样就可以转化成max(0, aj - ai - ci)。
然后我们可以得到从高的城市(即ai大的城市)到比它小的城市的花费一定为ci,所以这部分我们就可以先忽略,先找到一条到最高的路径,然后再从最高的路径下来即可,因此我们只需要求max(0, aj - ai - ci)所有的和即可。我们可以先按照ai来进行排序,从小往大走,让一个max1来保存ai + ci的最大值,如果我们当前走的点的aj + cj的值要比max1要大,我们就走到这个点,然后加长差值即可。走到最高点以后再下来即可。

#include<bits/stdc++.h>
#define ll long long
#define pr pair<ll,ll>
#define ios ios::sync_with_stdio(false)
#define CRL(a) memset(a,0,sizeof a)

using namespace std;

const int maxn = 1e5 + 5;
vector<pr> q;
int main()
{
    int n;
    cin >> n;
    ll sum = 0;
    for (int i = 1; i <= n;i++)
    {
        int a, b;
        cin >> a >> b;
        q.push_back({a, b});
        sum += b;
    }
    sort(q.begin(), q.end());
    ll max1 = q[0].first+q[0].second;
    for (int i = 1; i < n;i++)
    {
        sum += max(0LL, q[i].first - max1);
        max1 = max(max1, q[i].first + q[i].second);
    }
    cout << sum;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值