【包含内容】
0/1分数规划
一般形式: 然后选择做多k 个或 至少k 个 , 这样的数 删去类的 使得 结果 R 最大 , 最优的问题.
我们先定义一个函数F(L):= ,显然这只是对目标式的一个简单的变形。分离参数,得到F(L):=sigma((。这时我们就会发现,如果L已知的话,a[i]-L*b[i]就是已知的,当然x[i]是未知的。记d[i]=a[i]-L*b[i],那么F(L):=
一般采用 二分答案, L 检索 F[l] 与 0 的关系 来判断 L 是否是最优
F(L)= 即
也就是说,如果一个方案使得F(L)>0 说明了这组方案可以得到一个比现在的L更优的一个L,
既然有一个更优的解,那么为什么不用呢?
F(L) < 0 说明 此时 答案过大 .
例如 Poj 2976
http://poj.org/problem?id=2976
选择 k 个删除使得的答案最大 , 采用二分答案. 判优
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
//#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
typedef long long ll;
const int maxn = 1e5+10;
using namespace std;
const double esp = 1e-5;
int n,k;
double a[maxn],b[maxn],s[maxn];
int main()
{
while(~scanf("%d %d",&n,&k))
{
if(n==0&&k==0) break;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(s,0,sizeof(s));
rep(i,1,n+1)
scanf("%lf",&a[i]);
rep(i,1,n+1)
scanf("%lf",&b[i]);
double l = 0.0;
double r = 1000000.0;
while( l+esp < r)
{
double mid = (l+r)*1.0/2;
rep(i,1,n+1)
s[i] = a[i]-mid*b[i];
sort(s+1,s+n+1);
double sum =0;
rep(i,k+1,n+1) sum+=s[i];
if( sum >=0)
l = mid;
else
r = mid;
}
printf("%d\n",(int)(l*100+0.5));
}
return 0;
}
例2 :牛客网暑期ACM多校训练营(第五场)
A
链接:https://www.nowcoder.com/acm/contest/143/A
来源:牛客网
Kanade selected n courses in the university. The academic credit of the i-th course is s[i] and the score of the i-th course is c[i].
At the university where she attended, the final score of her is
Now she can delete at most k courses and she want to know what the highest final score that can get.
The first line has two positive integers n,k
The second line has n positive integers s[i]
The third line has n positive integers c[i]
Output the highest final score, your answer is correct if and only if the absolute error with the standard answer is no more than 10-5
示例1
复制
3 1
1 2 3
3 2 1
复制
2.33333333333
Delete the third course and the final score is
1≤ n≤ 105
0≤ k < n
1≤ s[i],c[i] ≤ 103
选择 至多K 个 删除, 那么 排序后 从后面选择 n-k 个 , 然后 依次比较前k 个是否选择
#include <iostream>
#include <math.h>
#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
typedef long long ll;
const int maxn = 1e5+10;
using namespace std;
const double esp = 1e-5;
int n,k;
double a[maxn];
double b[maxn];
double s[maxn];
bool judge(double mid)
{
rep(i,1,n+1)
s[i] = a[i]-mid*b[i];
sort(s+1,s+n+1);
double sum = 0;
rep(i,k+1,n+1)
sum+=s[i];
double mmx = sum;
rep(i,1,k+1)
mmx = max(sum+s[i],mmx);
if(mmx>=0)
return true;
return false;
}
int main()
{
scanf("%d %d",&n,&k);
rep(i,1,n+1)
scanf("%lf",&b[i]);
rep(i,1,n+1)
scanf("%lf",&a[i]);
double l =0.0 , r = 1.0;
rep(i,1,n+1)
{
a[i] = a[i]*b[i];
r = max(a[i],r);
}
while(l +esp <r)
{
double mid = (l+r)*1.0/2;
if(judge(mid))
l = mid;
else
r = mid;
// cout<<l<<" "<<r<<endl;
}
//cout<<l<<" "<<r<<endl;
printf("%.10f\n",l);
return 0;
}
文章浏览阅读258次。多线程基础之设计模式Future模式_线程 future
文章浏览阅读1w次,点赞17次,收藏60次。本文档仅对ccs编程过程中所出现的error#5、error#10008-D、error#10010做简要讲解在使用ccs对dsp编程过程中,用户可能会参考一些例程或在维护优化时阅读他人程序,而在导入程序时会出现各种各样的错误或警告,下面对编者在修改程序时遇到的error#5、error#10008-D和error#10010做简要讲解。1.error#5的错误更正讲解在ccs中导入其..._cannot find file "libc.a
文章浏览阅读359次。题意:给定m(m思路:暴力找出第一个串的所有长度大于等于3的子串,用KMP算法求其是否为剩下m-1个串的子串。为了复用next数组,枚举子串时先固定起点(求一遍next数组即可),然后由长到短枚举子串(剪枝)。#include #include using namespace std;#define N 60char s[12][N+5],t[N+5],res[N+5];int
文章浏览阅读1.4k次。reshape把指定的矩阵改变形状,但是元素个数不变,例如,行向量:a = [1 2 3 4 5 6]执行下面语句把它变成3行2列:b = reshape(a,3,2)执行结果:b =1 42 53 6若a=[1 2 34 5 67 8 9]使用reshpe后想得到b=[1 2 3 4 5 6 7 8 9]只需要将a转置一下就可以了:b=reshape(a',1,9)---------------..._matlab中reshape的含义
文章浏览阅读1k次。运算符在数学和C语言中的区别刚开始学C语言的人,一般都认为C语言中的运算符跟数学中的运算符完全相同,没必要去考虑和研究,从而在利用过程中经常出错而把学习C语言越来越难或神秘化,其实学C语言并不是很难的事,要把握有些重要技巧,很容易学会.著名计算机科学家沃思(Nikiklaus Wirth)说“程序=算法+数据类型”,要好好学会程序,首先要深入了解算法,而了解算法事实上指的是就是正确地了解和利用运算..._c语言中的加减乘除和数学中的加减乘除有什么不同【
文章浏览阅读3.9k次。一、三大框架基本结构1.为什么需要框架说明: 如果生产环境下的项目,都是从头(从底层写起)开发,难度太大了,并且开发的效率极其低下. 所以为了让项目快速的上线部署. 将某些特定的功能.进行了高级的封装. 那么我们如果需要使用封装后的API.,则必须按照人家的要求编码2.框架的分类:1.Spring框架:整个框架中负责“宏观调控”的(主导),负责整合其它的第三方的框架2.SpringMVC框架:主要负责实现前后端数据的交互3.Mybatis框架/MybatisPlus框架:持久层框.._后端框架三大框架
文章浏览阅读2次。 堆栈原理: 数组模拟堆栈: //数组模拟栈class ArrayStack{ //栈顶 private int top = -1; private int maxSize; private int[] arrayStack; public ArrayStack(int maxSize){ this.maxSi...
文章浏览阅读742次,点赞16次,收藏17次。不选: Enforce portability rules to share this project with others。勾选: Configure Advanced Settings after project creation。保存类型(T):Understand projects (*.udb)勾选:Include subdirectories (包含子文件夹)Additional Filters: (空)单击 文件夹 lab1。文件名(N):lab1。双击 文件夹 boot。_understand 6.5.1176
文章浏览阅读969次。在从零开始带你成为MySQL实战优化高手学习笔记(一)中学习到一条语句到底是怎么执行的,从链接获取数据到通过查询解析器解析SQL语句表达的什么意思,解析之后由查询优化器生成查询路径树,选出一条最优查询路径调用存储引擎接口..._mysql_global_status_innodb_buffer_pool_reads
文章浏览阅读8.8k次,点赞6次,收藏12次。传统的表单控件十分简陋,可以说是很难看,那怎么办?方法:我们自己做一个好看的样式出来,用各种标签啊,css啊,是可以做到的。如图:做出这样一个样子应该是很简单的,但是怎么让他具有上传的功能的呢?那就使用代理的方法,点击上传就等于点击(上传文件表单控件)废话不多说,直接上代码:html:测试插件body{font_文件上传框很丑
文章浏览阅读4.8k次,点赞3次,收藏18次。js简单表格操作,对表格进行增删改,效果图:全部代码:<!DOCTYPE html><html><head> <meta charset="utf-8" /> <script type="text/javascript" src="js/jquery.2.1.4.min.js" ></sc_"var str = '序号名字
文章浏览阅读1.1w次,点赞8次,收藏99次。今天通过一份销售数据,聊聊Power BI数据分析。一、分析数据数据源总的有四个表,店铺资料,销售目标,销售数据_本期,销售数据_去年同期。各表表头如下:1店铺资料表:2销售目标:3销售数据_本期:4销售数据_去年同期:数据中包含多个城市、督导、店铺的数据,我希望经过分析后能得到各个城市/店铺的销售情况,即业绩、业绩完成率、业绩贡献度、业绩增长率、各销售人员的销售能力等。此次..._powerbi汇总销售人员业绩包括无销售记录的人