B. Effective Approach
链接
用一个st数组记录每一个数据读入的下标,如果是正序寻找就是这个下标,如果是逆序寻找就是n减去这个下标,然后每种情况用res1和res2记录下来
#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 = 100005;
int a[N], st[N];
signed main () {
ios::sync_with_stdio(false);
int n, m;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
st[a[i]] = i; //将a[i]元素的对应的下标记录到st数组里面
}
int res1 = 0, res2 = 0;
cin >> m;
while (m--) {
int a;
cin >> a;
res1 = res1 + st[a]; //正序时枚举的次数
res2 = res2 + (n + 1) - st[a]; //倒叙时枚举的次数
}
cout << res1 << ' ' << res2;
//打印结果
return 0;
}
C. Yet Another Broken Keyboard
链接
C. Yet Another Broken Keyboard
可以发现键盘上面不能打出的字母将会将字符串分割成若干字串,将这些字串作为母串分别求他们的字串的数量然后累加就可以算出数量了
#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;
bool st[30];
signed main () {
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
string s;
cin >> s;
while (k--) {
char x;
cin >> x;
st[x - 'a'] = true;//用st[]记录这个字母存不存在
}
int res = 0;
//双指针算法
for (int i = 0; i < n; i++) {
int j = i;
int cnt = 0;
int flag = 0;//如果进入循环就变成1
while (j < n && st[s[j] - 'a'] == true) {
//用j向后寻找键盘上面不能打出来的数
j++;
cnt++;//计数器
flag = 1;
}
if (cnt > 0) {
//cnt大于1说明有字串
res = res + (cnt * (cnt + 1) / 2);//累加字串里面的所有子串
}
if (flag == 1) {//如果flag=1的话,就让i等于j,防止死循环
i = j - 1;
}
}
cout << res;//打印结果
return 0;
}
A. Parallelepiped
链接
这个题我都思路有问题,看了别人的思路才解决
总体来说还是求出每个变长然后累计乘4求出全部的棱长之和
#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 () {
// ios::sync_with_stdio(false);
double ab, bc, ac;
cin >> ab >> bc >> ac;
double a = sqrt(ab * bc * ac) / bc; //求a
double b = sqrt(ab * bc * ac) / ac; //求b
double c = sqrt(ab * bc * ac) / ab; //求c
printf("%.0lf", (a + b + c) * 4.0); //算全部棱长,然后保留整数
return 0;
}
A. Life Without Zeros
链接
这个题比较好写,先算出结果,然后将a和b分别去0,然后将结果去0为c,计算去0之后的结果相加和c进行比较相等打印yes,不等打印no
#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 () {
ios::sync_with_stdio(false);
int a, b;
cin >> a >> b;
int c = a + b;
string x1 = to_string(a);
string x2 = to_string(b);
string x3 = to_string(c); //转换字符串,方便去0
int a1 = 0;
for (int i = 0; i < (int)x1.size(); i++) {
if (x1[i] != '0') {
a1 = a1 * 10;
a1 = a1 + x1[i] - '0';
}
}
//将a去0之后保存在a1
int a2 = 0;
for (int i = 0; i < (int)x2.size(); i++) {
if (x2[i] != '0') {
a2 = a2 * 10;
a2 = a2 + x2[i] - '0';
}
}
//将b去0之后保存在a2
int a3 = 0;
for (int i = 0; i < (int)x3.size(); i++) {
if (x3[i] != '0') {
a3 = a3 * 10;
a3 = a3 + x3[i] - '0';
}
}
//将c去0之后保存在a3
if (a1 + a2 == a3) { //相等打印yes
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
C. Yet Another Array Restoration
链接
C. Yet Another Array Restoration
这个题还是比较有意思的,就是枚举然后找到一个最小的公差d,然后我们判断一下这个按照这个公差d生成的最后一项是不是负数,如果是就将最后一项倍增成正数,然后输出
#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 () {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n, x, y;
cin >> n >> x >> y;
int a = n - 1; //最多可以产生n-1个公差
while ((y - x) % a != 0) { //判断公差是不是合法,如果第一个合法的a肯定是最大的然后这样的公差是最小的
a--;
}
int d = (y - x) / a;//计算公差
//计算最后一项是不是负数,如果是就让y加上公差,直到最后一项不是负数
while ((y - (n - 1)*d) <= 0) {
y = y + d;
}
cout << y << ' ';//先打印y
//然后每次减去公差//一共打印n项
for (int i = 1; i < n; i++) {
y = y - d;
cout << y << ' ';
}
cout << '\n';
}
return 0;
}
C. Minimum Extraction
链接
这个题本质上面就是判断排序之后的前一项减后一项的最大值,因为每次都会减去最后的一个值,然后后面全部的值都会减去这个数,但是他们的差没变,所以其实本质就是前一项减后一项的全部的最大值,但是我不会写,看了别人的思路才会
#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 = 200005;
int a[N];
signed main () {
int t;
scanf("%lld", &t);
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
sort(a, a + n); //排序
int mmax = a[0]; //将最大值先设置为a[0]
for (int i = 1; i < n; i++) {
mmax = max(mmax, a[i] - a[i - 1]); //寻找差值的最大值
}
printf("%lld\n", mmax);
//打印结果
}
return 0;
}
标签:02,20,cout,int,long,define,yes,include,刷题
From: https://www.cnblogs.com/harper886/p/17139308.html