codeforces round 906 (div. 2) A~D

A.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    set<int> st;
    map<int, int> mp;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        st.insert(a[i]);
        mp[a[i]]++;
    }
    // for (auto it : st)
    // {
    //     cout << it << " ";
    // }
    if (n == 2)
    {
        cout << "Yes" << endl;
        return;
    }
    if (st.size() > 2)
    {
        cout << "No" << endl;
    }
    else if (st.size() == 1)
    {
        cout << "Yes" << endl;
    }
    else
    {
        int p = *st.begin();
        int q = *st.rbegin();
        if (n % 2 == 0)
        {
            if (mp[p] == mp[q])
            {
                cout << "Yes" << endl;
            }
            else
            {
                cout << "No" << endl;
            }
        }
        else
        {
            if (abs(mp[p] - mp[q]) == 1)
            {
                cout << "Yes" << endl;
            }
            else
            {
                cout << "No" << endl;
            }
        }
    }
    st.clear();
    mp.clear();
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

B.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
void solve()
{
    int n, m;
    cin >> n >> m;
    string a, b;
    cin >> a >> b;
    bool flag1 = true;
    int ans1 = 0, ans0 = 0;
    for (int i = 0; i < n - 1; i++)
    {
        if (a[i] == a[i + 1])
        {
            if (a[i] == '1')
            {
                ans1 = 1;
            }
            else
            {
                ans0 = 1;
            }
            flag1 = false;
        }
    }
    if (flag1)
    {
        cout << "Yes" << endl;
        return;
    }
    bool flag2 = true;
    for (int i = 0; i < m - 1; i++)
    {
        if (b[i] == b[i + 1])
        {
            flag2 = false;
            break;
        }
    }
    // cout << ans0 << " " << ans1 << " ";
    if (!flag2 || b[0] != b[m - 1])
    {
        cout << "No" << endl;
        return;
    }
    int flag = b[0] - '0'; // 可以调节的
    if (ans1 && ans0)
    {
        cout << "No" << endl;
        return;
    }
    else
    {
        if (ans1)
        {
            if (!flag)
            {
                cout << "Yes" << endl;
            }
            else
            {
                cout << "No" << endl;
            }
        }
        else
        {
            if (flag)
            {
                cout << "Yes" << endl;
            }
            else
            {
                cout << "No" << endl;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

C.

赛时直接暴力了,再调试一下双端队列试试

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
 
ll mgcd(ll a, ll b) {
    ll r;
    while (b > 0) {
        r = a % b;
        a = b;
        b = r;
    }
 
    return a;
}
 
ll mpow(ll a, ll b, ll mod) {
    ll ans = 1;
    a %= mod;
 
    while (b) {
        if (b & 1)
            ans = ans * a % mod;
 
        a = a * a % mod;
        b >>= 1;
    }
	ans%=mod;
    return ans;
}
vector<ll> v;
unordered_map<ll,ll> mp; 
priority_queue< ll, vector<ll> ,greater<ll> > q;
bool check(string s){
	ll l=s.length();
	for(int i=0;i<=l-i-1;i++){
		if(s[i]==s[l-i-1]) return false;
	}
	return true;
}
 
void solve() {
	ll l,c1=0,c0=0;
	v.clear();
	cin>>l;
	string s1;
	cin>>s1;
	for(int i=0;i<l;i++){
		if(s1[i]=='0') c0++;
		else c1++;
	}
	if(check(s1)){
		cout<<"0\n\n";
		return;
	}
	if(c1!=c0){
		cout<<"-1\n";
		return;
	}
	ll cnt=300;
	while(cnt){
		string s="";
		cnt--;
		ll l0=s1.length();
		int i;
		for(i=0;i<=l0-i-1;i++){
			if(s1[i]==s1[l0-i-1]){
				if(s1[i]=='1'){
					for(int j=0;j<i;j++){
						s+=s1[j];
					}
					s+="01";
					for(int j=i;j<l0;j++){
						s+=s1[j];
					}
					v.push_back(i);
				}else{
					for(int j=0;j<=l0-i-1;j++){
						s+=s1[j];
					}
					s+="01";
					for(int j=l0-i;j<l0;j++){
						s+=s1[j];
					}
					v.push_back(l0-i);
				}
				s1=s;
				break;
			}
		}
		if(i>(l0-i-1)){
			cout<<(ll)v.size()<<'\n';
			for(int i=0;i<v.size();i++){
				cout<<v[i]<<' ';
			}
			cout<<'\n';
			return;
		}
	}
	cout<<"-1\n";
}
int main() {
    std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T = 1;
    cin >> T;
	while (T--) {
        solve();
    }
    return 0;
}

D.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
const int N = 2e5 + 5;
ll a[N];
set<pair<ll, int>> S;
void solve()
{
    int n, cnt = 0;
    ll c, s = 0;
    cin >> n >> c;
    S.clear();
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (i > 1)
        {
            S.insert(make_pair(c * i - a[i], i));
        }
    }
    s += a[1];
    while (!S.empty() && s >= S.begin( )->first)
    {
        s += a[S.begin()->second];
        S.erase(S.begin());
        cnt++;
    }
    if (cnt == n - 1)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值