NC24866 [USACO 2009 Dec S]Music Notes
NC24866 [USACO 2009 Dec S]Music Notes
题目
题目描述
FJ is going to teach his cows how to play a song. The song consists of N (1 <= N <= 50,000) notes, and the i-th note lasts for Bi (1 <= Bi <= 10,000) beats (thus no song is longer than 500,000,000 beats). The cows will begin playing the song at time 0; thus, they will play note 1 from time 0 through just before time B1, note 2 from time B1 through just before time B1 + B2, etc.
However, recently the cows have lost interest in the song, as they feel that it is too long and boring. Thus, to make sure his cows are paying attention, he asks them Q (1 <= Q <= 50,000) questions of the form, "In the interval from time T through just before time T+1, which note should you be playing?" The cows need your help to answer these questions which are supplied as Ti (0 <= Ti <= end_of_song).
Consider this song with three notes of durations 2, 1, and 3 beats:
Beat: 0 1 2 3 4 5 6 ...
|----|----|----|----|----|----|--- ...
1111111111 : :
22222: :
333333333333333:
Here is a set of five queries along with the resulting answer:
Query Note
2 2
3 3
4 3
0 1
1 1
输入描述
* Line 1: Two space-separated integers: N and Q
* Lines 2..N+1: Line i+1 contains the single integer: Bi
* Lines N+2..N+Q+1: Line N+i+1 contains a single integer: Ti
输出描述
* Lines 1..Q: Line i of the output contains the result of query i as a single integer.
示例1
输入
3 5
2
1
3
2
3
4
0
1
输出
2
3
3
1
1
题解
思路
知识点:二分。
先做前缀和得到每个音符的终止位置,发现音符 \(i\) 出现在 \([sum_{i-1},sum_{i})\) 。要查找 \([T,T+1)\) 区间的音符,于是二分查找第一个位置 \(i\) 使得 \(sum_i>T\) ,就有 \([T,T+1) \subseteq [sum_{i-1},sum_{i})\) 那么 \(i\) 就是答案。
时间复杂度 \(O(q \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int B[50007];
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1;i <= n;i++) cin >> B[i], B[i] += B[i - 1];
while (q--) {
int t;
cin >> t;
cout << upper_bound(B + 1, B + n + 1, t) - B << '\n';
}
return 0;
}
NC24866 [USACO 2009 Dec S]Music Notes的更多相关文章
- BZOJ1774[USACO 2009 Dec Gold 2.Cow Toll Paths]——floyd
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- BZOJ1775[USACO 2009 Dec Gold 3.Video Game Troubles]——DP
题目描述 输入 * 第1行: 两个由空格隔开的整数: N和V * 第2到第N+1行: 第i+1行表示第i种游戏平台的价格和可以在这种游戏平台上面运行的游 戏.包含: P_i, G_i还有G_i对由空格 ...
- USACO 2009 Dec cow toll paths 过路费-floyd
这道题首先要明确一点,那就是当你从一个点走到自己时,也是需要花费这个点点权值的费用.这个点卡了我两次QWQ 然后我比较喜欢分两步搞: 首先,我们利用floyd的一个性质:就是在更新其他点之间的路线时要 ...
- Usaco 滑雪比赛 Bobsledding, 2009 Dec(dp)
Description 滑雪比赛bobsled 贝西参加了一场高山急速滑雪比赛,滑道总长度为 L.出发时,她的初速度为 1,贝西可以加速 或减速,每过 1 米,她能将速度增加 1.减少 1 或保持不变 ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- [USACO 2017 Dec Gold] Tutorial
Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...
- USACO 2009 Open 干草塔 Tower of Hay
USACO 2009 Open 干草塔 Tower of Hay Description 为了调整电灯亮度,贝西要用干草包堆出一座塔,然后爬到牛棚顶去把灯泡换掉.干草 包会从传送带上运来,共会出现N包 ...
- USACO 2009 Feb 股票市场 Stock Market
USACO 2009 Feb 股票市场 Stock Market Description 尽管奶牛们天生谨慎,她们仍然在住房抵押信贷市场中大受打击,现在她们准备在股市 上碰碰运气.贝西开挂了,她知道S ...
随机推荐
- JavaWeb和WebGIS学习笔记(七)——MapGuide Open Source安装、配置以及MapGuide Maestro发布地图——超详细!目前最保姆级的MapGuide上手指南!
JavaWeb和WebGIS学习笔记(七)--MapGuide Open Source安装.配置以及MapGuide Maestro发布地图 超详细!目前最保姆级的MapGuide上手指南! 系列链接 ...
- java自带的四种线程池
java预定义的哪四种线程池? newSingleThreadExexcutor:单线程数的线程池(核心线程数=最大线程数=1) newFixedThreadPool:固定线程数的线程池(核心线程数= ...
- 大一/初学者学C语言前必看!!!(建议收藏)
目录 数据类型 常量.变量 数组 字符串.转义字符 选择语句 循环语句 函数 操作符 结构体 指针 神秘的学习资料基地jq.qq.com/?_wv=1027&k=5kWJsY1z 一.数据类 ...
- Ubuntu环境Docker+K8s+Dashboard的安装配置(无坑亲测)
安装之前的准备: 安装docker 使用国内 daocloud 一键安装命令: curl -sSL https://get.daocloud.io/docker | sh 直接从dockerhub下载 ...
- 为什么Redis要比Memcached更火?
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 前言 我们都知道,Redis和Memcached都是内存数据库,它们的访问速度非常之快.但我们 ...
- 解构HE2E中的Kubernetes技术应用
摘要:我们从Kubernetes技术应用的角度解构华为云DevCloud HE2E DevOps实践. 本文分享自华为云社区<解构HE2E中的Kubernetes技术应用>,作者: 敏捷小 ...
- SSO 方案演进
背景介绍 随着业务与技术的发展,现今比以往任何时候都更需要单点登录 SSO 身份验证. 现在几乎每个网站都需要某种形式的身份验证才能访问其功能和内容. 随着网站和服务数量的增加,集中登录系统已成为一种 ...
- 『忘了再学』Shell基础 — 15、环境变量(三)
目录 1.LANG语系变量介绍 2.如何查看Linux中支持的语系 3.查看当前系统的语系 4.总结 提示: 在Linux系统中,环境变量分为两种.一种是用户自定义的环境变量,另一种是系统自带的环境变 ...
- linux篇-tomcat:Cannot find /usr/local/tomcat1/bin/setclasspath.sh
首先看下报错代码: Cannot find /usr/local/tomcat1/bin/setclasspath.sh This file is needed to run this program ...
- poj3784(对顶堆)
题意:多组数据,让你求出1~i(i为奇数&&i<=n)的中位数 思路:首先复杂度必为O(n)或O(nlogn)的(数据范围) 思索,如果题目要求1次中位数,好求!排个序,取a[( ...