2022-03-16每日刷题打卡
代码源——每日一题
删删 - 题目 - Daimayuan Online Judge
给定一个字符串,你可以删除多个(可以是 0) 相同 的字符,这样操作之后,你能否得到一个回文串?如果能,求最小化删除的个数。
输入格式
多组数据。
每一组数据包含两行,分别为字符串的长度 N,以及一个仅由小写字母组成的字符串 SS。
输出格式
对于每一组数据,输出一行。
如果不可能得到一个回文串,输出 −1。反之则输出最小操作次数。
样例输入
4
8
bilibili
3
qwq
9
daimayuan
7
xcpcxpc
样例输出
1
0
-1
2
解释:
在第一个例子中,删除开头的 b
得到 ilibili
。
第二个例子中,qwq
本身已回文,不需要操作。
第三个例子中,可以看到 daimayuan
不能靠仅删除一种字符得到一个回文串。
数据规模
- 1≤N≤10^5, 但保证 ∑N≤2×10^5
太菜了,自己只能想到个暴力解法。枚举26个字符,假设这个字符就是这个字符串要删除的字符,然后双指针跑字符串,当两边不一样时就判断两边有没有那边是我们当前枚举的字符,如果有就跳过他,操作数++然后继续双指针跑字符串,如果两边都没有我们当前枚举的字符,说明这个字符不能把字符串变成回文,我们去枚举下一个字符,如果有字符可以把字符串变成回文,那就把操作数记录下来,并维护最小值。最后如果所有字符都不能把字符串变成回文那就输出-1,反之输出最小的操作数。
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
typedef long long ll;
typedef pair<int, int>PII;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
char c = '#';
int n,res=1e9;
bool flag = true;
string str;
cin >> n >> str;
for (char i = 'a'; i <= 'z'; i++)
{
bool flag = true;
int l = 0, r = n - 1,ans=0;
while (l < r)
{
if (str[l] == str[r])l++, r--;
else
{
if (str[l] == i)l++, ans++;
else if (str[r] == i)r--, ans++;
else
{
flag = false;
break;
}
}
}
if (flag)res = min(res, ans);
}
if (res==1e9)cout << -1 << '\n';
else cout << res << '\n';
}
return 0;
}
CodeForces
Problem - K - Codeforces
Megumi is a girl who likes to eat.
Today there will be a party at Aki’s home. There are N cakes for Megumi of different weight on the table. She will choose one cake and eat no more than half of it every time. But out of conscience, she won’t eat those cakes whose weight is smaller than K.
However, she knows a magic to merge two cakes. That means she can put two cakes together and combine them into a larger cake whose weight is equal to the sum of weight of previous cakes. She can do this operation every time.
Now Aki wants to know the maximum total weight of cakes Megumi can eat. Please note that she can eat as many times as she can, and each time the weight she eat is a positive integer no more than the half of the weight of cake.
Input
The first line contains two integers N,K (1≤N≤200000; 2≤K≤10^6).
The second line contains N integers a1,a2,⋯,an (1≤ai≤10^6) denoting the weight of each cake.
Output
Output one integer denoting the maximum total weight of cakes she can eat.
Example
input
5 10
9 8 7 10 10
output
39
题目意思是说有n个蛋糕,你每次最多可以选一个蛋糕吃掉最多一半,你不会吃重量小于k的蛋糕,但你可以把多个蛋糕合成一个,合成的重量是蛋糕重量的和,问你最多可以吃掉多少重量的蛋糕。
贪心,题目说了可以选一个蛋糕吃掉最多一半,但没说一定要吃一半,你可以把所有蛋糕合成1个后1点点吃,吃到重量等于k后直接吃掉一半,这样你能吃掉的重量最多就是所有蛋糕的重量和减去k的一半。
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
typedef long long ll;
typedef pair<ll, ll>PII;
const int N = 1010, MOD = 1000003;
ll f[N][N], s[N][N],v[N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> v[i];
}
for (int i = 1; i <= n; i++)
{
s[i][i] = 1;
s[i][i-1] = 1;
for (int j = i; j <= n; j++)
{
s[i][j] = (v[j] * s[i][j - 1]) % MOD;
}
}
for (int len = 2; len <= n; len++)
{
for (int i = 1; i + len - 1 <= n; i++)
{
int j = i + len - 1;
for (int k = i; k < j; k++)
{
f[i][j] = max(f[i][j], f[i][k] + f[k + 1][j] + (s[i][k]-s[k+1][j])* (s[i][k] - s[k + 1][j]));
}
}
}
cout << f[1][n] << endl;
return 0;
}
Problem - J - Codeforces
Aki is the prime minister in JOJO’s Factory.
There are two types of machines in the factory, which are called A-machine and B-machine respectively. Both the number of two types of machines are N. One A-machine should work with exactly one B-machine and one B-machine should work with exactly one A-machine.
Unfortunately, some pairs of machines can not work together due to some unknown reason. A pair (i,j) means ii-th A-machine can not work with jj-th B-machine.
In order to improve the efficiency, you, his staff, should find a plan that the most A-machines can run normally.
Luckily, Aki is a talented prime minister, so the number of un-working pairs is no more than 2N−3.
Input
The first line contains two integers N,M (5≤N≤5×10^5; 0≤M≤2N−3) denoting the count of machines and the count of pairs of machines that don’t work together.
Then next MM lines follows, each containing two integers i,j (1≤i,j≤N) denoting the un-working pairs. It is guaranteed that all pairs of (i,j) are different.
Output
Output one line containing the maximal number of normally running A-machines.
Example
input
4 5
1 2
3 1
1 3
1 4
1 1
output
3
这题意思是说有个工厂有AB两种型号机器各有n台,而A机器必须要和一台B机器一起才能运作起来,但是有些A机器脾气不好,不想和某些B机器一起整,题目给出了某编号A机器不想和某编号B机器一起工作,现在问你最多同时能有几个A机器开工。
思维题,统计一下某型号A有多少个B机器是它讨厌的,如果讨厌次数大于n,就说明A无法开工了(B被你讨厌完了你就单着吧你),再统计某B机器被A讨厌的个数,如果大于B说明这个B无法和A配对了(那么多A讨厌你你就单着吧你(是不是有点双标了)),然后看看两边各有多少个还可能开工的,取最小值输出。
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
typedef long long ll;
typedef pair<int, int>PII;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int>a(n + 1), b(n + 1);
int a_num = n, b_num = n;
for (int i = 0; i < m; i++)
{
int l, r;
cin >> l >> r;
a[l]++;
b[r]++;
if (a[l] >= n)a_num--;
if (b[r] >= n)b_num--;
}
cout << min(a_num, b_num) << endl;
return 0;
}