contest 1.22
D.菱菱的旅途
排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<cstdio> #include<algorithm> typedef long long ll; using namespace std; struct Node{ ll v,id; }node[1000005]; bool cmp(Node a,Node b){ if(a.v!=b.v) return a.v>b.v; else return a.id>b.id; } int main() { ll n;scanf("%lld",&n); for(ll i=1;i<=n;i++) scanf("%lld",&node[i].v),node[i].id=i; sort(node+1,node+1+n,cmp); ll max_id=-1,min_id=n+1,ans=0; for(ll i=1;i<=n;i++){ max_id=max(max_id,node[i].id); min_id=min(min_id,node[i].id); ll now=node[i].v*max(max_id-node[i].id+1,node[i].id-min_id+1); ans=max(ans,now); } printf("%lld\n",ans); return 0; }
A.桥桥的告白
模拟
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char name[1005][35]; struct Node{ int cnt,id; }node[1005]; int main() { int n;scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%s",name[i]); node[i].cnt=0; node[i].id=i; } int m;scanf("%d",&m); for(int i=1;i<=m;i++){ int u,v;scanf("%d%d",&u,&v); node[u].cnt=node[v].cnt+1; node[u].id=node[v].id; } for(int i=1;i<=node[1].cnt;i++){ printf("I_love_"); } puts(name[node[1].id]); return 0; }
E.鑫鑫的算术
A.慢跑
单调队列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<cstdio> #include<stack> #include<algorithm> typedef long long ll; using namespace std; ll p[100005],v[100005],d[100005]; stack<ll> stk; int main() { ll n,t;scanf("%lld%lld",&n,&t); for(ll i=1;i<=n;i++){ scanf("%lld%lld",&p[i],&v[i]); d[i]=p[i]+v[i]*t; } ll ans=n; for(ll i=1;i<=n;i++){ while(!stk.empty()){ ll top=stk.top(); if(top>=d[i]) { ans--; stk.pop(); } else break; } stk.push(d[i]); } printf("%lld\n",ans); return 0; }
D.迷宫问题
dfs暴力
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<cstdio> #include<cstring> typedef long long ll; using namespace std; ll n; char Map[105][105]; ll vis[105][105]; int dire[8][2]={-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1}; bool in(ll x,ll y){return x>=1&&x<=n&&y>=1&&y<=n;} ll dfs(ll x,ll y){ if(x==1&&y==n) return 1; else{ ll ret=0; for(ll i=0;i<8;i++){ ll nx=x+dire[i][0]; ll ny=y+dire[i][1]; if(in(nx,ny)&&Map[nx][ny]=='0'&&!vis[nx][ny]) { vis[nx][ny]=1; ret+=dfs(nx,ny); vis[nx][ny]=0; } } return ret; } } int main() { scanf("%lld",&n); for(ll i=1;i<=n;i++){ for(ll j=1;j<=n;j++){ scanf(" %c",&Map[i][j]); } } ll ans=0; if(Map[1][1]=='0'){ vis[1][1]=1; ans=dfs(1,1); } printf("%lld\n",ans); return 0; }
E.星区
并查集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<cstdio> #include<cmath> using namespace std; int x,y,z,m; int d[55][55][55]; int id[55][55][55],tot=0; int fa[125005]; int ans; inline int get_id(int a,int b,int c){return id[a][b][c];} inline bool in(int a,int b,int c){return a>=1&&a<=x&&b>=1&&b<=y&&c>=1&&c<=z;} inline int findd(int x){ if(x==fa[x]) return x; else return fa[x]=findd(fa[x]); } inline void cal(int a,int b,int c){ int now=d[a][b][c]; int id1=get_id(a,b,c); int fa1=findd(id1); if(in(a-1,b,c)&&abs(d[a-1][b][c]-now)<=m){ int id2=get_id(a-1,b,c); int fa2=findd(id2); if(fa1!=fa2) fa[fa2]=fa1,ans--; } if(in(a,b-1,c)&&abs(d[a][b-1][c]-now)<=m){ int id2=get_id(a,b-1,c); int fa2=findd(id2); if(fa1!=fa2) fa[fa2]=fa1,ans--; } if(in(a,b,c-1)&&abs(d[a][b][c-1]-now)<=m){ int id2=get_id(a,b,c-1); int fa2=findd(id2); if(fa1!=fa2) fa[fa2]=fa1,ans--; } } int main() { scanf("%d%d%d",&x,&y,&z); scanf("%d",&m); for(int i=1;i<=x;i++){ for(int j=1;j<=y;j++){ for(int k=1;k<=z;k++){ scanf("%d",&d[i][j][k]); id[i][j][k]=++tot; fa[tot]=tot; } } } ans=x*y*z; for(int i=1;i<=x;i++){ for(int j=1;j<=y;j++){ for(int k=1;k<=z;k++){ cal(i,j,k); //printf("(%d,%d,%d) %d\n",i,j,k,ans); } } } printf("%d\n",ans); return 0; } /* 2 2 2 0 5 1 1 1 2 2 2 2 */
B.游济南
ans=2*总权值-距1的最远距离
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<cstdio> #include<cstring> #include<queue> typedef long long ll; using namespace std; struct Edge{ll v,w;}; vector<Edge> edge[50005]; ll vis[500005]; ll maxn=0; void dfs(ll x,ll tot){ maxn=max(maxn,tot); ll siz=edge[x].size(); vis[x]=1; for(ll i=0;i<siz;i++){ ll v=edge[x][i].v; ll w=edge[x][i].w; if(vis[v]) continue; dfs(v,tot+w); } } int main() { ll n;scanf("%lld",&n); ll tot=0; for(ll i=1;i<=n-1;i++){ ll u,v,w;scanf("%lld%lld%lld",&u,&v,&w); edge[u].push_back(Edge{v,w}); edge[v].push_back(Edge{u,w}); tot+=w; } dfs(1,0); printf("%lld\n",tot*2-maxn); return 0; }
C.飞行
单源最短路
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<cstdio> #include<vector> #include<queue> #include<cstring> #define inf 0x3f3f3f3f3f3f3f3f typedef long long ll; using namespace std; ll r,s,p,n,m; struct Edge{ll v,len;}; vector<Edge> edge[40005]; struct Node{ ll v,dis; bool operator <(const Node& b)const{ return dis>b.dis; } }; ll ans[3][40005]; void dijkstra(ll s){ priority_queue<Node> q; q.push(Node{s,0}); if(s==n) s=0; memset(ans[s],0x3f3f,sizeof(ans[s])); while(!q.empty()){ Node now=q.top();q.pop(); ll u=now.v,dis=now.dis; if(dis>=ans[s][u]) continue; ans[s][u]=dis; ll siz=edge[u].size(); for(ll i=0;i<siz;i++){ ll v=edge[u][i].v; ll len=edge[u][i].len; if(dis+len<ans[s][v]) q.push(Node{v,dis+len}); } } } int main() { scanf("%lld%lld%lld%lld%lld",&r,&s,&p,&n,&m); for(ll i=1;i<=m;i++){ ll u,v;scanf("%lld%lld",&u,&v); edge[u].push_back(Edge{v,1}); edge[v].push_back(Edge{u,1}); } dijkstra(1); dijkstra(2); dijkstra(n); ll tmp1=ans[1][n]*r+ans[2][n]*s; ll tmp2=inf; for(ll i=1;i<=n;i++){ ll now=ans[1][i]*r+ans[2][i]*s+ans[0][i]*p; tmp2=min(tmp2,now); } printf("%lld\n",min(tmp1,tmp2)); return 0; }
F.小迟的数字I
转载请注明出处:https://www.cnblogs.com/lllxq/