3.27周四晚训练 -- 河西301 总结
AC_zzh2013 · · 个人记录
T1
已AC
T2
错误代码
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int sum[100005];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;++i)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
int maxx=INT_MIN;
sort(a+1,a+n+1);
for(int i=1;i<=n;++i)
{
for(int j=i;j<=n;++j)
{
if(a[j]-a[i]<=k)
{
maxx=max(maxx,sum[j]-sum[i-1]);
}
}
}
cout<<maxx;
return 0;
}
错误原因
由于使用了sort排序的原因,导致前缀和需要在排序后使用,但是错误代码中却是在前面进行前缀和。
AC代码
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int sum[100005];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;++i)
{
cin>>a[i];
}
int maxx=INT_MIN;
sort(a+1,a+n+1);
for(int i=1;i<=n;++i)
{
sum[i]=sum[i-1]+a[i];
}
for(int i=1;i<=n;++i)
{
for(int j=i;j<=n;++j)
{
if(a[j]-a[i]<=k)
{
maxx=max(maxx,sum[j]-sum[i-1]);
}
}
}
cout<<maxx;
return 0;
}
T3
错误代码
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int b[100005];
int ans[100005];
int vis[1005][1005];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;++i)
{
int x,y;
cin>>x>>y;
vis[x][y]=1;
vis[y][x]=1;
}
int maxx=INT_MIN;
for(int i=1;i<=n;++i)//枚举第一个数字
{
int cnt=0;
for(int j=1;j<=n;++j)//枚举尝试剩下符合条件的数字
{
bool f=1;
for(int k=1;k<=n;++k)//check判断是否合法
{
if(b[k]==1 and vis[j][k]==1)
{
f=0;
break;
}
}
if(f)
{
b[j]=1;
cnt++;
}
}
if(maxx<cnt)
{
for(int i=1;i<=n;++i)
{
ans[i]=b[i];
}
maxx=cnt;
}
memset(b,0,sizeof(b));
}
cout<<maxx<<endl;
for(int i=1;i<=n;++i)
{
cout<<ans[i]<<' ';
}
return 0;
}
错误原因
由于错误代码的方法是可以选择的必选,不能选的跳过,可是可以选的不一定必须,所以正确的方法是dfs选和不选。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[100005];
int box[100005];
int ans[100005];
int vis[1005][1005];
int maxx=INT_MIN;
int n,m;
void dfs(int cur,int cnt)
{
if(cur>n)
{
if(maxx<cnt)
{
for(int i=1;i<=n;++i)
{
ans[i]=box[i];
}
maxx=cnt;
}
return ;
}
bool f=0;
for(int i=1;i<cur;++i)
{
if(vis[cur][i] and box[i])
{
f=1;
break;
}
}
if(f)
{
dfs(cur+1,cnt);
}
else
{
box[cur]=1;
dfs(cur+1,cnt+1);
box[cur]=0;
dfs(cur+1,cnt);
}
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=m;++i)
{
int x,y;
cin>>x>>y;
vis[x][y]=1;
vis[y][x]=1;
}
dfs(1,0);
cout<<maxx<<endl;
for(int i=1;i<=n;++i)
{
cout<<ans[i]<<' ';
}
return 0;
}
T4
错误代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int t;
cin>>t;
while(t--)
{
int ans,n,y,r,s,f,m;
cin>>ans>>n>>y>>r>>s>>f>>m;
int sum=1;
ans--;
while(ans)
{
ans--;
sum*=2;
}
sum--;
m+=sum;
f+=m/60;
m%=60;
s+=f/60;
f%=60;
r+=s/24;
s%=24;
while(1)
{
if(n%4==0 and n%100!=0 or n%400==0)
{
if(r<366)
{
break;
}
r-=366;
n++;
}
else
{
if(r<365)
{
break;
}
r-=365;
n++;
}
}
while(1)
{
if(y==1 or y==3 or y==5 or y==7 or y==8 or y==10 or y==12)
{
if(r<31)
{
break;
}
r-=31;
y++;
if(y==13)
{
n++;
y=1;
}
}
if(y==4 or y==6 or y==9 or y==11)
{
if(r<30)
{
break;
}
r-=30;
y++;
}
if(y==2)
{
if(n%4==0 and n%100!=0 or n%400==0)
{
if(r<29)
{
break;
}
r-=29;
}
else
{
if(r<28)
{
break;
}
r-=28;
}
}
}
cout<<n<<' '<<y<<' '<<r<<' '<<s<<' '<<f<<' '<<m<<'\n';
}
return 0;
}
错误原因
时分秒转换后可以为0,但是年月日不行。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void check(int x)
{
if(x%400==0 or x%100!=0 and x%4==0)
{
a[2]=29;
}
else
{
a[2]=28;
}
return ;
}
signed main()
{
int t;
cin>>t;
while(t--)
{
int ans,n,y,r,s,f,m;
cin>>ans>>n>>y>>r>>s>>f>>m;
int sum=1;
ans--;
while(ans)
{
ans--;
sum*=2;
}
sum--;
m+=sum;
f+=m/60;
m%=60;
s+=f/60;
f%=60;
r+=s/24;
s%=24;
check(n);
while(r>a[y])
{
r-=a[y];
y++;
if(y==2)
{
check(n);
}
if(y>12)
{
y=1;
n++;
}
}
cout<<n<<' '<<y<<' '<<r<<' '<<s<<' '<<f<<' '<<m<<'\n';
}
return 0;
}