第3题:给你n块巧克力,吃完每块需要的时间,问两个人从两边开始吃,最后他们都吃了几块,同时吃的情况,让给左边的女生
分析:直接开两个指针,还有统计两个人所花的时间,直接模拟即可。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100002;
int a[maxn],n,alice,bob,sum1,sum2;
int main()
{
while(~scanf("%d",&n))
{
int i,j,l=1,r=n;
alice=bob=sum1=sum2=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
while(l<=r)
{
if(sum1<=sum2)
{
sum1+=a[l];
alice++;
l++;
}
else
{
sum2+=a[r];
bob++;
r--;
}
}
cout<<alice<<" "<<bob<<endl;
}
return 0;
}
/*
5
1 2 3 2 1
*/
第4题:给你n个人,每次你可以打中2~n-1中的任何一个,与他相邻的也会受到b的伤害,死后也可以继续打,太凶残了= =
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int h[12],n,a,b,key[12],ans,res,s[32],out[12];
void dfs(int idx,int cnt)
{
if(cnt>=res)return;
if(idx==n-1)
{
int t=max((h[n-2]-s[n-2]>=0?(h[n-2]-s[n-2])/b+1:0),(h[n-1]-s[n-1]>=0?(h[n-1]-s[n-1])/a+1:0));
if(cnt+t<res)
{
for(int i=2;i<=idx-1;i++) key[i]=out[i];
key[idx]=t;
res=cnt+t;
}
return;
}
int i,tmp=(h[idx-1]-s[idx-1]>=0?(h[idx-1]-s[idx-1])/b+1:0);
if(cnt+tmp>=res)return;
s[idx-1]+=(tmp-1)*b,s[idx]+=(tmp-1)*a,s[idx+1]+=(tmp-1)*b;
for(i=tmp;i<=15;i++)
{
s[idx-1]+=b,s[idx]+=a,s[idx+1]+=b;
out[idx]=i;
dfs(idx+1,cnt+i);
}
s[idx-1]-=(i-1)*b,s[idx]-=(i-1)*a,s[idx+1]-=(i-1)*b;
}
int main()
{
int i,j,a1,an;
while(~scanf("%d%d%d",&n,&a,&b))
{
a1=an=0;
for(i=1;i<=n;i++) scanf("%d",&h[i]);
memset(key,0,sizeof(key));
memset(s,0,sizeof(s));
memset(out,0,sizeof(out));
res=202;
int t=h[1]/b+1;
h[1]=-1,h[2]-=a*t,h[3]-=b*t;
a1=t;
if(h[n]>=0)
{
t=h[n]/b+1;
h[n]-=b*t,h[n-1]-=a*t,h[n-2]-=b*t;
an=t;
}
dfs(2,0);
if(res==202)res=(a1+an);
else res+=(a1+an);
cout<<res<<endl;
key[2]+=a1;key[n-1]+=an;
for(int i=2;i<=n-1;i++)
{
while(key[i]) {cout<<i<<" ";key[i]--;}
}cout<<endl;
}
return 0;
}
/*
10 9 5
12 14 11 11 14 14 12 15 14 12
10 10 1
10 12 11 4 12 1 15 15 11 12
30
2 2 2 2 2 2 2 2 2 2 2 4 5 5 7 7 7 9 9 9 9 9 9 9 9 9 9 9 9 9
*/
第5题:给你n本书,问最长的区间,使得区间内最高的书与最矮的书的高度差不超过n的最大长度,还有这样长度的区间有哪几个。。。
单调队列 胡乱搞的。。。。。
/*
分析:做了前面几题后,这题容易想到用两个单调队列维护当前最值,
作为判断条件,如果差值大于k了,就去掉较前面的那个队列元素,并把区间头更新为它的标号+1,
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<memory.h>
using namespace std;
const int maxn=100002;
int qmin[maxn],qmax[maxn],n,m,k,key,a[maxn];
struct node
{
int l,r;
}p[maxn];
int main()
{
while(~scanf("%d%d",&n,&k))
{
int i,j,l=0,ans=0;
int lm=0,lx=0,rm=0,rx=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
while(lm<rm&&a[qmin[rm-1]]>a[i]) rm--;
while(lx<rx&&a[qmax[rx-1]]<a[i]) rx--;
qmin[rm++]=i;
qmax[rx++]=i;
while(a[qmax[lx]]-a[qmin[lm]]>k)
{
l=(qmin[lm]<qmax[lx]?qmin[lm++]:qmax[lx++]);//WA 了n 次。。。
}
if(ans<i-l)
{
ans=i-l;
key=1;
p[key].l=l+1;
p[key].r=i;
}
else if(ans==i-l)
{
key++;
p[key].l=l+1;
p[key].r=i;
}
}
printf("%d %d\n",ans,key);
if(ans)
{
for(i=1;i<=key;i++)
{
cout<<p[i].l<<" "<<p[i].r<<endl;
}
}
}
return 0;
}
/*
*/