CodeForces - 632E Thief in a Shop 完全背包
632E:http://codeforces.com/problemset/problem/632/E
参考:https://blog.csdn.net/qq_21057881/article/details/51023067
题意:
给定n个值,让你选择k个数,可以重复选择,问可以得到哪些数字。
思路:
显然最小的值起到很大的作用,我们可以把每个值都减去这个最小值,利用完全背包,建立dp【i】表示,取到 i 这么多值最少需要多少个数。如果取到 i值需要的数值小于等于K,那么k * 最小值 + i 就是我们能取到的一个值,因为一定需要 k*最小值 (注意我们一开始让每个数都剪去了最小值,要加回来)。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert>using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)#define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queuetypedef long long ll; typedef unsigned long long ull;typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3;//priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n'#define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que;const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 // const int mod = 10007; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601;template<typename T> inline T read(T&x){x=0;int f=0;char ch=getchar();while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x=f?-x:x; }/*-----------------------showtime----------------------*/const int maxn = 1009;int a[maxn],dp[maxn*maxn]; int main(){int n,k;scanf("%d%d", &n, &k);for(int i=1; i<=n; i++)scanf("%d", &a[i]);sort(a+1,a+1+n);n = unique(a+1,a+1+n) - (a+1);// debug(n);for(int i=2; i<=n; i++) a[i] = a[i] - a[1];memset(dp,inf,sizeof(dp));dp[0] = 0;for(int i=2; i<=n; i++){for(int j=a[i]; j<=a[i]*k; j++){dp[j] = min(dp[j] , dp[j-a[i]] + 1);}}for(int i=0; i<=a[n] * k; i++){if(dp[i] <= k){printf("%d ", a[1] * k + i);}}printf("\n");return 0; }
CF - 632E
转载于:https://www.cnblogs.com/ckxkexing/p/9741214.html
CodeForces - 632E Thief in a Shop 完全背包相关推荐
- CodeForces 632E Thief in a Shop(FFT)
题意:n种物品每种物品有无限个,每个物品有一个价格,现在问选取k个的所以可能总价. 思路:FFT.如果只选择两件商品的话,很容易想到FFT,对于其他的k,只要类似快速幂来求即可,这样做时间复杂度为O( ...
- Codeforces632E Thief in a Shop(NTT + 快速幂)
题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...
- Educational Codeforces Round 21 E. Selling Souvenirs(背包)
题目:http://codeforces.com/contest/808/problem/E 题目大意: 有n个物品,背包容量为m. 每个物品只会有1,2,3个单位占有空间,价值为c. 现在问如何可以 ...
- Thief in a Shop
https://codeforces.com/problemset/problem/632/E 题解:将所有元素减去最小元素 再进行完全背包 如果体积超过k则不可以. /* *@Author: STZ ...
- POJ 1293 - Duty Free Shop 01背包记录所选物品
裸的01背包.dp[x]只要是bool型记录当前空间是否可用.. 而为了找到用了哪些物品..dp[x]设置为int型..进行记录.. Program: #include<iostream> ...
- html轮播台袋效果,css3百叶窗轮播图效果
标题 #Con{width:900px; /*宽度*/ height:500px;/*高度*/ background:#9933FF;/*背景颜色*/ position:relative;} #con ...
- 2016区域赛前冲刺训练
UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...
- 【2021杭电多校赛】2021“MINIEYE杯”中国大学生算法设计超级联赛(1)签到题15869
2021"MINIEYE杯"中国大学生算法设计超级联赛(1) Start Time : 2021-07-20 12:10:00 End Time : 2021-07-20 17:1 ...
- 2020年12月统考练习题
词汇与语法B 1.He opened the letter and it contained ________. A.an important information B.some important ...
最新文章
- 微信小程序开发工具安装、设置
- iOS 跑马灯封装(带点击事件)
- ScrollView分栏视图分析
- linux查看fifo内容,linux 有名管道(FIFO)
- opencv 叠加文字_Hello world.
- stm32f103r6最小系统原理图_超强PCB布线设计经验谈附原理图
- CTF【解密】字符串flag被加密成已知新字符串,请解密出flag,可以使用Python解码出WriteUp
- js实时获取窗口大小变化
- python学习---简介
- mk-parallel-dump 实验
- 程序员降薪求职到底该不该?
- 微信 的微服务器配置,spring-boot wm-accesstoken
- 数据库设计 表和表之间的三种关系
- Multisim 14.1 安装步骤
- 国家多部委发布13份“十四五”规划,115项重大工程​
- cmd修改计算机睡眠、休眠时间,批量命令
- PCBA方案设计——蓝牙脂肪心率秤方案
- 中国粮食安全问题及其应对措施
- java translate_java – 当使用translate()方法时,JPanel中的Tit...
- 儿子于靖洋15个月照片
热门文章
- 为什么要引入模式概念来设计软件
- jmeter中文_JMeter安装配置
- python基础教程第二版答案-《Python基础教程》(第2版修订版)中要注意的地方...
- python创意小作品代码-Python学习,给自己的代码做个合集,定制自己的桌面软件!...
- python入门自学-你是如何自学 Python 的?
- python手机版怎么用-QPython,一个在手机上运行Python的神器
- php和python哪个工资高-Java、Python、Php学哪个好,哪个更有前景?
- 编程中python怎么读-python之文件读写
- python学精通要多久-精通python需要多久
- python3爬虫入门教程-总算懂得python3.4爬虫入门教程