列表

详情


NC24735. [USACO 2010 Mar G]Barn Allocation

描述

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.
Consider both a barn with 5 stalls that have the capacities shown and a set cow requests: Stall id:    1   2   3   4   5            +---+---+---+---+---+ Capacity:  | 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) FJ can't satisfy all four cows, since there are too many requests for stalls 3 and 4. Noting that Cow 2 requests an interval that includes stalls 3 and 4, we test the hypothesis that cows 1, 3, and 4 can have their requested stalls. No capacity is exceeded, so the answer for this set of data is 3 -- three cows (1, 3, and 4) can have their requests satisfied.

输入描述

* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 contains a single integer: C_i
* Lines N+2..N+M+1: Line i+N+1 contains two integers: A_i and B_i

输出描述

* Line 1: The maximum number of requests that can be satisfied

示例1

输入:

5 4
1
3
2
1
3
1 3
2 5
2 3
4 5

输出:

3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 45ms, 内存消耗: 2668K, 提交时间: 2023-04-02 16:53:57

#include<bits/stdc++.h>
using namespace std;
struct Pr {
	int l{0}, r{0};
};
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<int> c(n + 2);
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
	}
	vector<Pr> qu(m + 5);
	for (int i = 1; i <= m; i++) {
		cin >> qu[i].l >> qu[i].r;
	}
	sort(qu.begin() + 1, qu.begin() + 1 + m, [&](const Pr& A, const Pr& B) {
		return A.l < B.l;
	});
	priority_queue<int> pq;
	vector<int> cnt(n + 2);
	int ans = 0, idx = 1;
	for (int i = 1; i <= n; i++) {
		while (idx <= m && qu[idx].l == i) {
			pq.push(qu[idx].r);
			cnt[qu[idx].r]++;
			idx++;
		}
		while (pq.size() > ans + c[i]) {
			cnt[pq.top()]--;
			pq.pop();
		}
		ans += cnt[i];
	}
	cout << ans << "\n";
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 46ms, 内存消耗: 4296K, 提交时间: 2020-04-24 14:38:54

#include <bits/stdc++.h>
using namespace std;
const int MAXN=100100;
pair<int,int> p[MAXN];
priority_queue<int> q;
int s[MAXN],num[MAXN];
int n,m,ans,now;
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(register int i=1;i<=n;i++) cin>>s[i];
    for(register int i=1;i<=m;i++) cin>>p[i].first>>p[i].second;
    sort(p+1,p+m+1);
    p[m+1].first=INT_MAX;
    for(register int i=1;i<=n;i++)
    {
        while(p[now+1].first<=i) now++,q.push(p[now].second),num[p[now].second]++;
        while(q.size()-ans>s[i]) num[q.top()]--,q.pop();
        ans+=num[i];
    }
    cout<<ans<<endl;
    return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 53ms, 内存消耗: 2504K, 提交时间: 2020-02-26 21:23:00

#include<bits/stdc++.h>
using namespace std;
int n,m,s[110000],t,sum[110000],ans=0;
pair<int,int> a[110000];
priority_queue<int> q;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	scanf("%d",&s[i]);
	for(int i=1;i<=m;i++)
	scanf("%d%d",&a[i].first,&a[i].second);
	sort(a+1,a+m+1);
	a[m+1].first=n+1;
	for(int i=1;i<=n;i++)
	{
		while(a[t+1].first<=i)
		q.push(a[++t].second),sum[a[t].second]++;
		while(q.size()>s[i]+ans) sum[q.top()]--,q.pop();
		ans+=sum[i];
	}
	printf("%d\n",ans);
}

上一题