A.Separated Lunch
Key:DFS
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int n;
int a[N];
int ans=MAX;
void dfs(int p,int x,int y){
if(p>n){
ans=min(ans,max(x,y));
return ;
}
dfs(p+1,x+a[p],y);
dfs(p+1,x,y+a[p]);
return ;
}
void solve(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
dfs(1,0,0);
cout<<ans<<endl;
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
B.数的划分
Key:DFS+剪枝
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int n,m;
int ans=0;
void dfs(int num,int p,int now){
if(p==0){
if(num!=0)return ;
ans++;
return ;
}
if(num<now)return ;
for(int i=now;i<=num;i++){
dfs(num-i,p-1,max(now,i));
}
return ;
}
void solve(){
cin>>n>>m;
dfs(n,m,1);
cout<<ans<<endl;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
C.马的遍历
Key:BFS
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 405, MAX = 1e9, INF = -1e9;
int n,m,x,y;
int g[N][N];
int dx[8]={-1,-2,-2,-1,1,2,2,1};
int dy[8]={2,1,-1,-2,2,1,-1,-2};
void bfs(){
memset(g,-1,sizeof g);
g[x][y]=0;
queue<PII> q;
q.push({x,y});
while(!q.empty()){
PII t=q.front();
q.pop();
for(int i=0;i<8;i++){
int x0=t.first+dx[i];
int y0=t.second+dy[i];
if(x0>=1&&x0<=n&&y0>=1&&y0<=m){
if(g[x0][y0]==-1){
g[x0][y0]=g[t.first][t.second]+1;
q.push({x0,y0});
}
else g[x0][y0]=min(g[x0][y0],g[t.first][t.second]+1);
}
}
}
return ;
}
void solve(){
cin>>n>>m>>x>>y;
bfs();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<g[i][j]<<" ";
}
cout<<endl;
}
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
D.采药
Key:动态规划—背包问题—01背包
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 105, MAX = 1e9, INF = -1e12, M = 1005;
int n,V;
int v[N];
int w[N];
int dp[N][M];
void solve(){
cin>>V>>n;
for(int i=1;i<=n;i++){
cin>>v[i]>>w[i];
}
memset(dp,0,sizeof dp);
for(int i=1;i<=n;i++){
for(int j=1;j<=V;j++){
dp[i][j]=dp[i-1][j];
if(j-v[i]>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-v[i]]+w[i]);
}
}
cout<<dp[n][V]<<endl;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
E.最长上升子序列
Key:动态规划—线性DP
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 5010, MAX = 1e9, INF = -1e9;
int n;
int a[N];
int dp[N];
int ans=0;
void solve(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
dp[i]=1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
if(a[j]<a[i])dp[i]=max(dp[i],dp[j]+1);
ans=max(ans,dp[i]);
}
}
cout<<ans<<endl;
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
F.编辑距离
Key:动态规划—线性DP
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 2010, MAX = 1e9, INF = -1e9;
string a,b;
int dp[N][N];
void solve(){
cin>>a>>b;
int n=a.size();int m=b.size();
a=" "+a;b=" "+b;
for(int i=1;i<N;i++)dp[i][0]=dp[0][i]=i;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (a[i] != b[j]));
}
}
cout<<dp[n][m]<<endl;
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
G.第37次CSP认证第二题:机器人饲养指南
Key:动态规划—背包问题—完全背包
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 105, MAX = 1e9, INF = -1e12, M = 10005;
int n,m;
int w[N];
int dp[N][M];
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>w[i];
}
memset(dp,0,sizeof dp);
for(int i=1;i<=m;i++){
for(int j=0;j<=n;j++){
for(int k=0;k*i<=j;k++){
dp[i][j]=max(dp[i][j],dp[i-1][j-k*i]+k*w[i]);
}
}
}
cout<<dp[m][n]<<endl;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
H.Function
Key:记忆化搜索
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
#define val(a) (a<'a' ? (a-'A'+'a') : a)
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> Q;
const int N = 25, MAX = 1e9, INF = -1e9;
int x,y,z;
int dp[N][N][N];
int dfs(int a,int b,int c){
if(a<=0||b<=0||c<=0)return 1;
if(a>20||b>20||c>20)return dfs(20,20,20);
else{
if(dp[a][b][c]!=-1);
else if(a<b&&b<c){
dp[a][b][c]=dfs(a,b,c-1)+dfs(a,b-1,c-1)-dfs(a,b-1,c);
}
else{
dp[a][b][c]=dfs(a-1,b,c)+dfs(a-1,b-1,c)+dfs(a-1,b,c-1)-dfs(a-1,b-1,c-1);
}
return dp[a][b][c];
}
}
void solve(){
memset(dp,-1,sizeof dp);
while(1){
cin>>x>>y>>z;
if(x==y&&y==z&&z==-1)return ;
cout<<"w("<<x<<", "<<y<<", "<<z<<") = "<<dfs(x,y,z)<<endl;
}
return ;
}
signed main()
{
fast();
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}