纯纯的手速场
A AB Balance
题目大意
给定一个字符串问最少修改几个字母是的ab和ba的数量相同,求修改后的字符串
主要思路
由于中间的ab ba数量会抵消一部分,例如abbabab等,所以ab,ba的数量最多相差一个,也就是只与开头结尾的字符有关,假如开头结尾的字符相同,那么数量一定相同,如果字符不同,那么数量一定不相同
AC代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int main()
{
int T;
cin >> T;
while(T--)
{
string s;
cin >> s;
s[0] = s[s.size() - 1];
cout << s << endl;
}
}
B Update Files
题目大意
给定一个数n和k,最开始有一台电脑存在数据,最多有k条线缆,一共要求n台电脑都获得数据,问最少传输几次
主要思路
首先第一次一定是1,因为只有一台电脑存在数据,其次1,2,4…k, k,k所以找到该序列即可确定最少次数
AC代码
#include <bits/stdc++.h>
#define debug(a) cout << #a << " = " << a << endl;
#define int long long
using namespace std;
const int N = 1000010;
int n, k;
signed main(void)
{
int T;
scanf("%lld", &T);
while(T--)
{
scanf("%lld%lld", &n, &k);
int cnt = 1;
int ans = 0;
while(cnt < k) cnt *= 2;
int t = 1;
int sum = 1;
while(sum < n && t < cnt)
{
sum += t;
t *= 2;
ans++;
}
if(sum < n) ans += (n - sum + k - 1) / k;
printf("%lld\n", ans);
}
return 0;
}
C Banknotes
题目大意
给定一个数n和k,下面给出n个整数,分别代表10的几次方,问由k张纸币不能凑出的最小数是多少
主要思路
首先判断出当前会用到那几个数,也就是由前面的数能否在k个内凑到后面的数,如果不能那么答案一定在小于后面的数范围内,也就是判断出n个数中会在答案中用到的数有哪几个
接下来按每一个数判断,也就是用k+1张牌去凑出一个k张牌凑不出的数
根据贪心思想,我们先填满低位再去填高位,具体看注释
AC代码
#include <bits/stdc++.h>
#define debug(a) cout << #a << " = " << a << endl;
#define int long long
using namespace std;
const int N = 1000010;
int n, k;
int a[100];
signed main(void)
{
int T;
scanf("%lld", &T);
while(T--)
{
scanf("%lld%lld", &n, &k);
for(int i = 0; i < n; i++) scanf("%lld", &a[i]);
int pos = -1;
bool flag = true;
for(int i = 1; i < n; i++)
{
int cnt = pow(10, a[i] - a[i - 1]) - 1;
if(k < cnt)
{
pos = i + 1;
flag = false;
break;
}
}
int ans = 0;
if(!flag) n = min(n, pos);
// debug(pos)
k++;
for(int i = 1; i < n; i++)
{
int cnt = pow(10, a[i] - a[i - 1]) - 1;//当前位能填的数量
// debug(cnt)
if(k >= cnt) k -= cnt, ans += pow(10, a[i - 1]) * cnt;//如果k的数量够那么就全部使用
else
{
ans += pow(10, a[i - 1]) * k;//如果不够那么使用k张
k = 0;
break;
}
// debug(ans)debug(k)
}
int t = pow(10, a[n - 1]) * k;
// debug(k)
if(k) ans += t;//如果k有剩余这一步是判断最高位的使用数量
cout << ans << endl;
}
return 0;
}