poj1201 Intervals(差分約束)

2021-09-27 02:44:33 字數 2195 閱讀 8937

給定 n 個區間 [ai,bi]和 n 個整數 ci。

你需要構造乙個整數集合 z,使得∀i∈[1,n],z 中滿足ai≤x≤bi的整數 x 不少於 ci 個。

求這樣的整數集合 z 最少包含多少個數。

輸入格式

第一行包含整數 n。

接下來n行,每行包含三個整數ai,bi,ci。

輸出格式

輸出乙個整數表示結果。

資料範圍

1≤n≤50000

0≤ai,bi≤50000

1≤ci≤bi−ai+1

輸入樣例:

5

3 7 3

8 10 3

6 8 1

1 3 1

10 11 1

輸出樣例:

6
本體可化為差分約束來做。

設f [ i ] 為0~ i區間至少包含的總數,則對於每一組資料來說

ai 、 bi為區間端點,則可以化為 f [ bi ] - f[ ai ] >=ci  即 f[bi] >= f[ai] +ci 

同時要注意本題中所含有的隱藏條件,每乙個數只能選一次, 所以1>=f[ i ] - f[ i - 1 ] >=0

最後就轉化為了, 求 區間 1 - n的最少包含的數 , 即 f[n] - f[-1] > = c 

由於不能出現負一, 所以整體右移1。

ac code:

#include#include#include#include#define inf 0x3f3f3f3f

using namespace std;

const int n = 500006;

struct edge

edge[2*n+5];

int head[n+5],tot;

int d[n+5];bool vis[n+5];

inline void add(int from,int to,int dis)

void spfa(int u)}}

}}void init()

int main()

for(int i = 1;i<=50001;++i)

spfa(0);

printf("%d",d[50001]);

}

上述**過多冗餘,導致跑了700+ms,下邊進行範圍上的優化,跑60+ms

ac code:

#includeusing namespace std;

#define inf 0x3f3f3f3f

const int n = 50000;

struct edge

edge[n*3+5];

int head[n+5],tot;

int d[n+5];bool vis[n+5];

inline void add(int from,int to,int dis)

void spfa(int u)

edge[m];

int head[n],tot;

inline void add(int from,int to,int dis)

int d[n];

bool vis[n];

void spfa(int u)}}

}}inline void init()

int main()

for(int i = 1;i <= 50001;++i)

spfa(50001);

cout<<-d[0]#include #include #include #include using namespace std;

const int n = 50010, m = n * 3;

int n;

int h[n], w[m], e[m], ne[m], idx;

int dist[n];

bool st[n];

void add(int a, int b, int c)

void spfa()

while (q.size())}}

}}int main()

for (int i = 1; i <= 50001; i ++ )

spfa();

printf("%d\n", dist[50001]);

return 0;

}

差分約束 poj 1201 Interval

差分約束 poj 1201 又是一道vector tle。確實很好的差分約束。很好的差分約束,注意隱含條件 0 d i 1 d i 1 題目保證了沒負圈。include include include using namespace std define n 50005 define inf 100...

POJ 1201 樹狀陣列

給你n個區間,每個區間為 a,b 每個區間取c個數構成乙個集合,求集合最小容量 把區間按b排序,從第乙個區間開始取,從後往前取,這樣盡可能和後面的區間重複 另外如果我們發現當前區間取得個數已經超過了c,那麼只需要讓之前區間換就行,而總數是不變的,所以不用更新答案 求當前區間已經取了多少個數用樹狀陣列...

poj 1201 差分約束

傳送門 題意 ai到bi間至少有ci個元素,問所有數中至少有多少元素。額。這樣好像說不清楚,我拿樣例說下吧。3到7之間至少有3個元素,8到10之間至少有3個元素,6到8之間至少有1個元素,1到3之間至少有1個元素,10到11之間至少有1個元素。最少情況如下 1 2 3 4 5 6 7 8 9 10 ...