Codeforces Round #828 (Div. 3) 题解前两题
1744A - Number Replacement
简单题,属于签到范畴,题意就是看看数字和字符是否一一对应,for循环检查一下即可,暴力一下好了
下面展示一些 内联代码片
。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
string b;
cin >> b;
int flag = 1;
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j < n; j++)
{
if (a[i] == a[j] && b[i]!=b[j])
{
flag = 0;
}
}
}
if (flag)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
}
1744B - Even-Odd Increments
该题需要达到的目标很简单,就是根据指令加相应的数字,但是需要注意的是超时问题,我在第五个样例TTL了,过后经过优化,原本用for循环进行判断的地方我改用直接判断基数还是偶数了,减少循环次数,降低耗时。
#include <iostream>
#include <string>
using namespace std;
using ll = long long;
int main()
{
ll t;
cin >> t;
while (t--)
{
ll n, q;
ll even=0, odd=0;
cin >> n >> q;
ll* a = new ll[n];
ll sum = 0;
for (ll i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] % 2 == 0)
{
odd++;
}
else
{
even++;
}
sum += a[i];
}
for (ll i = 0; i < q; i++)
{
ll c;
cin >> c;
ll cao;
cin >> cao;
if (c == 0)
{
sum += cao * odd;
if (cao % 2 != 0)
{
even += odd;
odd = 0;
}
}
else
{
sum += cao * even;
if (cao % 2 != 0)
{
odd += even;
even = 0;
}
}
cout << sum << endl;
}
}
return 0;
}
1744C - Traffic Light
题目意思难以理解,我搞不懂什么是对于绿灯又要远又要近,官方题解
Let’s note that for each second of color c in the traffic light, we need to find the rightmost green time, and then find the largest distance between color c and the nearest green. Also, let’s not forget that traffic light states are cyclical.
又要largest distance又要nearest green,最近的绿色就是从左往右的第一个相邻绿色,哪里找最远的距离呢,感觉没法理解,可能是我英语问题.
按照题解写出来的代码
#include<iostream>
#define endl "\n"
#define IOS std::ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
using namespace std;
void solve()
{
int n;
char c;
cin>>n>>c;
string s;
cin>>s;
s=' '+s+s;
int ans=0;
int last=0;
for(int i=2*n;i>=1;i--)
{
if(s[i]=='g')
{
last=i;
}
if(i<=n&&s[i]==c)
{
ans=max(ans,last-i);
}
}
cout<<ans<<endl;
}
int main()
{
IOS;
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}