2022.4.1
Codeforces Round #780 (Div. 3)
A. Vasya and Coins
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int a[N];
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
ll a, b;
cin >> a >> b;
b *= 2;
if(a==0)
cout << 1 << '\n';
else if(b==0)
{
cout<<a + 1<<'\n';
}
else cout << a + b+1<< '\n';
}
return 0;
}
B - Vlad and Candies
推导发现只有最后两个相差大于1时才不能构造
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
int a[N];
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for (int i = 1; i<=n;i++)
cin >> a[i];
sort(a + 1, a + 1 + n);
int f = 1;
if(n==1)
{
if(a[1]>1)
f = 0;
}
else if(abs(a[n]-a[n-1])>1)
{
f = 0;
}
if(f)
cout << "yes" << '\n';
else
cout << "no" << '\n';
}
return 0;
}
C. Get an Even String
贪心,我们的最终目的是得到一个even字符串,可以考虑在删除的过程中构造,每次只贪心一对字母,每次遍历记录当前字母的次数,一旦有一个字母出现过两次就把他加入到构造的字符串当中,同时vis需要清零,因为我们只考虑每次贪心当前的这一对字母。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
char s[N];
int vis[30];
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
cin >> s;
memset(vis, 0, sizeof vis);
int n = strlen(s),cnt=0;
for (int i = 0; i < n;i++)
{
vis[s[i] - 'a']++;
if(vis[s[i]-'a']==2)
{
cnt += 2;
memset(vis, 0, sizeof vis);
}
}
cout << n - cnt << '\n';
}
return 0;
}
D. Maximum Product Strikes Back
ai(-2,2),首先当ai=0的时候相乘是没有意义的,因为最后都是0,于是以0为分界线划分成了几个区间。每个区间存在两种情况,负数有奇数个和偶数个。遍历每个区间,当有偶数个的时候:只需要保存整个区间相乘的值,当有奇数个时:分两次遍历,第一次从左到右从第一个负数到倒数第二个负数区间内的相乘值,第二次从右到左的第一个和倒数第二个区间的相乘值。取两次的最大值。特别注意当只有一个负数时,此时为整个区间。所以需要提前设定l=n,r=0。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
int a[N];
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for (int i = 1; i <= n;i++)
cin >> a[i];
int ans = 0, ansl = n, ansr = 0;
for (int l = 1; l <= n;l++)
{
if(a[l]==0)
continue;
int r = l+1;
while(a[r]!=0&&r<=n)
r++;
r--;
int f=1,cnt = 0;
for (int j = l; j <= r;j++)
{
if(abs(a[j])>1)
cnt++;
if(a[j]<0)
f *= -1;
if(f==1&&cnt>ans)
{
ansl = l - 1, ansr = n - j;
ans = cnt;
}
}
f = 1, cnt = 0;
for (int j = r; j >= l;j--)
{
if(abs(a[j])>1)
cnt++;
if(a[j]<0)
f *= -1;
if(f==1&&cnt>ans)
{
ansl = j - 1, ansr = n - r;
ans = cnt;
}
}
l = r;
}
cout << ansl << ' ' << ansr << '\n';
}
return 0;
}