A 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;
}
B 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;
}
C LuoTianyi and the Show
解题思路
1.只用-1
2.只用-2
3.从中间考虑
对于第三种情况,用哈希把的情况存起来,用前后缀数组表示左和右的空位,再判断即可
当然也可以不用求前后缀,但是得排序
参考代码
#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;
}