3.4下午测试 — 407
P2335 位图
用一个数组把所有黑点存起来就能水过去了,但是我是直接枚举的,所以70
ACcode:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 155;
bool a[N][N];
int n, m;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
cin >> a[i][j];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i][j])
{
cout << 0 << ' ';
continue;
}
int ans = INT_MAX;
for (int x = 1; x <= n; x++)
{
for (int y = 1; y <= m; y++)
{
if (a[x][y])
ans = min(ans, abs(i - x) + abs(j - y));
}
}
cout << ans << ' ';
}
cout << "\n";
}
return 0;
}P1466 集合 Subset Sums
打表少了一个39,干掉15分
ACcode:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int ans[45]={0,0,0,1,1,0,0,4,7,0,0,35,62,0,0,361,657,0,0,4110,7636,0,0,49910,93846,0,0,632602,1199892,0,0,8273610,15796439,0,0,110826888,212681976,0,0,1512776590};
int n;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
cout<<ans[n];
return 0;
}虽然这是打表,但是可以帮助我们拿很多分
其实这题就是背包,所以:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105 , M = 1e4 + 5;
int n , a[N] , dp[N][M];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
a[i] = i;
int ans = n * (n + 1) / 2;
if (ans % 2 == 1)
{
cout << 0;
return 0;
}
dp[0][0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= ans; j++)
{
if (j >= a[i])
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - a[i]];
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][ans / 2];
return 0;
}P1466 集合 Subset Sums
复杂递归
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[105][105];
int cnt[105];
int maxi,n;
bool vis[105];
bool own[105];
void dfs(int u)
{
if(vis[u])
return ;
vis[u]=1;
for(int i=maxi;i;i--)
{
cnt[i]+=a[u][i];
if(cnt[i]>50)
{
own[i]=1;
dfs(i);
}
}
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
int u,v,num;
cin>>u>>v>>num;
a[u][v]=num;
maxi=max(maxi,max(u,v));
}
for(int i=1;i<=maxi;i++)
{
memset(vis,0,sizeof vis);
memset(own,0,sizeof own);
memset(cnt,0,sizeof cnt);
dfs(i);
for(int j=1;j<=maxi;j++)
{
if(own[j] && i!=j)
cout<<i<<" "<<j<<"\n";
}
}
return 0;
}