列表

详情


NC24083. [USACO 2017 Dec P]Greedy Gift Takers

描述

Farmer John's nemesis, Farmer Nhoj, has N cows (), conveniently numbered 1…N. They have unexpectedly turned up at Farmer John's farm, so the unfailingly polite Farmer John is attempting to give them gifts.

To this end, Farmer John has brought out his infinite supply of gifts, and Nhoj's cows have queued up in front of him, with cow 1 at the head of the queue and cow N at the tail. Farmer John was expecting that at every timestep, the cow at the head of the queue would take a gift from Farmer John and go to the tail of the queue. However, he has just realized that Nhoj's cows are not that polite! After receiving her gift, each cow may not go to the tail of the queue, but rather may cut some number of cows at the tail, and insert herself in front of them. Specifically, cow ii will always cut exactly c_i cows ().

Farmer John knows that some cows might receive multiple gifts; as he has an infinite supply, this does not worry him. But he is worried that some cows might become unhappy if they do not get any gifts.

Help Farmer John find the number of cows who never receive any gifts, no matter how many gifts are handed out.

输入描述

The first line contains a single integer, N.
The second line contains N space-separated integers .

输出描述

Please output the number of cows who cannot receive any gifts.

示例1

输入:

3
1 2 0

输出:

1

原站题解

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

C++(clang++ 11.0.1) 解法, 执行用时: 144ms, 内存消耗: 1152K, 提交时间: 2023-06-28 11:52:48

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,a[N],b[N];
bool cmp(int x) {
	for(int i=1; i<x; i++) b[i]=a[i];
	sort(b+1,b+x);
	int min=n-x;
	for(int i=1; i<x; i++) {
		if(b[i]>min) return 0;
		min++;
	}
	return 1;
}
int main() {
	cin>>n;
	for(int i=1; i<=n; i++) cin>>a[i];
	int l=0,r=n;
	while(l<r) {
		int mid=l+r+1>>1;
		if(cmp(mid)) l=mid;
		else r=mid-1;
	}
	cout<<n-l<<endl;
	return 0;
}

上一题