Problem AT Yes-Yes?
判断输入字符串s是否是Yes重复n次的子字符串(n>=1)
思路:一次遍历即可
情况1:如果s的长度==1且s[0]不为Yes中的任意一个,输出NO
否则输出YES
情况2:s的长度大于1,进行一次遍历
case1:如果字符非Yes中的任意一个,输出NO
case2:如果字符为Y且下一个字符不为n, 输出NO
case3:如果字符为e且下一个字符不为s,输出NO
case4:如果字符为s且下一个字符不为Y,输出NO
case5:输出YES
#include <iostream>
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
int N;
std::cin >> N;
while (N) {
N -= 1;
std::string s;
std::cin >> s;
if (s.size() == 1) {
if (s[0] != 'Y' && s[0] != 'e' && s[0] != 's') {
std::cout << "NO\n";
}
else {
std::cout << "YES\n";
}
}
else {
int i;
for (i = 0; i < s.size() - 1;++i)
{
if (s[i] != 'Y' && s[i] != 'e' && s[i] != 's') {
std::cout << "NO\n";
break;
}
else if (s[i] == 'Y' && s[i + 1] != 'e') {
std::cout << "NO\n";
break;
}
else if (s[i] == 'e' && s[i + 1] != 's') {
std::cout << "NO\n";
break;
}
else if (s[i] == 's' && s[i + 1] != 'Y') {
std::cout << "NO\n";
break;
}
}
if (i == s.size() - 1) {
std::cout << "YES\n";
}
}
}
return 0;
}
Problem B Lost permutation
如果一个数组可以排序成一个从1开始每次递增1的连续数组,那么这个数组称为permutation array
给定一个数组b,再给定一个值s,判断能否将s分解为不同的数值,恰好使数组b成为permutation数组
思路:建立集合sett,将b中的元素加入到sett中,然后从1开始进行for循环,如果当前数值存在于sett中,继续循环,如果不存在于sett中,s减去这个数并将这个数加入到sett,一直到s小于等于0时结束循环。
循环结束后:
s < 0:说明s的值已经不够分配最后一个数,输出NO
s == 0:
对sett进行遍历,因为sett是有序序列,说明sett的相邻的元素差值为1,如果差值不为1,则输出NO,结束遍历。遍历的起点不需要从sett的第一个元素开始,因为sett每加入一个新的元素,sett之前的元素都是有序的。所以只要从最后一次加入sett的元素开始遍历即可。
如果遍历正常结束,则输出YES
#include <iostream>
#include <set>
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
int N, n, s;
std::cin >> N;
while (N) {
N -= 1;
std::cin >> n >> s;
std::set<int> sett;
for (int i = 0; i < n; ++i) {
int t;
std::cin >> t;
sett.insert(t);
}
int last_push;
for (int i = 1; s > 0; ++i) {
auto it = sett.find(i);
if (it == sett.end()) {
s -= i;
last_push = i;
sett.insert(i);
}
}
if (s < 0) {
std::cout << "NO\n";
}
else {
auto limit = sett.end(), it = sett.find(last_push), next = it;
limit--;
next++;
while (it != limit) {
if (*it + 1 != *next) {
std::cout << "NO\n";
break;
}
++it;
++next;
}
if (it == limit) {
std::cout << "YES\n";
}
}
}
return 0;
}
problem C Thermostat
给定long long 型整数,l, r, x, a,b.判断能否通过若干次变化将a变到b。并且在变化的过程中a±的值不能小于x,且不超过范围[l, r]。如果能够变到b,那么输出最少操作次数,如果不能,输出-1;
思路:分情况讨论。
如果a == b,则需要Operation 0次。
如果a < b,那么将a与b交换。
case1:a只需一次操作就能变到b,如果b - a >= x;即a与b的差值超过x。
case2:需要两次操作变到b,如果r - b >= x || a - l >= x。
第一个条件的意思是a可以先增加到区间[a + x, r],再减去一个大于等于x的数得到b。第二个条件的意思a可以先减小到区间[l, a - x]再增加一个大于等于x的数得到b。这里补充一下为什么第一个r-b>=x就可以判定两次就能得到b,因为如果r - b >= x,那么说明r - a 一定大于x,就是说,a一定可以加一个数得到r,那么再减去一个数,得到b。同理,a - l >= x说明b - l一定大于x,先让a得到一个较小的数,再加一个大于刚才a减去的x的数,就可以得到b,所以这种情况是两次操作。
case3:需要三次操作变到b,如果 r - a >= x && b - l >= x。
这个跟case2差不多,因为a是一个较小的数,所以先让a变换到r,a变换到l,再让a变换到b。第一个条件说明a可以到区间[a + x, r]。在a第一次增加以后,再将a减去一个比刚才x更大的数,将a缩小到一个[l, b - x]的区间,在这个区间中,必然存在一个不小于x的数,使a + x == b;
#include <iostream>
typedef long long int ll;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
int N;
std::cin >> N;
while (N) {
N -= 1;
ll l, r, x, a, b;
std::cin >> l >> r >> x >> a >> b;
if (a > b) {
ll tmp = a;
a = b;
b = tmp;
}
if (a == b) {std::cout << "0\n";}
else if (b - a >= x) { std::cout << "1\n"; }
else if (r - b >= x || a - l >= x) {std::cout << "2\n"; }
else if (r - a >= x && b - l >= x){std::cout << "3\n";}
else {std::cout << "-1\n";}
}
return 0;
}
D. Make It Round
题意:给定n和m,n乘一个任意小于等于m的数,使乘积的末尾0的数目最多。如果有多个答案,输出最大数。如果没有这样的数,输出m * n;
思路:题目要求末尾的0的数目最多,而且是两个数相乘的形式。所以设置一个数divide为10整数倍数,如果divide是一个小于m的数,那么不断的给divide扩大十倍。 这里扩大十倍的含义是不断的找n与10,与100,与1000....的最小公倍数,这样可以确定m范围内一个数与n相乘后最多可以有几个0。这里用cnt表示n变化到最小公倍数需要变化多少次。
比如n = 3, m = 250。可以发现3与10的最小公倍数30得到cnt = 10,满足末尾有一个0的条件,与100的最小公倍数300,cnt = 100也满足条件,此时末尾有两个0。但是与1000的最小公倍数cnt显然大于m = 250,所以不满足条件。最后cnt = 100。
在找到这样的一个cnt后,再找出区间[cnt,m]中0数目不变但是最大的数即可。即n * c * cnt仍然满足末尾0数目不变,比如上面的cnt = 100, n = 3。 n * 100 与 n * 2 * 100的结果的末尾的0的个数相等。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
#define INT_MAX pow(2, 31) - 1
#define INT_MIN -pow(2, 31)
bool if_primary(ull&a){
for (int i = 2; i <= sqrt(a); ++i){
if (!(a % i)){return false;}
}
return true;
}
ull lcm(ull a, ull b){
if(a > b){swap(a, b);}
if (!(b % a)){return b;}
if(if_primary(a) && if_primary(b)){return a * b;}
for (int i = 1; ;++i){if ((b * i) % a == 0 ){return b * i;}}
}
int main(){
int N, m, n;
cin >> N;
while (N--){
cin >> n >> m;
ull cnt = 1, divide = 10;
while(1){
int x = lcm(divide, n) / n;
if (x > m){break;}
divide *= 10;
cnt = x;
}
cnt *= int(m / cnt);
cout << n * cnt << endl;
}
}
超时 寄
2023.3.7更
思路:找出数字n中2和5质因子的数目,然后在满足m的条件下尽可能的让2和5的数目相等,这样可以凑更多的10因子出来,然后再继续更新一下提升的次数,让最后的数值最大。
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cmath>
using namespace std;
typedef long long int ll;
#define INT_MAX pow(2, 31) - 1
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int N;
cin >> N;
while (N--){
int n, m, t;
cin >> n >> m;
t = n;
int two_num = 0, five_num = 0;
ll increasing_time = 1;
while (n && n % 2 == 0){two_num += 1; n /= 2;}
while (n && n % 5 == 0){five_num += 1; n /= 5;}
while (two_num < five_num && increasing_time * 2 <= m){two_num += 1; increasing_time *= 2;}
while (five_num < two_num && increasing_time * 5 <= m){five_num += 1; increasing_time *= 5;}
while (increasing_time * 10 <= m) increasing_time *= 10;
increasing_time *= m / increasing_time;
cout << (increasing_time == 1? t * m : t * increasing_time) << endl;
}
return 0;
}