A
count(1)
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
int cnt=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==1)cnt++;
}
cout<<n-cnt<<endl;
return 0;
}
C
只要a[i]!=1&&a[i-1]!=1就能插入0,每次插入后ai后要是又能插入的话就需要记录一下,其实就是把ai-=w就行了
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
int n;
cin>>n;
vector<int>a(n+1);
int sum=0;
for(int i=1;i<=n;i++)cin>>a[i];
a[n+1]=INT_MAX;
a[0]=INT_MAX;
for(int i=1;i<=n+1;i++)
{
if(a[i]!=1&&a[i-1]!=1)
{
sum+=min(a[i],a[i-1])-1,a[i]-=min(a[i],a[i-1])-1;
//cout<<i<<endl;
}
}
cout<<sum<<endl;
return 0;
}
G H
线性筛+构造
以n=3为eg:1 3 2 其实ai+i后是2 5 5 看出在相邻数字时将ai+i和aj+j构造成一个相同的质数就能过了(why -1 )
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool not_prime[2000005]={0};
int a[2000005],prime[2000005];
signed main()
{
int n,t,z=0;
cin>>n;
for(int i=2;i<=2*n;++i)
{
if(!not_prime[i])
{
prime[++z]=i;
for(int j=i*i;j<=2*n;j+=i)
not_prime[j]=1;
}
}
t=n;
while(t>=1)
{
int x=prime[z],y=t;
z--;
for(int i=y;x-i<=y&&i>=1;i--)
{
a[i]=x-i;
t--;
}
}
for(int i=1;i<=n;i++)
cout<<a[i]<<' ';
return 0;
}
I
模拟
建议画图就好(看题目看不太懂使我脑壳昏去)
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int t,a,k,sum=0;
cin>>t>>a>>k;
if(t>0){
if(-k<=a&&a<=t+k){
sum+=abs(a);
sum+=abs(a-t);
}
else{
sum+=abs(t);
sum+=2*abs(t-a);
}
}
else{
if(-k+t<=a&&a<=k){
sum+=abs(a);
sum+=abs(a-t);
}
else{
sum+=abs(t);
sum+=2*abs(t-a);
}
}
cout<<sum<<"\n";
return 0;
}
看到一位✌️的写法。
if(abs(a)<=k||0<=a&&a<=t||t<=a&&a<=0) cout<<abs(a)+abs(a-t)<<endl;
else cout<<abs(t)+2*abs(t-a)<<endl;
M
最多俩 找特例
(在这反省一下我每次写t组测试都不写solve函数导致很多时候有特例都是cout然后continue 在这里有循环就不方便了)
#include <bits/stdc++.h>
#define int long long
using namespace std;
void solve()
{
int n,a[100005],b[100005];
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=n; i++)
cin>>b[i];
if(n==1)
{
cout<<-1<<endl;
return;
}
if(n==2)
{
if(a[1]==b[1])cout<<-1<<endl;
else cout<<1<<endl;
return;
}
for(int i=2; i<=n-1; i++)
{
if(a[i]==b[i])
{
cout<<1<<endl;
return;
}
if(a[i-1]==b[i])
{
cout<<1<<endl;
return;
}
if(a[i+1]==b[i])
{
cout<<1<<endl;
return;
}
if(a[i]==b[i-1])
{
cout<<1<<endl;
return;
}
if(a[i]==b[i+1])
{
cout<<1<<endl;
return;
}
}
cout<<2<<endl;
}
signed main() {
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
标签:std,int,namespace,long,寒假,using,include
From: https://www.cnblogs.com/godcy/p/18037144