——wqs二分
想到這個這題就完了。
賽時沒想到這個你就完了。
時間複雜度 o(n
loga)
o(n\log a)
o(nloga)
不難發現這題有凸性,可以wqs二分。
我們把只能選 k 個段的限制去掉,二分乙個罰款額,每次多選乙個段,貢獻就要罰款,這樣可以調整到選 k 個的答案。
最後把罰款後的貢獻再加上 k × 罰款額。
去掉限制也不會?dp[
i]dp[i]
dp[i
] 表示前 i
ii 個最優答案即可。同時別忘了記錄段數(的最值)。
沒在noi考綱上啊,所以還是有可能考的
#
include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using
namespace std;
#define
maxn
500005
#define
lllong
long
#define
dbdouble
#define
endl
putchar
('\n')#
define
lowbit
(x)(
-(x)
&(x))#
define
fifirst
#define
sesecond
namespace
while
(s >=
'0'&& s <=
'9')
return f * x;
}void
putpos
(ll x)
void
putnum
(ll x)
if(x<0)
return
putpos
(x);
}void
aiput
(ll x,
int c)
int n,m,s,o,k;
int a[maxn]
,b[maxn]
;int f[maxn]
,st[maxn]
,hd,tl;
ll dp[maxn]
,ct[maxn]
,ans;
intheck
(ll ad)
dp[0]
=0; ct[0]
=0;for
(int i = m;i <= n;i ++)}
ans = dp[n]
;return ct[n];}
}int
main()
int l =
-500000
,r =
500000
,md;
while
(l < r)
int cn =
heck
(l);
ans -= l*
1ll*k;
aiput
(ans,
'\n');
return0;
}
二分入門題
在乙個遞增的序列裡,查詢元素是否存在,若存在輸出yes,不存在輸出no.本題多組資料,首先輸入乙個數字n,然後輸入n個數,資料保證數列遞增,然後再輸入乙個查詢數字。若存在輸出yes,不存在輸出no.4 1 3 5 8 3 yes include include includeusing namesp...
二分模版題
給定乙個按照公升序排列的長度為n的整數陣列,以及 q 個查詢。對於每個查詢,返回乙個元素k的起始位置和終止位置 位置從0開始計數 如果陣列中不存在該元素,則返回 1 1 輸入格式 第一行包含整數n和q,表示陣列長度和詢問個數。第二行包含n個整數 均在1 10000範圍內 表示完整陣列。接下來q行,每...
二分查詢簡單題
leetcode 35.search insert position 最普通的binary search,若target存在,則返回所在下標 若target不存在,則返回target待插入的位置,本質上就是實現lower bound函式 無論mid left right left 2,還是mid l...