当前位置:网站首页>Transform optimization problems into decision-making problems
Transform optimization problems into decision-making problems
2022-07-05 05:36:00 【Falling spring is only inadvertently】
Turn the optimization problem into a decision problem
Preface
It is a bit similar to the binary search problem , Translate the best answer into , Is there any equal to x Or a better answer .
Travel to Canada
The main idea of the topic
Dongjian decided to drive across Canada . Canadian 1 Highway is one of the longest highways in the world , It connects all major cities from east to West . Dongjian plans to start from the western city of Victoria , Go straight east along the highway , Go to 8030km Outside the eastern city of St. John .
This highway is famous for its numerous road signs ”. Because the highway passes n Major cities , And when we are about to reach a city , The number of road signs indicating the remaining distance is very large . Marked with a distance of i How far is the road sign of the city from before it arrives Mi Rice starts , With Gi Meter interval is set .
int n,k;
int l[5000],m[5000],g[5000];
// Decision making issues , arrive dist Can encounter k More than road signs
bool decision(int dist){
int ret = 0;
for(int i=0;i<n;++i){
// Can meet 1 More than i The road signs of a city
if(dist >= l[i]-m[i])
ret += (min(dist,l[i]) - (l[i]-m[i]))/g[i] + 1;
return ret>= k;
}
}
// Computation first k Location of road signs
int optimize(){
// Cyclic inequality : !decision(lo)&& decision(hi)
int lo = -1,hi = 80300001;
while(lo+1 < hi){
int mid = (lo + hi)/2;
if(decision(mid))
hi = mid ;
else lo = mid;
}
return hi;
}
Drop out course
The main idea of the topic
Bojun blindly chose more courses this semester , The result exceeded the credits . After getting the midterm grades , Because the grades did not meet the criteria for obtaining scholarships next semester , There is only one sigh left . Now? , He can only seize the opportunity to withdraw from the course starting next week .
The school will allocate scholarships according to the ranking after the mid-term and final exams . for example , A student chooses the first 1 There are courses c: Students attend classes . If this student's first i The mid-term ranking of courses is Ii,
that , The cumulative ranking of the student's mid-term exam will be defined as the following form .
∑ r i ∑ c i \frac{\sum_{}{}{}{r_i}}{\sum_{}{}{}{c_i}} ∑ci∑ri
If you drop out of the course , Then the withdrawn subjects will not be included in the cumulative ranking of the mid-term exam . School rules , Even if you drop out of the course , As long as the remaining courses exceed k individual , You can get scholarships according to the ranking . When Bo Jun withdraws from the course appropriately , Try to write a program to calculate the minimum cumulative ranking he can get .
Their thinking
Convert to decision(x) = Withdrawing courses appropriately can reduce the cumulative ranking to x The following
Turn into ∑ r i ∑ c i < = x \frac{\sum_{}{}{}{r_i}}{\sum_{}{}{}{c_i}} <= x ∑ci∑ri<=x
Finishing becomes
0 < = x ∑ c i − ∑ r i = ∑ ( x c i − r i ) 0<= x\sum_{}{}{}{c_i} - \sum_{}{}{}{r_i} = \sum_{}{}{}{(xc_i - r_i)} 0<=x∑ci−∑ri=∑(xci−ri)
int n,k;
int c[N],r[N];
// Decision making issues , Can the cumulative ranking be changed into average
bool decision(double average){
vector<double>v;
for(int i=0;i<n;++i)
v.push_back(average*c[i] - r[i]);
sort(v.begin(),v.end());
// Turn this question into ‘v in k Can the sum of the elements be greater than 0’ The problem of , Then use greedy methods to solve
double sum = 0;
for(int i = n-k;i<n;++i)
sum += v[i];
return sum >= 0;
}
// optimization problem , Calculate the minimum cumulative ranking that can be accumulated
double optimiza(){
// The cumulative ranking is [0,] The real number between
// Cyclic infinitive
double lo = -1e9 ,hi = 1;
for(int iter = 0;iter<100;iter++){
double mid = (lo + hi)/2;
// Can you reach the cumulative ranking mid
if(decision(mid))
hi = mid;
else
lo = mid;
}
return hi;
}
边栏推荐
- 剑指 Offer 53 - II. 0~n-1中缺失的数字
- [jailhouse article] jailhouse hypervisor
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- SAP method of modifying system table data
- Pointnet++ learning
- Pointnet++的改进
- Reflection summary of Haut OJ freshmen on Wednesday
- [binary search] 34 Find the first and last positions of elements in a sorted array
- CF1634E Fair Share
猜你喜欢

Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
![[to be continued] [UE4 notes] L2 interface introduction](/img/0f/268c852b691bd7459785537f201a41.jpg)
[to be continued] [UE4 notes] L2 interface introduction

Sword finger offer 35 Replication of complex linked list

网络工程师考核的一些常见的问题:WLAN、BGP、交换机

SAP-修改系统表数据的方法

Yolov5 adds attention mechanism

剑指 Offer 06.从头到尾打印链表
![[depth first search] 695 Maximum area of the island](/img/08/cfff4aec667216e4f146205a12c13f.jpg)
[depth first search] 695 Maximum area of the island
![[jailhouse article] performance measurements for hypervisors on embedded ARM processors](/img/c0/4843f887f77b80e3b2329e12d28987.png)
[jailhouse article] performance measurements for hypervisors on embedded ARM processors

Talking about JVM (frequent interview)
随机推荐
Solution to the palindrome string (Luogu p5041 haoi2009)
Yolov5 adds attention mechanism
游戏商城毕业设计
Haut OJ 1352: string of choice
Haut OJ 1321: mode problem of choice sister
On-off and on-off of quality system construction
PC register
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Drawing dynamic 3D circle with pure C language
Control Unit 控制部件
剑指 Offer 58 - II. 左旋转字符串
SAP-修改系统表数据的方法
Web APIs DOM node
Graduation project of game mall
Hang wait lock vs spin lock (where both are used)
Little known skills of Task Manager
AtCoder Grand Contest 013 E - Placing Squares
剑指 Offer 05. 替换空格
F - Two Exam(AtCoder Beginner Contest 238)
[article de jailhouse] jailhouse hypervisor