A.Painting the Ribbon
题目大意
A将n段彩带涂成m种颜色,B可以修改k段彩带,B想让彩带颜色一样
思路
B需要将所有彩带都涂成A已经涂过的某种颜色,显然至少有一种颜色涂了n/m(向上取整)种,那么B把其余的彩带涂成这种颜色即可
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define pll pair<ll,ll>
#define endl "\n"
const int Inf =1e18;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
inline ll read();
void solve() {
int n=read(),m=read(),k=read();
int num=n-ceil((double)n/m);
if(k>=num)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr),cout.tie(nullptr);
int tt = 1;
tt=read();
while (tt--)
solve();
}
inline ll read() {
ll x = 0, ch = getchar(), f = 1;
while (!isdigit(ch)) {
if (ch == '-')
f = -f;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
B.Make It Ugly
题目大意
给出一串序列,如果某个位置的前一个和后一个数字相同,那么中间这个也会变成一样的数字,题目要求移除从序列中移除最少数量的数字,让这个序列不能全部变成一样的数字
思路
开始的思路是让这个序列的头和尾数字不一样即可,但除此之外,还可以让两个与头部不一样的数字相遇,这样这两个数字就不会被改变了。只需要维护一个非头部数字的位置数组即可
代码
如果序列中数字完全一样输出-1即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define pll pair<ll,ll>
#define endl "\n"
const int Inf =1e18;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 10;
inline ll read();
int a[N];
void solve() {
int n=read();
bool f=0;
for(int i=1;i<=n;i++){
a[i]=read();
if(a[i]!=a[1])f=1;
}
if(f==0){
cout<<-1<<endl;
return ;
}
int ans=inf;
if(a[1]!=a[n]){
cout<<0<<endl;
return ;
}
vector<int>pos;
pos.push_back(0);
for(int i=2;i<=n;i++){
if(a[i]!=a[1]){
pos.push_back(i);
}
}
pos.push_back(n+1);
for(int i=0;i<pos.size()-1;i++){
ans=min(ans,pos[i+1]-pos[i]-1);
}
cout<<ans<<endl;
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr),cout.tie(nullptr);
int tt = 1;
tt=read();
while (tt--)
solve();
}
inline ll read() {
ll x = 0, ch = getchar(), f = 1;
while (!isdigit(ch)) {
if (ch == '-')
f = -f;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
C. Long Multiplication
题目大意:
给出两个超级大的整数,你可以任意交换他们的第i位数字,要求所得的两个整数乘积最大
思路
首先明确,两个数乘积最大时为两个数相等的情况。那么,就想办法让这两个数在交换过程中接近他们两个的平均值。而我们又不可能计算出他们的平均值,那么就先找到第一位数字不同的位置,判断哪个数字比较大,在后面低位的比较中尽可能让大的数字减小,将所有本位大的转移给比较结果小的即可。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define pll pair<ll,ll>
#define endl "\n"
const int Inf =1e18;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int mod=1e9+7;
inline ll read();
void solve() {
string s1,s2;
cin>>s1>>s2;
bool f=0;
bool flag=0; //0:s1>s2 1:s1<s2
for(int i=0;i<s1.size();i++){
if(s1[i]==s2[i])continue;
else if(f==0){
f=1;
if(s1[i]-'0'>s2[i]-'0')flag=0;
else flag=1;
}else{
if(flag==0&&s1[i]-'0'>s2[i]-'0')
swap(s1[i],s2[i]);
else if(flag==1&&s1[i]-'0'<s2[i]-'0')
swap(s1[i],s2[i]);
}
}
cout<<s1<<endl<<s2<<endl;
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr),cout.tie(nullptr);
int tt = 1;
tt=read();
while (tt--)
solve();
}
inline ll read() {
ll x = 0, ch = getchar(), f = 1;
while (!isdigit(ch)) {
if (ch == '-')
f = -f;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}