【Codeforces】Codeforces Round 872 (Div. 2) (ABC) (-38!)

LuoTianyi and the Palindrome String

解题思路

暴力打表找规律,发现当字符串中只由1种构成就为-1,其他为字符串长度-1

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
ll T;
string s, y;
int ans;
ll len;
unordered_map<char, bool>mp;
void solve() {
	ans = -1;
	mp.clear();
	cin >> s;
	for (int i = 0; i < s.size(); i++) mp[s[i]] = 1;
	if (mp.size() == 1) ans = -1;
	else ans = s.size() - 1;
	cout << ans << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

LuoTianyi and the Table

解题思路

第一点可以确定左上角要么是最大,要么是最小,为了使差值最大,那么次小或者次大就应该放在左上角的相邻格,两个相邻格中还得判断n和m的大小

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n, m;
ll ans1, ans2;
ll b[N];
void solve() {
	cin >> n >> m;
	for (int i = 1; i <= n * m; i++) cin >> b[i];
	sort(b + 1, b + n * m + 1);
	ll ma = b[n * m], mi1 = b[1], mi2 = b[2];
	ans1 = 0;
	ans1 += (ma - mi1) * (n - 1) * (m - 1);
	ans1 += (ma - max(mi1, mi2)) * (min(n, m) - 1);
	ans1 += (ma - min(mi1, mi2)) * (max(n, m) - 1);
	ll mi = b[1], ma1 = b[n * m], ma2 = b[m * n - 1];
	ans2 = 0;
	ans2 += (ma1 - mi) * (n - 1) * (m - 1);
	ans2 += (max(ma1, ma2) - mi) * (max(n, m) - 1);
	ans2 += (min(ma1, ma2) - mi) * (min(n, m) - 1);
	cout << max(ans1, ans2) << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

LuoTianyi and the Show

解题思路

1.只用-1

2.只用-2

3.从中间考虑

对于第三种情况,用哈希把x>0的情况存起来,用前后缀数组表示左和右的空位,再判断即可

当然也可以不用求前后缀,但是得排序

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n, m;
ll x;
ll ans;
ll res;
unordered_map<ll, int>mp;
struct node {
	ll x;
	ll l, r;
};
void solve() {
	ans = 0;
	mp.clear();
	ll cl = 0, cr = 0;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> x;
		if (x == -1) cl++;
		else if (x == -2) cr++;
		if (x > 0) mp[x]++;
	}
	x = mp.size();
	ll fl[m + 5] = {};
	ll fr[m + 5] = {};
	for (int i = 1; i <= m; i++) {
		fl[i] = fl[i - 1];
		if (!mp[i]) fl[i]++;
	}
	for (int i = m; i >= 1; i--) {
		fr[i] = fr[i + 1];
		if (!mp[i]) fr[i]++;
	}

	for (ll i = 1; i <= m; i++) {
		if (mp[i]) {
			res = x;
			res += min(fl[i], cl);
			res += min(fr[i], cr);
			ans = max(ans, res);
		}
	}

	res = 0;
	for (int i = 1; i <= m; i++) {
		if (mp[i]) res++;
		else {
			if (cr > 0) {
				cr--;
				res++;
			}
		}
	}
	ans = max(ans, res);

	res = 0;
	for (int i = m; i >= 1; i--) {
		if (mp[i]) res++;
		else {
			if (cl > 0) {
				cl--;
				res++;
			}
		}
	}
	ans = max(ans, res);

	cout << ans << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

用数组把位置存起来 

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n, m;
ll x;
ll ans;
ll res;
unordered_map<ll, int>mp;
vector<ll>v;
bool cmp(ll a, ll b) {
	return a < b;
}
void solve() {
	ans = 0;
	mp.clear();
	v.clear();
	ll cl = 0, cr = 0;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> x;
		if (x == -1) cl++;
		else if (x == -2) cr++;
		else mp[x]++;
	}
	x = mp.size();
	v.push_back(-1);
	for (auto i : mp) v.push_back(i.first);

	sort(v.begin(), v.end(), cmp);
	for (ll i = 1; i <= x; i++) {
		res = x + min(cl, v[i] - i) + min(cr, m - v[i]  - (x - i));
		ans = max(ans, res);
	}

	res = max(min(m, cl + x), min(m, cr + x));
	ans = max(ans, res);

	cout << ans << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值