A.简单数学
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef double D;
const int N = 2e5 + 10;
typedef pair<int, int> Pii;
void solved()
{
int a, b, c;
cin >> a >> b >> c;
if (a > b && c > b)
{
int ans = min(a - b, c - b);
cout << ans;
}
else
cout << 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--)
{
solved();
}
return 0;
}
B.(构造,模拟)
要找k个不一样,那无非就是直接令k个平移,然后把第k个放第一个,然后其余都是相等的
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef double D;
const int N = 2e5 + 10;
typedef pair<int, int> Pii;
int a[N];
void solved()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
if((k==1)||(k>n)){cout<<-1;return;}
if(k==0){
for(int i=1;i<=n;i++)cout<<a[i]<<' ';
return;
}
cout<<a[k]<<" ";
for(int i=1;i<k;i++)cout<<a[i]<<" ";
for(int i=k+1;i<=n;i++)cout<<a[i]<<" ";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t=1;
while (t--)
{
solved();
}
return 0;
}
C(数学,模拟)
判断是不是4的倍数只要看最后俩位就行,因为整百的都是4的倍数,然后模拟就行
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef double D;
const int N = 2e5 + 10;
typedef pair<int, int> Pii;
int a[N];
void solved()
{
string s;
cin>>s;
int ans=0;
if(s.size()==1){
int x=s[0]-'0';
if(x%4==0)cout<<0<<"\n";
else cout<<-1;
return;
}
int pre=s[s.size()-1]-'0';
int tp=(s[s.size()-2]-'0')*10+pre;
if(tp%4==0){cout<<ans<<"\n";return;}
for(int i=0;i<s.size();i++){
ans++;
int x=s[i]-'0';
tp=pre*10+x;
if(tp%4==0){cout<<ans<<"\n";return;}
pre=x;
}
cout<<-1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t=1;
while (t--)
{
solved();
}
return 0;
}
D(贪心,前缀和)
要的使这三red的数量最多,因为数量最多,所以r全部在最前面,e在最中间,d在最后,r,e,d尽量平均,令,r,e,d,的数量为a,b,c,a+b+c=n,要使得abc相乘最大,当a==b==c时,这里只要统计一下前缀和就行,然后看余数,当区间的大小小于3记得特判为0
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef double D;
const int N = 2e5 + 10;
typedef pair<int, int> Pii;
const int inf=1e9;
int pr[N],pe[N],pd[N];
void solved()
{
int n,k;
cin>>n>>k;
string s;cin>>s;
for(int i=1;i<=n;i++){
pr[i]=pr[i-1];
pe[i]=pe[i-1];
pd[i]=pd[i-1];
if(s[i-1]=='r')pr[i]++;
else if(s[i-1]=='e')pe[i]++;
else pd[i]++;
}
while(k--){
int ans=inf;
int l,r;
cin>>l>>r;
int s=(r-l+1);
if(s%3==0){
int num=s/3;
int tp=0;
tp+=num-(pr[l+num-1]-pr[l-1]);
tp+=num-(pe[l+2*num-1]-pe[l+num-1]);
tp+=num-(pd[r]-pd[r-num]);
ans=min(tp,ans);
}
else{
int num=s/3;
int tp=0;
int d1,d2;
if(s%3==1){
//左边多1
d1=l+num,d2=r-num+1;
tp=s-(pr[d1]-pr[l-1]+pe[d2-1]-pe[d1-1]+pd[r]-pd[d2-1]);
ans=min(tp,ans);
//中间多1
d1=d1-1;
tp=s-(pr[d1]-pr[l-1]+pe[d2-1]-pe[d1-1]+pd[r]-pd[d2-1]);
ans=min(tp,ans);
//右边多1
d2=d2-1;
tp=s-(pr[d1]-pr[l-1]+pe[d2-1]-pe[d1-1]+pd[r]-pd[d2-1]);
ans=min(tp,ans);
}
else{
//左边少1
d1=l+num-1,d2=r-num;
tp=s-(pr[d1]-pr[l-1]+pe[d2-1]-pe[d1-1]+pd[r]-pd[d2-1]);
ans=min(tp,ans);
//中间少1
d1=d1+1;
tp=s-(pr[d1]-pr[l-1]+pe[d2-1]-pe[d1-1]+pd[r]-pd[d2-1]);
ans=min(tp,ans);
//右边少1
d2=d2+1;
tp=s-(pr[d1]-pr[l-1]+pe[d2-1]-pe[d1-1]+pd[r]-pd[d2-1]);
ans=min(tp,ans);
``` js
}
}
cout<<ans<<"\n";
}
}
`` ...就这么多了吧,懒+没时间