2023/03/05刷题
链接
这个题还是比较有意思的.我们可以统计左边奇数的数量和右边奇数的数量,然后还需要统计一下左边和右边奇偶性不同的个数.(因为这样的一对数才能翻转.)最后综合考虑一下
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
const int N = 104;
void solve() {
int n;
cin >> n;
int odd_1 = 0;//统计左边奇数的数量
int odd_2 = 0;//统计右边奇数的数量
int cnt = 0;//统计奇偶不同的一对数的数量
int a, b;
for (int i = 1; i <= n; i++) {
cin >> a >> b;
if (a % 2 == 1) {
odd_1++;
}
if (b % 2 == 1) {
odd_2++;
}
if (a % 2 != b % 2) {
cnt++;
}
}
if ((odd_1 + odd_2) % 2 == 1) {//如果一共有奇数个奇数不可能把两行都变成偶数
cout << -1 << '\n';
return;
}
if (odd_1 % 2 == 0 && odd_2 % 2 == 0) {//如果有左边有偶数个奇数并且右边有偶数个奇数//不需要翻转
cout << 0 << '\n';
return;
}
if (odd_1 % 2 == 1 && odd_2 % 2 == 1 && cnt >= 1) {
//如果左边有奇数个奇数右边有奇数个奇数,并且cnt>=1,只需要翻转一次
cout << 1 << '\n';
} else {//如果cnt=0的话不可能进行翻转打印-1
cout << -1 << '\n';
}
}
signed main () {
solve();
return 0;
}
A. Great Sequence
链接
这个题先把每个数有多少个存下来.然后用一个循环让每个数除以k,看看有没有除以k的数字如果有把这两个数字进行配对,之后对剩下的数字每个都配对一个新的数字就可以了,最后打印出来结果
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
const int N = 2 * 100000 + 5;
int a[N] = {0};
void solve() {
map <int, int> st;//用map来存每个数的个数,用数组会超时
int n, k;
scanf("%lld %lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
st[a[i]]++;//统计每个数字的数量
}
sort(a + 1, a + n + 1);//排序保证大的在后面
for (int i = 1; i <= n; i++) {
//如果a[i]是k的倍数并且st里面有a[i]/k这个key值,并且该值大于0的话
if (a[i] % k == 0 && st.find(a[i] / k) != st.end() && st[a[i] / k] >= 1) {
st[a[i]]--;//让a[i]值减一
st[a[i] / k]--;//让a[i]/k的值减一
}
}
int ans = 0;
//对于剩下的数字有几个就给它配对几个最后打印出来
for (auto it = st.begin(); it != st.end(); it++) {
ans = ans + it->second;
}
printf("%lld\n", ans);
/*
2
5 3
5 2 3 5 15
*/
}
signed main () {
int t;
scanf("%lld", &t);
while (t--) {
solve();
}
return 0;
}
A. Prefix and Suffix Array
链接
这个题比较简单,我们对于n为偶数的字符串我们直接将两个长度为n/2的字符拼接起来然后判断其是不是回文就可以,对于n为奇数的字符串中间那个字符是多少并不重要,我们还是只需要把长度为n/2(下取整)的字符串拼接起来判断一下就可以
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
void solve() {
int n;
cin >> n;
int len = n / 2;
vector<string> s;//储存两个n/2的字符串
string x;
for (int i = 1; i <= 2 * n - 2; i++) {
cin >> x;
if (x.size() == len) {
s.push_back(x);
}
}
string res = s[0] + s[1];//拼接两个字符串
int flag = 1;//1为是,0为不是
for (int i = 0; i < len; i++) {//判断两个字符串是不是回文
if (res[i] != res[res.size() - i - 1]) {
flag = 0;
break;
}
}//打印
if (flag == 1) {
yes;
} else {
no;
}
}
signed main () {
int t;
scanf("%lld", &t);
while (t--) {
solve();
}
return 0;
}
/*
2
20 2
13 2 8 12 15 11 2 4 19 6 13 2 14 8 9 12 10 7 16 5
*/
4867.整除数
链接
看一下代码里面的注释
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
signed main () {
int n, k;
cin >> n >> k; //读入n和k
//先让n+1除以k向上取整,然后在乘以k就可以得到严格大于n的一个最近的k的倍数
int res = ((n + 1 + k - 1) / k) * k;
cout << res << '\n';
return 0;
}
/*
2
20 2
13 2 8 12 15 11 2 4 19 6 13 2 14 8 9 12 10 7 16 5
*/
B. Not Dividing
链接
这个题没想起来看了题解才发现我想的太复杂的.如果a[i]中有是1的数字我们将这个数字变成2,(这样就算全部的a[i]都为1,只执行n次),然后对如果a[i]%a[i-1]==0这样的话,我们对a[i]加1(因为a[i]至少为2所以加上1之后肯定不是a[i-1]的倍数了)这样我们就可以保证最多只会执行2n次
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
const int N = 10005;
void solve() {
int n;
int a[N] = {0};
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] == 1) {//如果a[i]为1就变成2
a[i]++;
}
}
for (int i = 2; i <= n; i++) {//如果a[i]可以整除a[i-1]就让a[i]++
if (a[i] % a[i - 1] == 0) {
a[i]++;
}
}
for (int i = 1; i <= n; i++) {//打印结果
printf("%lld ", a[i]);
}
printf("\n");
}
signed main () {
int t;
cin >> t;
while (t) {
solve();
t--;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具