问题描述
我有一些现实生活中的墙,其特征是两个高度(最左边和最右边)。
例如
I I I I
I I I I I
I I I I I I
I_____I I_____I I____I
第一个具有最左边的高度hl=4
,最右边的高度hr=3
,第二个hl=4
和hr=4
,依此类推。
鉴于hl
和hr
,我现在的任务是找到最小体积的墙以达到hl
和hr
。因此(a)只允许降低墙体任一侧的高度,但不允许增加高度;(b)损失的体积应最小。
在第一种方法中,我已使用最小高度hMin=std::min(hl,hr)
将问题减小到一个高度。这样,我可以用“墙壁”填充地图,并使用hMin
作为键,然后使用lower_bound
和max(hl,hr)
搜索获得解决方案。
现在考虑到两个高度的最佳解决方案,我在构建严格的弱阶时遇到各种麻烦。到目前为止,我一直尝试将密钥扩展到第二个高度,减少自定义,并等效地使用lower_bound。 我的风俗习惯看起来有点像:
struct KeyLess
{
bool operator(Key const&x,Key const&y) const
{
if ((x.hl + roundOff < y.hl ) && (x.hr+ roundOff < y.hr))
return true;
if ((y.hl + roundOff < x.hl ) && (y.hr+ roundOff < x.hr))
return false;
return false;
}
};
但是显然x.hl> y.hl
和x.hr < y.hr
或这些类型在视觉上都有问题
I I
I I I I
I I I I I I
I_____I I_____I I____I
并且没有给出严格的弱排序afaik。
我很乐意为我的问题构造一个较少的运算符,或者向我展示找到该问题的解决方案的另一种方法。
示例
I I I I I I
I I I I I I I I
I I I I I I I I
I I I I I I I I I
I_____I I_____I I____I I_____I I_____I
给出hl=3
和hr=5
,它应该返回第三个(hl=4
和hr=5
)。
只要我能找到解决方案,墙壁在地图上的保存顺序本身就无关紧要(但是我认为这也是我在这里找到有意义的顺序的问题。
解决方法
我想你想要
struct KeyLess
{
bool operator(Key const&x,Key const&y) const
{
return std::pair(std::abs(x.hl - x.hr),std::min(x.hl,x.hr))
< std::pair(std::abs(y.hl - y.hr),std::min(y.hl,y.hr));
}
};
即首先按高度差排序,然后按较小的高度排序。如果您仍然需要区分
I I
I I
I I I I
I_____I I_____I
然后您可以通过任意选择第一个小于第二个来扩展该范围
struct KeyLess
{
bool operator(Key const&x,Key const&y) const
{
return std::tuple(std::abs(x.hl - x.hr),x.hr),x.hl < x.hr)
< std::tuple(std::abs(y.hl - y.hr),y.hr),y.hl < y.hr);
}
};
,
您有一个地图(或地图集),它需要严格的弱排序,但是您并不在乎顺序。
您可以简单地使用unordered_map(或unordered_set),而不必担心。
或者您可以创建严格的弱排序。幸运的是,使用#include
#Packages
if (!require('lubridate')) {install.packages('lubridate')};library('lubridate')
if (!require('magrittr')) {install.packages('magrittr')};library('magrittr')
if (!require('rvest')) {install.packages('rvest')};library('rvest')
if (!require('slackr')) {install.packages('slackr')};library('slackr')
if (!require('XML')) {install.packages('XML')};library('XML')
#Basics
linux <- "/home/pi/R/projects"
setwd(linux)
slackr_setup(config_file = ".coronapush")
y <- 1
#scraping
bag_alt <- xml2::read_html('https://www.bag.admin.ch/bag/de/home/krankheiten/ausbrueche-epidemien-pandemien/aktuelle-ausbrueche-epidemien/novel-cov/situation-schweiz-und-international.html')
anzahl_faelle_alt <- bag_alt %>%
html_nodes('#2030838475+ .mod-table tr:nth-child(1) td:nth-child(2)') %>%
html_text()
anzahl_faelle_alt <- gsub("[[:space:]]","",anzahl_faelle_alt) %>%
as.integer()
anzahl_tote_alt <- bag_alt %>%
html_nodes('#2030838475+ .mod-table tr:nth-child(3) td:nth-child(2)') %>%
html_text()
anzahl_tote_alt <- gsub("[[:space:]]",anzahl_tote_alt) %>%
as.integer()
anzahl_tests_alt <- bag_alt %>%
html_nodes('tr:nth-child(4) td:nth-child(2)')%>%
html_text()
anzahl_tests_alt <- gsub("[[:space:]]",anzahl_tests_alt) %>%
as.integer()
#prepare loop
tests_gemacht <- F
faelle_gemacht <- F
tote_gemacht <- F
y <- 1
#Loop
repeat {
bag_neu <- xml2::read_html('https://www.bag.admin.ch/bag/de/home/krankheiten/ausbrueche-epidemien-pandemien/aktuelle-ausbrueche-epidemien/novel-cov/situation-schweiz-und-international.html')
anzahl_faelle_neu <- bag_neu %>%
html_nodes('#2030838475+ .mod-table tr:nth-child(1) td:nth-child(2)') %>%
html_text()
anzahl_faelle_neu <- gsub("[[:space:]]",anzahl_faelle_neu) %>%
as.integer()
anzahl_tote_neu <- bag_neu %>%
html_nodes('#2030838475+ .mod-table tr:nth-child(3) td:nth-child(2)') %>%
html_text()
anzahl_tote_neu <- gsub("[[:space:]]",anzahl_tote_neu) %>%
as.integer()
anzahl_tests_neu <- bag_neu %>%
html_nodes('tr:nth-child(4) td:nth-child(2)')%>%
html_text()
anzahl_tests_neu <- gsub("[[:space:]]",anzahl_tests_neu) %>%
as.integer()
if(anzahl_faelle_alt != anzahl_faelle_neu) {
ansteckungen <- paste0("Neue BAG-Zahlen: ",anzahl_faelle_neu," Ansteckungen,")
slackr_bot(ansteckungen)
faelle_gemacht <- T
}
if(anzahl_tests_alt != anzahl_tests_neu) {
anzahl_tests_neu_formatiert <- format(anzahl_tests_neu,big.mark = "'")
tests <- paste0(anzahl_tests_neu_formatiert," Tests")
slackr_bot(tests)
tests_gemacht <- T
}
if(tests_gemacht == T && faelle_gemacht == T) {
positivitaet <- anzahl_faelle_neu/anzahl_tests_neu*100
positivitaet<- round(positivitaet,1) %>%
as.character()
positivitaet <- paste0(positivitaet,"% Positivität")
slackr_bot(positivitaet)
}
if(anzahl_tote_alt != anzahl_tote_neu) {
tote <- paste0(anzahl_tote_neu," Tote")
slackr_bot(tote)
tote_gemacht <- T
}
if(tests_gemacht == T && faelle_gemacht == T && tote_gemacht == F){
tote <- paste0(anzahl_tote_neu," Tote")
slackr_bot(tote)
tote_gemacht <- T
}
y <-y+1
if (faelle_gemacht == T && tote_gemacht == T && tests_gemacht == T){
quit()
}
if (y == 15000){
quit()
}
x <- runif(1,2)
Sys.sleep(x)
print(y)
}
但是,当然,如果顺序确实很重要,但是具有其自身的含义并且可以任意更改,则应该使用std :: vector。让该算法将事物放置在需要的地方。