01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019
题意:https://nanti.jisuanke.com/t/41420
给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值。
思路:
从最大重量的石子开始背包,每次ans+=dp【j-v【i】】就行了。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int v[N],dp[N];
void Init(int _[],int n)
{
for(int i=;i<=n;++i)
_[i]=;
} void PR(int _[],int n)
{
for(int i=;i<=n;++i)
pr("%d ",_[i]);
pr("\n");
} bool J(int sum,int x,int min_)
{
return *x>=sum&&*x<=sum+min_;
} int main()
{
int T;
sc("%d",&T);
while(T--)
{
int n,sum=;
sc("%d",&n);
for(int i=;i<=n;++i)
sc("%d",&v[i]),sum+=v[i];
sort(v+,v++n);
Init(dp,sum); ll ans=;
dp[]=;
for(int i=n;i>=;--i)
{
for(int j=sum;j>=v[i];--j)
if(dp[j-v[i]])
{
dp[j]+=dp[j-v[i]];
dp[j]%=mod;
ans+=J(sum,j,v[i])?dp[j-v[i]]:;
ans%=mod;
}
}
pr("%lld\n",ans);
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019的更多相关文章
- The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game
题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING
题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...
- The Preliminary Contest for ICPC Asia Shenyang 2019 C Dawn-K's water (完全背包)
完全背包为什么要取到M,可以取到2*M嘛,这题需要整取,对于不能整取的背包容量,dp[k]=INF,以及dp[j-water[i].weight]=INF时,dp[j]也不需要更新.如果不整取的话,后 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 K题 center
You are given a point set with nn points on the 2D-plane, your task is to find the smallest number o ...
- 洛谷 P1064 金明的预算方案【DP/01背包-方案数】
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...
- caioj 1412 动态规划3:a+b问题(完全背包方案数)
每个素数就是一个物品,然后就相当于求完全背包方案数 把max改成+就好了. #include<cstdio> #include<vector> #include<cstr ...
- HDU 1284 钱币兑换问题 (动态规划 背包方案数)
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- UVa 11137 (完全背包方案数) Ingenuous Cubrency
题意:用13.23……k3这些数加起来组成n,输出总方案数 d(i, j)表示前i个数构成j的方案数则有 d(i, j) = d(i-1, j) + d(i, j - i3) 可以像01背包那样用滚动 ...
- Buy the souvenirs---hdu2126(01背包输出方案数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 有n个物品每个物品的价格是v[i],现在有m元钱问最多买多少种物品,并求出有多少种选择方法: 如 ...
随机推荐
- mybatis-puls 字段为null时候的更新问题
在mybatis-puls重设置的全局更新策略 为null的字段忽略更新.但是在某些业务需求下面,可能需要某些字段更新为null值.那么改如何设置 1, 在你的实体属性上面单独添加需要更新nu l l ...
- golang——写文件和读文件
之前聊过,操作文件——读写文件,直接调用接口即可. 如果是一直写入操作,写入操作一直进行的,免不了会有,有时一大批数据过来,有时没有一条数据. 鉴于此场景,选择用select....channel 的 ...
- win10系统配置FTP
FTP是一种远程传输协议,支持这种协议的就是FTP服务器.我们可以在自己的PC机上创建一个.然后通过网页就可以访问FTP服务器下的文件夹. 搭建过程 1.首先需要开启FTP服务.在菜单中打开控制面板. ...
- 整合spring boot时操作数据库时报错Caused by: java.lang.InstantiationException: tk.mybatis.mapper.provider.base.B
原文:https://blog.csdn.net/u__f_o/article/details/82756701 一般出现这种情况,应该是没有扫描到对应的mapper包,即在启动类下配置MapperS ...
- python 普通继承方式和super继承方式
Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this),用于传递对象本身,而在调用的时候则不必显式传递,系统会自动传递. 举一个很常见的例子: >> ...
- 关于go module
从Go 1.11开始引入module,用于版本管理. 通过使用module,工程目录的位置不用必须放在GOPATH下. 当前版本是1.13,下文中将以Go1.13为例介绍module. 在Go 1.1 ...
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Ubuntu16.04.3安装并配置samba方法
目的:单个Linux用户可以在windows电脑上通过映射网络驱动器的方式来访问自己的工作目录. 方法如下: 1.安装smb服务器:apt-get install samba2.安装smb支持的文件系 ...
- Android 中更新 UI 的四种方式
runOnUiThread handler 的 post handler 的 sendMessage View 自身的 post
- FreeMarker学习2
为了处理缺失变量,FreeMarker提供了两个运算符: 用于防止对象不存在而导致的异常 !:指定缺失变量的默认值 ??:判断某个变量是否存在,返回boolean值 ${item.createtime ...