比赛的时候就是看不懂题
比赛后发现就是个冒泡排序!?
A C C o d e : AC~Code: AC Code:
#include
#include
#include
using namespace std;
int n,a[1110],b[1110],z,ans,w[1110];
int main()
{
freopen("laundry.in","r",stdin);
freopen("laundry.out","w",stdout);
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i]>>b[i];
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(a[i]==b[j])
w[i]=j;
for(int i=1; i<=n-1; i++) //冒泡排序
for(int j=1; j<=n-i; j++)
if(w[j]>w[j+1])
{
z=w[j];
w[j]=w[j+1];
w[j+1]=z;
ans++;
}
cout<<ans;
return 0;
}
感觉自己比赛时打的代码是对的,
但是只得了 40 p t s 40pts 40pts
赛后也不知道自己原来错哪儿了,
但其实正解很简单
前缀和一下就好了。
A C C o d e AC~Code AC Code
#include
#include
#include
using namespace std;
int n,p,c,a[1110],cpn;
int f[1110],ans;
int main()
{
freopen("meet.in","r",stdin);
freopen("meet.out","w",stdout);
cin>>n>>p>>c;
for(int i=1; i<=n; i++)
{
cin>>cpn;
a[cpn+1]++;
}
for(int i=1; i<=p; i++)
f[i]=f[i-1]+a[i];
for(int i=1; i<=p; i++)
for(int j=1; j<=p; j++)
{
if(f[i]-f[j]<=c)
ans=max(ans,i-j);
}
cout<<ans;
return 0;
}
比赛时打了个暴力
错了
正解:模拟!(差点AC)
A C C o d e AC~Code AC Code
#include
#include
#include
#include
using namespace std;
long long head,tail,ans;
long long l,r,n;
char c;
struct node
{
long long l,r;
}a[110010];
bool cmp(const node&k,const node&l)
{
return ((k.l<l.l)||(k.l==l.l)&&(k.r<l.r));
}
int main()
{
freopen("paint.in","r",stdin);
freopen("paint.out","w",stdout);
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>l>>c;
if(c=='R')
l=r-l;
else
l=r+l;
a[i].l=min(l,r);
a[i].r=max(l,r);
r=l;
}
sort(a+1,a+1+n,cmp);
head=a[1].l;
tail=a[1].r;
for(int i=2; i<=n; i++)
{
if(a[i].r<head)continue;
if(a[i].r<=tail)
{
ans=ans+a[i].r-max(a[i].l,head);
head=a[i].r;
}
else if(a[i].l>tail)
{
head=a[i].l;
tail=a[i].r;
}
else if(a[i].l<head)
{
ans=ans+tail-head;
head=tail;
tail=a[i].r;
}
else
{
ans=ans+tail-a[i].l;
head=tail;
tail=a[i].r;
}
}
cout<<ans;
return 0;
}
这道题可称之为 d f s dfs dfs模板题
比赛时漏了个判断
0 p t s 0pts 0pts
A C C o d e AC~Code AC Code
#include
#include
#include
using namespace std;
int dx[5]={0,1,0,-1,0};
int dy[5]={0,0,1,0,-1};
int n,b[6][6],a[6][6],l=1,r;
int ans;
char c;
void dfs(int x,int y,int l,int r)
{
if(l==r)
{
if(l*2>ans)
ans=l+r;
return;
}
for(int i=1; i<=4; i++)
{
if(x+dx[i]>0&&x+dx[i]<=n&&y+dy[i]>0&&y+dy[i]<=n&&b[x+dx[i]][y+dy[i]]==0)
{
b[x+dx[i]][y+dy[i]]=1;
if(a[x+dx[i]][y+dy[i]]==1&r==0)
dfs(x+dx[i],y+dy[i],l+1,r);
if(a[x+dx[i]][y+dy[i]]==0)
dfs(x+dx[i],y+dy[i],l,r+1);
b[x+dx[i]][y+dy[i]]=0;
}
}
}
int main()
{
freopen("hshoe.in","r",stdin);
freopen("hshoe.out","w",stdout);
cin>>n;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
cin>>c;
if(c==')')
a[i][j]=0;
if(c=='(')
a[i][j]=1;
}
if(a[1][1]==0)
{
cout<<0;
return 0;
}
b[1][1]=1;
dfs(1,1,1,0);
cout<<ans;
return 0;
}
0 + 40 + 0 + 0 = 40 p t s 0+40+0+0=40pts 0+40+0+0=40pts