NC24083 [USACO 2017 Dec P]Greedy Gift Takers
NC24083 [USACO 2017 Dec P]Greedy Gift Takers
题目
题目描述
Farmer John's nemesis, Farmer Nhoj, has N cows (\(1≤N≤10^5\)), 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 cic_ici cows (\(0≤c_i≤N−1\)).
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 \(c_1,c_2,…,c_N\) .
输出描述
Please output the number of cows who cannot receive any gifts.
示例1
输入
3
1 2 0
输出
1
题解
思路
知识点:二分。
第一个难点是要注意到,可行性函数是关于队伍的第几头牛的,因为发现如果某头牛拿不到礼物那他之后的牛一定拿不到,而拿的到的牛之前的牛一定拿得到,因此答案位置处于函数零点,且函数单调。
第二个难点是如何写可行性检验的函数。我们先统计 \(mid\) 牛之前的牛的 \(cnt[i]\) ,表示牛插到第 \(i(1\leq i < mid)\) 个位置的数量,而且 \(cnt[i]\) 只会随 \(mid\) 靠前减少,而不会更改或者变多,因为牛要么排到后面要么还在 \(1\leq i <mid\) 。对于第 \(i\) 个位置,若 \(\sum_{i=1}^i cnt[i] \geq i\) ,说明 \(mid\) 牛之前肯定会存在 \(\geq i\) 的牛插队到 \(\leq i\) 的位置,由于,所以队伍始终至少在 \(i\) 之前循环,所以不可能到达 \(mid\) 牛;而 \(\sum_{i=1}^i cnt[i] < i\) 说明,在 \(mid\) 牛之前会存在 \(< i\) 的牛插队到 \(\leq i\) 的位置,即 \(\leq i\) 的队伍一定长度缩短,而且这些牛之后会到同样位置,因此 \(cnt[i]\) 不增,即 \(\leq i\) 排队的牛会不增,表示 \(\leq i\) 的队伍必定不会循环,特别地如果 \(\sum_{i=1}^{mid-1} cnt[i] < mid-1\) ,即表示 \(\leq mid-1\) 的队伍必定不会循环,即可行。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int n;
int c[100007], cnt[100007];
bool check(int mid) {///判断是否拿得到
    memset(cnt, 0, sizeof(cnt));
    int sum = 0;
    for (int i = 1;i < mid;i++) cnt[c[i]]++;
    for (int i = 1;i < mid;i++) {
        sum += cnt[i];
        if (sum >= i) return 0;
    }
    return 1;
}
int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> c[i], c[i] = n - c[i];
    int l = 1, r = n;
    while (l <= r) {
        int mid = l + r >> 1;
        if (check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    cout << n - r << '\n';///n减最后一个拿得到的位置(第一个拿不到的位置减一) = 拿不到的总数
    return 0;
}
												
											NC24083 [USACO 2017 Dec P]Greedy Gift Takers的更多相关文章
- [USACO 2017 Dec Gold] Tutorial
		
Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...
 - USACO Section 1.1-2 Greedy Gift Givers
		
Greedy Gift Givers 贪婪的送礼者 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那 ...
 - usaco training <1.2 Greedy Gift Givers>
		
题面 Task 'gift1': Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided t ...
 - [BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树
		
Description Farmer John's nemesis, Farmer Nhoj, has NN cows (1≤N≤10^5), conveniently numbered 1…N. T ...
 - [USACO 2017DEC] Greedy Gift Takers
		
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5139 [算法] 二分答案 时间复杂度 : O(NlogN^2) [代码] #incl ...
 - [USACO] 2017 DEC Bronze&Silver
		
link:http://www.usaco.org/index.php?page=dec17results Problem A(Bronze) 这是一道非常简单的判断重叠面积的题目,但第一次提交仍会出 ...
 - [USACO17DEC]Greedy Gift Takers
		
题目描述 Farmer John's nemesis, Farmer Nhoj, has NN cows (1 \leq N \leq 10^51≤N≤105 ), conveniently numb ...
 - USACO Section 1.1 Greedy Gift Givers 解题报告
		
题目 问题描述 有若干个朋友,朋友之间可以选择互相赠送一些有价值的礼物.一个人可以选择将一部分钱分给若干个朋友,例如某人送给其他两个人钱,总共赠送3元,两个人平均分,原本应该是每人1.5元,但是只能取 ...
 - P4090 [USACO17DEC]Greedy Gift Takers
		
题目链接 题意分析 首先 如果当前序列中一头奶牛拿不到礼物的话 那么他后面的奶牛也拿不到礼物 所以我们可以二分 由于可以操作无限次 所以我们对于当前\([1,mid)\)的奶牛按照\(c\)值排序之后 ...
 
随机推荐
- 2021.07.02 UVa1197 多路归并模板
			
2021.07.02 UVa1197 多路归并模板 UVA11997 K Smallest Sums - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 分析: 题解 UVA11997 ...
 - 别像弱智一样提问 Stop-Ask-Questions-The-Stupid-Ways
			
https://github.com/xcr1234/Stop-Ask-Questions-The-Stupid-Ways 你真的准备好了吗? 感谢群友 for you 提供 避免 xy-proble ...
 - echarts踩坑总结
			
1,有关echarts引用的相关报错直接写这句 import * as echarts from 'echarts' 2,折线图 chartsObj = { tooltip: { trigger: ...
 - ChCore Lab2 内存管理 实验笔记
			
本文为上海交大 ipads 研究所陈海波老师等人所著的<现代操作系统:原理与实现>的课程实验(LAB)的学习笔记的第二篇.所有章节的笔记可在此处查看:chcore | 康宇PL's Blo ...
 - [笔记] Slope Trick:解决一类凸代价函数的DP优化问题
			
原理 当序列 DP 的转移代价函数满足 连续: 凸函数: 分段线性函数. 时,可以通过记录分段函数的最右一段 \(f_r(x)\) 以及其分段点 \(L\) 实现快速维护代价的效果. 如:$ f(x) ...
 - c/c++递归打印文件夹
			
调用linux的系统函数,实现tree的功能,递归打印文件夹 使用到得函数: DIR *opendir(const char *name); // 打开文件夹 struct dirent *readd ...
 - 详解六大国产CPU处理器
			
一个执着于技术的公众号 CPU作为计算机设备的运算和控制核心,负责指令读取.译码与执行,因研发门槛高.生态构建难,被认为是集成电路产业中的"珠穆朗玛峰". 纵观全球,Intel.A ...
 - 154寻找旋转排序数组中的最小值II
			
title: 寻找旋转排序数组中的最小值II 题目描述 题目链接:寻找旋转排序数组中的最小值II 解题思路 和上题同理:数组特点有 nums[mid] < nums[right],最小值肯定在m ...
 - 如何彻底禁止 macOS Monterey 自动更新,去除更新标记和通知
			
请访问原文链接:如何彻底禁止 macOS Monterey 自动更新,去除更新标记和通知,查看最新版.原创作品,转载请保留出处. 作者主页:www.sysin.org 随着 macOS Montere ...
 - 【爬虫+情感判定+Top10高频词+词云图】“刘畊宏“热门弹幕python舆情分析
			
一.背景介绍 最近一段时间,刘畊宏真是火出了天际,引起一股全民健身的热潮,毕竟锻炼身体,是个好事! 针对此热门事件,我用Python的爬虫和情感分析技术,针对小破站的弹幕数据,分析了众多网友弹幕的舆论 ...