记录一下比赛中知识盲区
C Cheap kangaroo
三目运算实现辗转相除求两数最大公约数
辗转相除求最大公约数
ll gcd(ll a, ll b)
{
return b > 0 ? gcd(b, a % b) : a;
}
ac
#include <bits/stdc++.h>
#define ll long long
#define TLE ios::sync_with_stdio(false);
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
const double PI = 3.1415926535;
ll gcd(ll a, ll b)
{
return b > 0 ? gcd(b, a % b) : a;
}
void solve()
{
ll ans=INF,sum=0;
ll n, a[N];
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum += a[i];
}
for (int i = 1; i <= n; i++)
{
ans = min(ans, gcd(a[i], a[i - 1]));
}
cout << sum<<" " << ans << endl;
}
int main()
{
TLE;
int _;
cin >> _;
while (_--)
{
solve();
}
return 0;
}
D Magica bamboos
奇数减奇数等于偶数,偶数减偶数等于偶数。
ac代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define TLE ios::sync_with_stdio(false);
const int N = 1e5 + 10;
int a[N];
//判断奇偶
bool odd(int n)
{
if (n % 2 == 0)return false;
else return true;
}
void solve()
{
int n,a,ans=1;
cin >> n;
cin >> a;
if (odd(a))//只有后续数字与第一个数字奇偶性相同才能保证数组中所有元素俩俩之差不出现奇数
{
for (int i = 2; i <= n; i++)
{
cin >> a;
if (!odd(a))ans = 0;
}
}
else
{
for (int i = 2; i <= n; i++)
{
cin >> a;
if (odd(a))ans = 0;
}
}
if (ans == 1)cout << "yes" << endl;
else cout << "no" << endl;
}
int main()
{
TLE;
int _;
cin >> _;
while (_--)
{
solve();
}
return 0;
}
另外,一直判断相邻元素差即可。
#include<bits/stdc++.h>
#include <iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int n, a[N];
int main()
{
ios::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
bool flag = 0;
for (int i = 1; i < n; ++i)
{
int k = a[i + 1] - a[i];
if (k % 2 !=0)
{
flag = 1;
break;
}
}
if (flag==0) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}
H
//判断回文
bool rev(string s)
{
string t;
t = s;
reverse(s.begin(), s.end());
if (s == t)return true;
else return false;
}
ac
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define TLE ios::sync_with_stdio(false);
bool pp(string s,string p)//字符串长度1e3可以写n2的暴力
{
for (int i = 0; i < s.length(); i++)
{
int j;
for ( j = 0; j < p.length(); j++)
{
if (s[i] == p[j])break;
}
if (j >= p.length())return false;
}
return true;
}
bool rev(string s)
{
string t;
t = s;
reverse(s.begin(), s.end());
if (s == t)return true;
else return false;
}
int main()
{
int n;
cin >> n;
string s,p="AHIMOTUVWXY";
while (n--)
{
cin >> s;
if (pp(s, p) && rev(s))cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}
M
//学一个新的unorder_map
unordered_map<string, double>mp;
mp["JD"]=1;
ac
#include <bits/stdc++.h>
#define ll long long
#define TLE ios::sync_with_stdio(false);
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
unordered_map<string, double>mp;
void solve()
{
mp.clear();
int n, m;
string s;
double k, ans=0;
mp["JD"] = 1.0;
cin >> n >> m;
while (n--)
{
cin >> s >> k;
mp[s] = k;
}
while (m--)
{
cin >> k >> s;
ans += k * mp[s];
}
printf("%.6lf\n", ans);
}
int main()
{
TLE;
int _;
cin >> _;
while (_--)
{
solve();
}
return 0;
}