block ——4115
思路:蒟蒻我只想到dfs,每次找到[L,R]中的min,将其为0,则剩下的变成两个区间…然而次题贪心即可…
flower ——4116
思路:经典的dp,然而最后写成了贪心,O(n)。
泛泛而言,一组数据一定是一个大波浪的形状,则最优的情况便是找的谷峰谷底。
#include<bits/stdc++.h>
#define REP(i,f,t) for(int i=(f),i##_end_=(t);i<=i##_end_;i++)
#define DREP(i,f,t) for(int i=(f),i##_end_=(t);i>=i##_end_;i--)
#define LL long long
#define INF 0x3f3f3f3f
#define N 100005
using namespace std;
int n,top,ans;
int h[N];
int main(){
// freopen("flower.in","r",stdin);
// freopen("flower.out","w",stdout);
cin>>n;
REP(i,1,n)scanf("%d", &h[i]);
REP(i,1,n)if(h[top]!=h[i])h[++top]=h[i];
REP(i,1,top)ans+=(i==1)||(i==top)||(h[i-1]<h[i]&&h[i]>h[i+1])||(h[i-1]>h[i]&&h[i]<h[i+1]);
cout<<ans<<endl;
return 0;
}
puzzle ——4117
思路:暴力bfs60,这种题比赛只能弃坑啊,debug要一年…
然而,正解是bfs+最短路(Dij,SPFA)那就要预处理一些东西了,即空格周围的点的移动。
#include<bits/stdc++.h>
#define lnt register int
#define REP(i,f,t) for(lnt i=(f),i##_end_=(t);i<=i##_end_;i++)
#define DREP(i,f,t) for(lnt i=(f),i##_end_=(t);i>=i##_end_;i--)
#define LL long long
#define INF 0x3f3f3f3f
#define clear(x,val) memset(x,val,sizeof(x))
#define N 32
using namespace std;
void Rd(int &res){
res=0;char c;
while(c=getchar(),c<48);
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>47);
}
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int mp[N][N],n,m,q;
struct Node{
int x,y;
};
Node fk;
struct node{
int ax,ay,bx,by;
int step;
bool operator<(const node &cmp)const{
return step>cmp.step;
}
};
bool mark[N][N][N][N];
int dp[N][N][4][4],dis[N][N];
bool check(int x,int y){return x<=n&&x&&y<=m&&y&&mp[x][y];}
int direction(int x1,int y1,int x2,int y2){//node1->node2
lnt xd=x2-x1,yd=y2-y1;
lnt d;
for(d=0;d<4;d++)if(dx[d]==xd&&dy[d]==yd)return d;
return -1;
}
void bfs(node S){
mp[S.ax][S.ay]=0;
clear(dis,-1);
dis[S.bx][S.by]=0;
queue<Node>Q;
Q.push((Node){S.bx,S.by});
while(!Q.empty()){
Node now=Q.front();Q.pop();
REP(i,0,3){
lnt nx=now.x+dx[i];
lnt ny=now.y+dy[i];
if(!check(nx,ny) || ~dis[nx][ny])continue;
dis[nx][ny]=dis[now.x][now.y]+1;
Q.push((Node){nx,ny});
}
}
mp[S.ax][S.ay]=1;
}
void Init(){
REP(i,1,n)
REP(j,1,m){
if(!mp[i][j])continue;
REP(k,0,3){
Node nxt=(Node){i+dx[k],j+dy[k]};
if(!check(nxt.x,nxt.y))continue;
bfs((node){nxt.x,nxt.y,i,j,0});
REP(l,0,3){
if(!~dis[nxt.x+dx[l]][nxt.y+dy[l]])dp[i][j][k][l]=-1;
else dp[i][j][k][l]=dis[nxt.x+dx[l]][nxt.y+dy[l]]+1;
}
}
}
}
int Dij(node S){
if(S.ax==fk.x&&S.ay==fk.y)return S.step;
priority_queue<node>Q;
bfs(S);
REP(k,0,3){
lnt x=S.ax+dx[k],y=S.ay+dy[k];
if(~dis[x][y])Q.push((node){S.ax,S.ay,x,y,dis[x][y]});
}
while(!Q.empty()){
node now=Q.top();Q.pop();
if(now.ax==fk.x&&now.ay==fk.y)return now.step;
if(mark[now.ax][now.ay][now.bx][now.by])continue;
mark[now.ax][now.ay][now.bx][now.by]=true;
lnt k=direction(now.bx,now.by,now.ax,now.ay);
if(k==-1)continue;
REP(d,0,3){
int x=now.ax+dx[d],y=now.ay+dy[d];
if(d!=direction(now.ax,now.ay,x,y))continue;
if(!check(x,y)||!~dp[now.bx][now.by][k][d])continue;
Q.push((node){x,y,now.ax,now.ay,now.step+dp[now.bx][now.by][k][d]});
}
}
return -1;
}
int main(){
// freopen("puzzle.in","r",stdin);
// freopen("puzzle.out","w",stdout);
cin>>n>>m>>q;
REP(i,1,n)REP(j,1,m)Rd(mp[i][j]);
clear(dp,-1);
Init();
while(q--){
clear(mark,0);
node S;S.step=0;
Rd(S.bx),Rd(S.by),Rd(S.ax),Rd(S.ay);
Rd(fk.x),Rd(fk.y);
printf("%d\n",Dij(S));
}
}
小结:现在这种离线赛总算是找到一些感觉了,希望继续保持!