CF295C Greg and Friends
首先
我们考虑每次船来回运人时都可以看成一种dp状态
又因为人的体重只有50kg和100kg两种,
所以我们可以开一个三维数组dp[i][j][k],第1维表示在出发岸50kg有i个,第2维表示在出发岸100kg有j个,第3维表示船在哪一岸
又考虑到每一个人都是不同的,所以我们需要对在船岸的这一边的人数和上船的人数去组合数,可以打一个杨辉三角的表b[i][j],用来快速查询组合数
所以状态转移就可以枚举在此岸上船50kg和100kg的人数并取组合数乘上自己的原值(乘法原理)
其次
本题还要求求最短运送次数,我们就可以用广搜来遍历枚举答案
同理dp的状态转移也可以在广搜中一起完成
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct kk
{
ll i,j,k;
}sh[1000001];//数组模拟队列
ll n,w,step[100][100][3],dp[100][100][3];
ll a[100],t50,t100,b[1000][1000],mod;
void bfs(ll i1,ll j1,ll wh)
{
ll l,r;
l=0;r=0;
sh[l].i=i1;
sh[l].j=j1;
sh[l].k=wh;
r++;
while (l<r)
{
ll nowi,nowj,nowh;
nowi=sh[l].i;
nowj=sh[l].j;
nowh=sh[l].k;
l++;
if (nowh==1)//枚举出发岸
for (ll x=0;x<=nowi;x++)
{
for (ll y=0;y<=nowj;y++)
{
if (50*x+100*y>w || (x==0 && y==0))
continue;//判断体重是否超过船载重
ll xi,xj,xwh;
xi=nowi-x;
xj=nowj-y;
xwh=1-nowh;
if (step[xi][xj][xwh]!=0)//重点,此处与一般的广搜不同,当一种状态被遍历过了不能直接标记掉,还要取最优情况
{
if (step[nowi][nowj][nowh]+1<step[xi][xj][xwh])
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
if (step[nowi][nowj][nowh]+1==step[xi][xj][xwh])//步数相同时,更新dp值
{
dp[xi][xj][xwh]+=(b[nowi+1][x+1]%mod)*(b[nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
}
}
else
{
dp[xi][xj][xwh]=(b[nowi+1][x+1]%mod)*(b[nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
sh[r].i=xi;
sh[r].j=xj;
sh[r].k=xwh;
r++;
}
}
}
if (nowh==0)//枚举对岸
for (ll x=0;x<=t50-nowi;x++)
{
for (ll y=0;y<=t100-nowj;y++)
{
if (50*x+100*y>w || (x==0 && y==0))
continue;
ll xi,xj,xwh;
xi=nowi+x;
xj=nowj+y;
xwh=1-nowh;
if (step[xi][xj][xwh]!=0)
{
if (step[nowi][nowj][nowh]+1<step[xi][xj][xwh])
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
if (step[nowi][nowj][nowh]+1==step[xi][xj][xwh])
{
dp[xi][xj][xwh]+=(b[t50-nowi+1][x+1]%mod)*(b[t100-nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
}
}
else
{
dp[xi][xj][xwh]=(b[t50-nowi+1][x+1]%mod)*(b[t100-nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
sh[r].i=xi;
sh[r].j=xj;
sh[r].k=xwh;
r++;
}
}
}
}
}
int main()
{
scanf("%lld%lld",&n,&w);
for (ll x=1;x<=n;x++)
scanf("%lld",&a[x]);
memset(dp,0,sizeof(dp));
memset(step,0,sizeof(step));
for (ll x=1;x<=n;x++)
{
if (a[x]==50)
t50++;
if (a[x]==100)
t100++;
}
b[1][1]=1;
for (ll x=2;x<=50;x++)
{
for (ll y=1;y<=x;y++)
b[x][y]=b[x-1][y]+b[x-1][y-1];//杨辉三角
}
mod=1000000007;
dp[t50][t100][1]=1;//令1表示出发岸,0表示到达岸
step[t50][t100][1]=0;//初始化
bfs(t50,t100,1);//广搜函数
if (step[0][0][0]==0)//如何到达的步数为0就不能到达
{
printf("-1\n");
printf("0\n");
}
else
{
printf("%lld\n",step[0][0][0]);//答案即为出发岸无人且船在对岸的情况
printf("%lld\n",dp[0][0][0]);
}
}
CF295C Greg and Friends的更多相关文章
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改
A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...
- Codeforces Round #156 (Div. 2)---A. Greg's Workout
Greg's Workout time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 295C Greg and Friends BFS
Greg and Friends BFS的过程中维护一下方案数. 我个人感觉不是很好想, 但是写出来之后怎么感觉这题这么SB啊啊. #include<bits/stdc++.h> #def ...
- Codeforces 295 B. Greg and Graph
http://codeforces.com/problemset/problem/295/B 题意: 给定一个有边权的有向图.再给定一个1~n的排列. 按排列中的顺序依次删除点,问每次删除后,所有点对 ...
- codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...
- Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
随机推荐
- Spring中用@DependsOn注解控制Bean的创建顺序
1. 概述 Spirng容器自己会管理bean的生命周期和bean实例化的顺序,但是我们仍然可以根据我们自己的需求进行定制.我可以可以选择使用SmartLifeCycle接口,也可以用@Depends ...
- 温故知新————c++ 多态
参考: 1. https://blog.csdn.net/weixin_42678507/article/details/89414998 (直接说明原理) 2 .https://www.cnblo ...
- Mysql的Sql语句优化
在Mysql中执行Sql语句经常会遇到有的语句执行时间特别长的情况,出现了这种情况我们就需要静下心分析分析. 首先,我们需要确定系统中哪些语句执行时间比较长.这个可以使用Mysql的慢日志来跟踪.下面 ...
- Spring Cloud Config配置git私钥出错
重装了电脑之后,重新生成了ssh key文件id_rsa和id_rsa.pub文件. 然后在配置中心的配置了私钥之后启动项目,报错如下: Reason: Property 'spring.cloud. ...
- 多测师讲解python __for 循环___高级讲师肖sir
横向输出 1.遍历字符串 2.遍历列表 3.遍历元组 方法一: 方法二: 方法三: #循环字典:方法一# dict1={"name":"zhihao",&quo ...
- Mysql架构与内部模块-第一章
Mysql作为大多数中小型企业的首选数据库,也可能是众多同僚接触的第一个数据库,其热门程度不言而喻,一些相对基础的知识本系列不做赘述,主要简述Mysql相关的进阶知识. 本章将由浅入深的讲解从连接My ...
- pytest文档45-allure添加环境配置(environment)
前言 在 allure 报告首页 ENVIRONMENT 显示 'There are no environment variables' 没有环境变量的配置信息. 环境变量配置可以添加报告相关的配置参 ...
- HDU - 6736 F - Forest Program
题意 给你n个点m条边,并且保证整个图是仙人掌. 仙人掌:每条边仅属于1条或者0条回路 且无重边和自环 让你删掉一些边使其变成一棵树(拥有点数-1条边) 注意一个点也是森林 图可能是不联通的 思路 考 ...
- DataX 3.0 源码解析一
源码解析 基本调用类分析 任务启动由python脚本新建进程进行任务执行,后续执行由Java进行,以下将对java部分进行分 其中的调用原理机制. Engine 首先入口类为com.alibaba.d ...
- c++ 获取文件创建时间、修改时间、访问时间、文件内容长度
int GetFileInfo(string& strPath, int& iCreateTime, int& iModifyTime, int& iAccessTim ...