目录
1.ASC
答案:76
2、修改数组(19年省赛——3月28日)
注意:初始化的时候初始到1e6+5
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
int n,a[100005],p[1000010];
void init() {
for(int i=1; i<=1000005; i++)
p[i]=i;
}
int find(int x) {
return p[x]==x ? x : p[x]=find(p[x]);
}
int main() {
init();
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=n; i++) {
int x=find(a[i]);
p[x]++;
cout<<x<<" ";
}
return 0;
}
3、大胖子走迷宫(19年国赛——3月25日)
注意:5*5 3*3可以在原地不动,需要特判,起点终点不是两个角。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
const int dr[]= {0,0,-1,1};
const int dc[]= {1,-1,0,0};
int S,n,k,vis[305][305];
char a[305][305];
struct node {
int x,y,t;
node (int x,int y,int t):x(x),y(y),t(t) {}
};
bool inside(int xx,int yy) {
if((xx+S/2)>=1 && (xx+S/2)<=n && (yy+S/2)>=1 && (yy+S/2)<=n) return true;
else return false;
}
bool judge(int xx,int yy) {
for(int i=xx-S/2; i<=xx+S/2; i++)
for(int j=yy-S/2; j<=yy+S/2; j++)
if(a[i][j]=='*') return false;
return true;
}
void bfs() {
queue<node> q;
node u(3,3,0);
vis[3][3]=1;
q.push(u);
while(!q.empty()) {
node u=q.front();
q.pop();
if(u.x==n-2 && u.y==n-2) {
cout<<u.t<<endl;
return;
}
//判定体型
if(u.t<k) S=5;
if(u.t>=k && u.t<2*k) S=3;
if(u.t>=2*k) S=1;
if(S!=1) q.push(node(u.x, u.y, u.t + 1));
for(int i=0; i<4; i++) {
int nx=u.x+dr[i];
int ny=u.y+dc[i];
if(inside(nx,ny) && judge(nx,ny) && !vis[nx][ny]) {
vis[nx][ny]=1;
node v(nx, ny, u.t + 1);
q.push(v);
}
}
}
}
int main() {
cin>>n>>k;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
cin>>a[i][j];
bfs();
return 0;
}
4、数字三角形(20年省赛——3月19日)
注意:虽然是一道线性dp问题,还是一个经典题,可是有要求:向左下走的次数与向右下走的次数相差不能超过 1。
数字三角形可以自顶向下划分,也可以自底向上划分。不过这题有要求,所以自顶向下更容易控制,直接取最后一行的中间元素即可
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
int dp[105][105],a[105][105],n;
int maxn;
int main() {
cin>>n;
for(int i=1; i<=n; i++)
for(int j=1; j<=i; j++)
cin>>a[i][j];
dp[1][1]=a[1][1];
for(int i=2; i<=n; i++)
for(int j=1; j<=i; j++)
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+a[i][j];
if(n%2) cout<<dp[n][n/2+1];
else cout<<max(dp[n][n/2],dp[n][n/2+1]);
return 0;
}