3-7 周五晚上训练 410 总结
tanruiqing · · 个人记录
得分:
错题分析:
T2:
错误点:数组开小了,
AC代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
bool ch[205][205];
int tong[205][205];
int wx[1000005],wy[1000005];int l;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
cin >> ch[i][j];
if(ch[i][j] == 1)wx[++l] = i,wy[l] = j;
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
if(ch[i][j] == 0){
int minn = 1e18;
for(int k = 1 ; k <= l ; k++){
minn = min(minn,abs(i - wx[k]) + abs(j - wy[k]));
}
tong[i][j] = minn;
}
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
cout << tong[i][j] << " ";
}
cout << '\n';
}
return 0;
}
T4:
错误点:DFS想得太复杂了,所以自己在考试的时候写错了……
AC代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int x[105],y[105];
int tong[105][105];
int box1[105],box2[105];
int l,ans;
bool vis[105];
void dfs(int setp){
if(setp > n){
ans++;
return ;
}
for(int i = 1 ; i <= n ; i++){
if(vis[i] == 0 && (x[setp] == i || y[setp] == i)){
vis[i] = 1;
dfs(setp + 1);
vis[i] = 0;
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> x[i] >> y[i];
}
dfs(1);
cout << ans;
return 0;
}
T5
错误点:也许是DFS学的太废了,打了个表全部WA了……
正解是背包DP,我们可以将
DP四步法:
- 状态表示:我们设
dp_i 为从第一个数到第i 个数可以分成两个总和一样的集合的方案数。 - 状态转移方程:对于前
i 个数有两种选择:-
- 选:那么答案就是前
i-1,j-a_i 个数的方案数。
- 选:那么答案就是前
-
- 不选:那么答案就是前
i-1 个数的方案数。
- 不选:那么答案就是前
- 所以,对于前
i 个数可行的方案数的答案就是dp_{i-1,j}+dp_{i-1,j-a_i} 。
-
- 答案:
dp_{n,sum/2}/2 。 - 初始化:对于前
0 个数可行的方案数就是1 (不选)。
AC代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[100005];
int ans;
int sum;
int dp[105][1005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
sum = (1 + n) * n / 2;
if(sum % 2 != 0){
cout << 0;
return 0;
}
dp[0][0] = 1;
for(int i = 1 ; i <= n ; i++){
for(int j = 0 ; j <= sum / 2 ; j++){
dp[i][j] = dp[i - 1][j];
if(j > a[i]){
dp[i][j] += dp[i - 1][j - i];
}
}
}
cout << dp[n][sum / 2] / 2;
return 0;
}
T6
错误点:偏分的代码,结果还水过了一个点。
正解:DFS。我们可以每次对于一个公司
AC代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[1005][1005];
bool vis[1005];
int cnt[1005];
int own[1005];
int maxx;
void dfs(int u){
if(vis[u] == true){
return ;
}
vis[u] = true;
for(int i = maxx ; i >= 1 ; i--){
cnt[i] += a[u][i];
if(cnt[i] > 50){
own[i] = true;
dfs(i);
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1,u,v,s ; i <= n ; i++){
cin >> u >> v >> s;
a[u][v] = s;
maxx = max(maxx,max(u,v));
}
for(int i = 1 ; i <= maxx ; i++){
memset(vis , 0 , sizeof vis);
memset(cnt , 0 , sizeof cnt);
memset(own , 0 , sizeof own);
dfs(i);
for(int j = 1 ; j <= maxx ; j++){
if(own[j] == true && i != j){
cout << i << " " << j << '\n';
}
}
}
return 0;
}
End