A题意:给三个数,给出三个数中不一样的数
思路:模拟
代码:
#include <iostream>
#include <unordered_map>
using namespace std;
int findUnique(int a, int b, int c) {
unordered_map<int, int> count;
count[a]++;
count[b]++;
count[c]++;
for(auto it : count) {
if(it.second == 1) {
return it.first;
}
}
return 0; // This line will never be reached as there will always be exactly one unique value.
}
int main() {
int t;
cin >> t;
while(t--) {
int a, b, c;
cin >> a >> b >> c;
int unique = findUnique(a, b, c);
cout << unique << endl;
}
return 0;
}
B题意:给一个3*3的正方形,每一个点的串和行的字符都要是A,B,C,在唯一个?填补上应该填的
思路:直接模拟暴力来写
代码:
#include <iostream>
#include <array>
#include <vector>
char findMissing(const std::array<std::string, 3>& square, int row, int col) {
// set容器帮助我们跟踪哪些字母已经出现了
std::array<bool, 3> present{false};
// 检测缺失的字母所在的行
for (char ch : square[row])
if (ch != '?')
present[ch - 'A'] = true;
// 检测缺失的字母所在的列
for (const std::string& s : square)
if (s[col] != '?')
present[s[col] - 'A'] = true;
// 返回第一个没有出现的字母
for (int i = 0; i < 3; ++i)
if (!present[i])
return 'A' + i;
// 安全网:我们预计这行代码永远都不会被执行到
return 'A';
}
int main() {
int t;
std::cin >> t;
while (t--) {
std::array<std::string, 3> square;
std::vector<std::pair<int, int>> question_marks;
for (int i = 0; i < 3; ++i) {
std::cin >> square[i];
for (int j = 0; j < 3; ++j) {
if (square[i][j] == '?') {
question_marks.emplace_back(i, j);
}
}
}
// 此处只处理一个问号的情况(如题干所述)
auto& mark = question_marks[0];
char missing_char = findMissing(square, mark.first, mark.second);
std::cout << missing_char << std::endl;
}
return 0;
}
C题意:给一个数组,判断能否用数组内的数字建立一个正方形
思路:直接求平方根
代码:
#include <iostream>
#include <vector>
#include <cmath>
bool canBuildSquare(const std::vector<long long>& buckets) {
long long totalSquares = 0;
for (auto& num : buckets) {
totalSquares += num;
}
// 判断总面积是否是完全平方数
long long side = sqrt(totalSquares);
return side * side == totalSquares;
}
int main() {
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
std::vector<long long> buckets(n);
for (int i = 0; i < n; ++i) {
std::cin >> buckets[i];
}
std::cout << (canBuildSquare(buckets) ? "YES" : "NO") << '\n';
}
return 0;
}
D题意,有元音a,e为V,b,c,d为C,存在两个组合,CV,CVC,给一个字符串,按照两个组合划分
思路:根据题意我们能知道优先判断是否有CVC,的情况,然后再选择用CVC还是CV,假如CVC后为C,那么使用,否则使用CV,
代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool in1(char c)
{
if(c=='a'||c=='e')
return true;
return false;
}
void test() {
ll n;
cin >> n;
string s;
cin >> s;
int t;
int i = 0;
if (s.size() <= 3) {
cout << s << endl;
return;
}
for (i = 0; i < s.size() - 3;) {
if (!in1(s[i]) && in1(s[i + 1]) && !in1(s[i + 2])) {
i++;
if (!in1(s[i + 2])) {
i--;
cout << s.substr(i, 3) << ".";
i += 3;
continue;
}
i--;
cout << s.substr(i, 2) << ".";
i += 2;
} else {
cout << s.substr(i, 2) << ".";
i += 2;
}
}
if (s.size() <= 3)
cout << s << endl;
else cout << s.substr(i) << endl;
}
int main() {
int t;
cin >> t;
while (t--) { // Repeat for the number of test cases
test();
}
return 0;
}
E题意:给出一个数组,让选出范围l,r,求出在范围内的奇数和以及偶数和,最后判断是否存在奇数和以及偶数和相等的情况,假如存在输出YES,否则输出NO
思路:采用前缀和根据题意我们可以求出,之所以用前缀和,我们能看到求l到r,因为奇数和要等于偶数和,所以偶数和加奇数和为0,所以奇数处的数值修改为负数
可以通过判断是否出现前缀和为0,以及当前前缀和的值已经出现过,假如出现过,那么当前的数值减去前面已经出现的就是0,说明是YES,要是两种都没出现就是NO
代码:
#include <iostream>
#include <vector>
#include <set>
void solve() {
int n;
std::cin >> n;
std::vector<long long> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
if (i % 2 == 1) {
a[i] = -a[i];
}
}
std::partial_sum(a.begin(), a.end(), a.begin());
std::set<long long> st;
for (int i = 0; i < n; i++) {
if (st.count(a[i]) || a[i] == 0) {
std::cout << "YES" << std::endl;
return;
}
st.insert(a[i]);
}
std::cout << "NO" << std::endl;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
F题意:在一个数轴上,给t个测试案例,每个案例中,给出n个出发点和终点,n个出发点的人,都以1m/s的速度出发,最终到达终点后停下,求整个过程中,有多少次两人相见
思路:根据题意,我们能知道有n个出发点和终点,因为所有人速度都一样,所以我们只需要关注起点和终点的情况就可以了,假如说一个出发点到一个终点内包含有其他的终点,那么就一定会相遇,所以只要看是否有包含就可以,我们可以用一个map来存终点和出发点的关系,再用对终点进行排序,因为map本身会进行排序,因为终点减去初始点范围越大,那么出发点就越小,所以根据这个道理,我们可以根据map排序好的出发点来寻找对应的终点用查找在数组中的位置,那么这个数组位置前的数字就都是被包含在出发点到终点当中的终点,也就是会相遇的
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void test()
{
map<int,int>mp;
ll n,ans=0;
cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++)
{
ll a,b;
scanf("%lld%lld",&a,&b);
v[i]=b;
mp[a]=b;
}
sort(v.begin(),v.end());
for(auto it:mp)
{
auto pos= lower_bound(v.begin(),v.end(),it.second);
ans+=pos-v.begin();
v.erase(pos);
}
printf("%lld\n",ans);
}
signed main()
{
int t;
cin>>t;
while(t--)
test();
}
这里的代码必须用二分查找low_bound,来对时间进行优化,运行时间为4960ms,假如说运行时间到5000ms,就会超时,大多都用的树状数组,等期末结束就去补
G题意://G题实在补不出来,需要用最短路算法,但是有点忘了,马上期末全结束,期末结束了再来补
PS:妈的,比赛的时候网烂炸了,只做了40分钟就退了,一定要把这个div4全部题补了,靠
浙公网安备 33010602011771号