当前位置:网站首页>[oiclass] share prizes
[oiclass] share prizes
2022-07-06 14:48:00 【Liujincheng LJC】
Share prizes
Title Description
In the school creative knowledge competition , Mingming and Wenwen got a total of n(1≦ n≦250) A prize , Every prize has a value Vi (1 ≦ Vi ≦ 2,000). They want to share these prizes equally by value , If you can't score equally, try to minimize the gap between them . Now give the number of prizes and their value , Clearly want to calculate the minimum difference after division , And the number of schemes divided .
for example : Yes 5 The value of each prize is :2, 1,8, 4, 16. Mingming and Wenwen are divided into two parts , The first four are part 1+2+4+8=15,16 As a separate part , Then the difference between the two parts :16-15 = 1. This is the division scheme with the smallest gap , And the division method of this scheme is only 1 Kind of .
Prizes of the same value intersect and convert into different schemes , Such as : There are four prizes worth {1, 1, 1, 1}, Yes 6 There are different division schemes , Divide these prizes into two parts , Every part of it 2 A prize .
Input
first line : An integer n(1≦n≦250);
Then there was n That's ok , One integer per row Vi (1 ≦ Vi ≦ 2,000) Represents the value of each prize .
Output
first line : An integer , Represents the minimum difference between the two parts of the division .
The second line : An integer , The number of division schemes representing the minimum difference , The result is right 1,000,000 Seeking remainder (mod 1,000,000).
The sample input #1:
5
2
1
8
4
16
Sample output #1:
1
1
The sample input #2:
4
1
1
1
1
Sample output #2:
0
6
Ideas
First, we can use the idea of dynamic programming :
set up f[i][j] For the former i The value of these prizes is j Different schemes
So the equation of state transition is going to be :
f[i][j] = f[i-1][j]+f[i-1][j-a[i]]
The code written at this time is like this :
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1000000;
const int N = 300;
int n,a[N],f[N][2000*N];
int maxn = 0;
int main(){
cin >> n;
for(int i=1;i<=n;i++)cin >> a[i],maxn+=a[i];
f[0][0] = 1;
for(int i=1;i<=n;i++){
for(int j=0;j<=maxn;j++){
f[i][j] = (f[i-1][j]+f[i-1][j-a[i]])%MOD;
}
}
int ans=2000*N,cnt=-1;
for(int j=0;j<=maxn;j++){
if(abs((maxn-j)-j)<ans){
ans = abs((maxn-j)-j);
cnt = f[n][j];
}
}
cout << ans << endl << cnt << endl;
return 0;
}
result : MLE
The space complexity of this code is too high . So I put f The first dimension of is changed to a scrolling array :
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1000000;
const int N = 260;
int n,a[N],f[3][2000*N];
bool vis[3][2000*N];
int maxn = 0;
int main(){
cin >> n;
for(int i=1;i<=n;i++)cin >> a[i],maxn+=a[i];
f[0][0] = 1;
vis[0][0] = 1;
for(int i=1;i<=n;i++){
for(int j=0;j<=maxn;j++){
if(j-a[i]>=0){
f[i%2][j] = (f[(i-1)%2][j]+f[(i-1)%2][j-a[i]])%MOD;
vis[i%2][j]= vis[(i-1)%2][j] || vis[(i-1)%2][j-a[i]];
}
else{
f[i%2][j] = f[(i-1)%2][j];
vis[i%2][j] = vis[(i-1)%2][j];
}
}
}
int ans=2000*N,cnt=-1;
for(int j=0;j<=maxn;j++){
if(abs((maxn-j)-j)<ans and vis[n%2][j]!=0){
ans = abs((maxn-j)-j);
cnt = f[n%2][j];
}
}
cout << ans << endl << cnt << endl;
return 0;
}
This time it became TLE
Time optimization , take mod The final answer is :
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1000000;
const int N = 260;
int n,a[N],sum[N],f[3][2000*N];
bool vis[3][2000*N];
int maxn = 0;
int main(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i];
maxn+=a[i];
sum[i]=sum[i-1]+a[i];
}
f[0][0] = 1;
vis[0][0] = 1;
for(int i=1;i<=n;i++){
int I2=i%2,I12=(i-1)%2;
for(int j=0;j<=sum[i];j++){
if(j-a[i]>=0){
f[I2][j] = f[I12][j]+f[I12][j-a[i]];
if(f[I2][j]>=MOD) f[I2][j]-=MOD;
vis[I2][j]= vis[I12][j] || vis[I12][j-a[i]];
}
else{
f[I2][j] = f[I12][j];
vis[I2][j] = vis[I12][j];
}
}
}
int ans=2000*N,cnt=-1;
for(int j=0;j<=maxn;j++){
if(abs((maxn-j)-j)<ans and vis[n%2][j]!=0){
ans = abs((maxn-j)-j);
cnt = f[n%2][j];
}
}
cout << ans << endl << cnt << endl;
return 0;
}
Turned into AC !
边栏推荐
- 数字电路基础(五)算术运算电路
- Pointers: maximum, minimum, and average
- 函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
- Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
- MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
- 后台登录系统,JDBC连接数据库,做小案例练习
- 四元数---基本概念(转载)
- 使用 flask_whooshalchemyplus jieba实现flask的全局搜索
- 【指针】删除字符串s中的所有空格
- flask实现强制登陆
猜你喜欢
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
数据库多表链接的查询方式
How to transform functional testing into automated testing?
Realize applet payment function with applet cloud development (including source code)
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
数字电路基础(四) 数据分配器、数据选择器和数值比较器
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Keil5 MDK's formatting code tool and adding shortcuts
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
随机推荐
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
Fundamentals of digital circuits (I) number system and code system
Fundamentals of digital circuits (II) logic algebra
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
“Hello IC World”
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
函数:字符串反序存放
关于超星脚本出现乱码问题
[issue 18] share a Netease go experience
About the garbled code problem of superstar script
Always of SystemVerilog usage_ comb 、always_ iff
Intranet information collection of Intranet penetration (2)
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
JDBC read this article is enough
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
Why can swing implement a form program by inheriting the JFrame class?
Chain team implementation (C language)
JDBC 的四种连接方式 直接上代码