这里写目录标题
- 1.亮灯时长 [AcWing 3774](https://www.acwing.com/problem/content/submission/3777/)
- 2.兔子跳 [AcWing 3773](https://www.acwing.com/problem/content/3776/)
- 3.A strange lift [洛谷 P1135 ](https://www.luogu.com.cn/problem/P1135)
- 4.{A}+{B}[HDU 1412](https://acm.hdu.edu.cn/showproblem.php?pid=1412)
- 5.海报pla [bzoj 1113](https://vjudge.net/contest/449052#problem/F)
- 6.Bakery [codeforces 707B](https://vjudge.net/contest/449425#problem/F)
1.亮灯时长 AcWing 3774
算法:模拟+前缀和
难度:三级
#include<iostream>
#include<cstdio>
#define N 100010
using namespace std;
int a[N],osum[N],esum[N];
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m,ans=0,maxn=0,maxo,maxe;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
a[n+1]=m;
maxo=maxe=n+1;
for(int i=1;i<=n+1;i++)
{
if(i%2==1)
{
if(i==1)osum[i]=a[i]-a[i-1];
else osum[i]=osum[i-2]+a[i]-a[i-1];
}
else if(i%2==0)
{
esum[i]=esum[i-2]+a[i]-a[i-1];
}
//cout<<a[i]-a[i-1]<<" ";
}
if((n+1)%2)maxe--;
else maxo--;
maxn=osum[maxo];
//cout<<maxn<<endl;
for(int i=1;i<=n+1;i++)
{
ans=0;
if(a[i]-a[i-1]==1)continue;
if(i%2==1)
{
if(i!=1)
ans=osum[i-2];
ans+=a[i]-a[i-1]-1;
ans+=esum[maxe]-esum[i-1];
}
else if(i%2==0)
{
ans=osum[i-1];
ans+=a[i]-a[i-1]-1;
ans+=esum[maxe]-esum[i];
}
//cout<<ans<<endl;
maxn=max(maxn,ans);
}
cout<<maxn<<endl;
}
return 0;
}
2.兔子跳 AcWing 3773
算法:贪心
难度:三级
#include<iostream>
#include<algorithm>
#include<cstdio>
#define N 100010
using namespace std;
int a[N];
int main()
{
int T;
cin>>T;
while(T--)
{
int n,x,ans=0,maxn=0;
bool is_one=false;
cin>>n>>x;
for(int i=1;i<=n;i++)
{
cin>>a[i];
maxn=max(a[i],maxn);
if(a[i]==x)is_one=true;
}
if(is_one)
{
cout<<1<<endl;
continue;
}
else if(x<maxn)
{
cout<<2<<endl;
continue;
}
if(x%maxn)ans=(x/maxn+1);
else ans=x/maxn;
cout<<ans<<endl;
}
return 0;
}
3.A strange lift 洛谷 P1135
算法:最短路 bfs 队列
难度:二级
#include <iostream>
#include <queue>
#include<cstring>
using namespace std;
typedef struct {
int floor;
int pushcount;
} QElement;
queue<QElement> q;
int n,a,b;
int s[1000];
int t[1000]={0};
int main()
{
QElement e1,e2;
int i;
while(cin >> n&&n!=0)
{
while(!q.empty())q.pop();
memset(s,0,sizeof(s));
memset(t,0,sizeof(t));
cin>>a>>b;
for (i=1; i<=n; i++) cin >> s[i];
e1.floor=a;
e1.pushcount=0;
q.push(e1);
t[a]=1;
while (!q.empty())
{
e2=q.front();
q.pop();
if (e2.floor==b) break;
i=e2.floor+s[e2.floor];
if (i<=n && t[i]==0)
{
e1.floor=i;
e1.pushcount=e2.pushcount+1;
q.push(e1);
t[i]=1;
}
i=e2.floor-s[e2.floor];
if (i>=1 && t[i]==0)
{
e1.floor=i;
e1.pushcount=e2.pushcount+1;
q.push(e1);
t[i]=1;
}
}
if (e2.floor==b) cout << e2.pushcount<<endl;
else cout << -1<<endl;
}
}
4.{A}+{B}HDU 1412
算法:STL
难度:一级
注释:注意的是PE问题,空格的输出应符合要求
#include<iostream>
#include<algorithm>
#define N 10010
using namespace std;
int n,m,a[N],b[N];
int main()
{
int i,j;
while(cin>>n>>m)
{
for(i=0;i<n;i++)
cin>>a[i];
for(j=0;j<m;j++)
cin>>b[j];
sort(a,a+n);
sort(b,b+m);
j=0,i=0;
while(i<n&&j<m)
{
if(a[i]<b[j])
{
cout<<a[i]<<" ";
i++;
}
else if(a[i]>b[j])
{
cout<<b[j]<<" ";
j++;
}
else if(a[i]==b[j])
{
cout<<a[i];
i++;
j++;
if(i==n&&j==m)cout<<endl;
else cout<<" ";
}
}
while(i<n)
if(i==n-1)cout<<a[i++]<<endl;
else cout<<a[i++]<<" ";
while(j<m)
if(j==m-1)cout<<b[j++]<<endl;
else cout<<b[j++]<<" ";
}
return 0;
}
5.海报pla bzoj 1113
算法:单调栈
难度:二级
注释:只需要考虑高度即可,宽度可以省略,
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 250010
using namespace std;
struct dandiao_stack{
int s[N],num[N];
int top;
dandiao_stack()
{
memset(s,0,sizeof(s));
memset(num,0,sizeof(num));
top=0;
}
void push(int x,int n)
{
while(top>0&&x<s[top])
top--;
s[++top]=x;
num[top]=n;
}
void pop()
{
top--;
}
}f;
int a[N],i;
int main()
{
int n,x,ans=0;
cin>>n;
for(i=1;i<=n;i++)
cin>>x>>a[i];
for(i=1;i<=n;i++)
{
f.push(a[i],i);
f.pop();
if(f.s[f.top]!=a[i])ans++;
f.push(a[i],i);
}
cout<<ans;
return 0;
}
6.Bakery codeforces 707B
算法:模拟,最小生成树思想
难度:三级
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 2010
#define M 5000010
#define ll long long
using namespace std;
struct point{
int x,y;
}p[N];
struct edge{
int u,v;
ll w;
}e[M],con[N];
int c[N],k[N],f[N],cnt;
int vn=0,build[N],en=0;
ll ans;
bool cmp(const edge &a,const edge &b)
{
return a.w<b.w;
}
void init(int n)
{
for(int i=0;i<=n;i++)
f[i]=i;
}
int find(int u)
{
if(f[u]==u)return u;
return find(f[u]);
}
ll dis(int i,int j)
{
return (abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y));
}
int main()
{
int n,m=0;
cin>>n;
init(n);
for(int i=1;i<=n;i++)
cin>>p[i].x>>p[i].y;
for(int i=1;i<=n;i++)
{
cin>>c[i];
e[m].u=i;
e[m].v=0;
e[m++].w=c[i];
}
for(int i=1;i<=n;i++)
cin>>k[i];
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
e[m].u=i;
e[m].v=j;
//cout<<i<<" "<<j<<" "<<dis(i,j)<<endl;
e[m++].w=dis(i,j)*(k[i]+k[j]);
}
}
//cout<<m<<endl<<endl;
sort(e,e+m,cmp);
for(int i=0;i<m;i++)
{
//cout<<e[i].u<<" "<<e[i].v<<" "<<e[i].w<<endl;
int u=find(e[i].u);
int v=find(e[i].v);
if(u!=v)
{
if(e[i].v==0)
{
build[++vn]=e[i].u;
}
else
{
con[++en].u=e[i].u;
con[en].v=e[i].v;
}
f[u]=v;
ans+=e[i].w;
cnt++;
}
if(cnt==n)break;
}
cout<<ans<<endl;
cout<<vn<<endl;
for(int i=1;i<=vn;i++)
cout<<build[i]<<" ";
cout<<endl<<en<<endl;
for(int i=1;i<=en;i++)
cout<<con[i].u<<" "<<con[i].v<<endl;
return 0;
}