日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

洛谷P1937 [USACO10MAR]仓配置Barn Allocation

發(fā)布時(shí)間:2023/12/1 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷P1937 [USACO10MAR]仓配置Barn Allocation 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

Farmer John recently opened up a new barn and is now accepting stall allocation requests from the cows since some of the stalls have a better view of the pastures.

The barn comprises N (1 <= N <= 100,000) stalls conveniently numbered 1..N; stall i has capacity C_i cows (1 <= C_i <= 100,000). Cow i may request a contiguous interval of stalls (A_i, B_i) in which to roam (1 <= A_i <= N; A_i <= B_i <= N), i.e., the cow would like to wander among all the stalls in the range A_i..B_i (and the stalls must always have the capacity for her to wander).

Given M (1 <= M <= 100,000) stall requests, determine the maximum number of them that can be satisfied without exceeding stall

capacities.

農(nóng)夫約翰最近開(kāi)了一個(gè)新的牲口棚屋,并且現(xiàn)在接受來(lái)自奶牛的分配畜欄請(qǐng)求因?yàn)槠渲械囊恍┬髾谟懈蔑L(fēng)景。

畜欄包括N個(gè)畜欄(1 ≤ N ≤ 100,000),方便起見(jiàn),我們把它們編號(hào)為1..N,畜欄i能容納Ci只牛(1 ≤ Ci ≤ 100,000),第i只牛需要連續(xù)編號(hào)畜欄(從Ai到Bi)來(lái)漫步其中,

(1 ≤ Ai ≤ N; Ai ≤ Bi ≤ N),換言之,這只牛想要在編號(hào)范圍為Ai..Bi的畜欄漫步(所有它想要畜欄必須實(shí)施為它空出位置來(lái)供它散步)

給出M個(gè)畜欄分配請(qǐng)求(1 ≤ M ≤ 100,000),回答最多能滿足多少只牛的要求(不增加另外畜欄)

考慮以下例子:

畜欄號(hào): 1 2 3 4 5 +---+---+---+---+---+ 容納空間: | 1 | 3 | 2 | 1 | 3 | +---+---+---+---+---+ Cow 1 XXXXXXXXXXX (1, 3) Cow 2 XXXXXXXXXXXXXXX (2, 5) Cow 3 XXXXXXX (2, 3) Cow 4 XXXXXXX (4, 5)

約翰顯然不能滿足所有的牛,因?yàn)樾髾?,4請(qǐng)求太多了

經(jīng)過(guò)試驗(yàn),我們發(fā)現(xiàn),我們能滿足牛1,3,4需要,所以這組數(shù)據(jù)答案為3

輸入輸出格式

輸入格式:

?

第一行包括兩個(gè)以空格隔開(kāi)的正整數(shù):N,M

第二行到第N+1行:第i+1行包括一個(gè)整數(shù):Ci

第N+2到第N+M+1行:第i+N+1 包括兩個(gè)整數(shù)Ai、Bi

?

輸出格式:

?

僅一行:能滿足的最大需要

?

輸入輸出樣例

輸入樣例#1:
5 4 1 3 2 1 3 1 3 2 5 2 3 4 5 輸出樣例#1:
3

說(shuō)明

Source: USACO 2010 March Gold

Translator: @chrome01

分析:這道題其實(shí)和借教室那道題差不多,可以考慮用線段樹(shù)來(lái)維護(hù),我們考慮一個(gè)區(qū)間是只用考慮它的最小值的,如果最小值都能滿足條件,那么肯定是能夠滿足條件的,那么怎么樣才能讓題目給定的區(qū)間不重復(fù)呢?考慮貪心,我們先按照右端點(diǎn)從小到大排序,再按照左端點(diǎn)從大到小排序,這樣可以保證區(qū)間之間盡量不要互相影響,最后先查詢最小值,再修改就好了.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath>using namespace std;const int inf = 0x7ffffff;int n,m,minn[300010],flag[300010],ans; bool flag2 = false;struct node {int a,b; }e[100010];bool cmp(node x,node y) {if (x.b != y.b)return x.b < y.b;elsereturn x.a > y.a; }void pushup(int o) {minn[o] = min(minn[o * 2],minn[o * 2 + 1]); }void pushdown(int o) {if (flag[o]){flag[o * 2] += flag[o];flag[o * 2 + 1] += flag[o];minn[o * 2] -= flag[o];minn[o * 2 + 1] -= flag[o];}flag[o] = 0; }void build(int l,int r,int o) {if (l == r){scanf("%d",&minn[o]);return;}int mid = (l + r) >> 1;build(l,mid,o * 2);build(mid + 1,r,o * 2 + 1);pushup(o); }void update(int l,int r,int o,int x,int y) {if (x <= l && r <= y){flag[o]++;minn[o]--;return;}pushdown(o);int mid = (l + r) >> 1;if (x <= mid)update(l,mid,o * 2,x,y);if (y > mid)update(mid + 1,r,o * 2 + 1,x,y);pushup(o); }int query(int l,int r,int o,int x,int y) {if (x <= l && r <= y)return minn[o];pushdown(o);int mid = (l + r) >> 1,res = inf;if (x <= mid)res = min(query(l,mid,o * 2,x,y),res);if (y > mid)res = min(query(mid + 1,r,o * 2 + 1,x,y),res);return res; }int main() {scanf("%d%d",&n,&m);build(1,n,1);for (int i = 1; i <= m; i++)scanf("%d%d",&e[i].a,&e[i].b);sort(e + 1,e + 1 + m,cmp);for (int i = 1; i <= m; i++){if (query(1,n,1,e[i].a,e[i].b) <= 0)continue;update(1,n,1,e[i].a,e[i].b);ans++;}printf("%d\n",ans);return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/zbtrs/p/7510346.html

總結(jié)

以上是生活随笔為你收集整理的洛谷P1937 [USACO10MAR]仓配置Barn Allocation的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。