[USACO09DEC]音符Music Notes (二分、STL)
https://www.luogu.org/problem/P2969
题目描述
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
约翰准备教他的奶牛们弹一首歌.这首歌由N(1<=n<= 50000)个音阶组成,第i个音阶要敲击Bi<=10000次.奶牛从第0时刻开始弹,因此他从0时刻到Bi-1时刻都是敲第1个音阶, 然后他从B1时刻到B1+B2-1时刻敲第2个音阶,从B1+B2到B1+B2+B3-1时刻敲第3个音阶……现在有q(i<q<50000)个问题:在时间段区间t,T+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
输入
输出
题解:
二分查找
STL的upper_bound函数,找到第一个大于待查元素的值,用法见代码。类似的还有lower_bound,找到的是第一个不小于待查元素的值。
下面介绍一下STL中的lower_bound()和upper_bound():(原文链接:https://blog.csdn.net/qq_40160605/article/details/80150252)
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
在从大到小的排序数组中,重载lower_bound()和upper_bound()
lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
STL写法:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI=acos(-);
const int maxn=;
using namespace std; int n,q;
int B[maxn];
int ans[maxn]; int main()
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&B[i]);
ans[i]=ans[i-]+B[i];
} for(int i=;i<q;i++)
{
int x;
scanf("%d",&x);
printf("%d\n",upper_bound(ans+,ans++n,x)-ans);
}
return ;
}
二分写法:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI=acos(-);
const int maxn=;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n,q;
int l,r,mid;
int B[];
int num[]; int main()
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&B[i]);
num[i]=num[i-]+B[i];
}
for(int i=;i<=q;i++)
{
int x;
scanf("%d",&x);
int l,r;
l=;
r=n;
while(l<=r)
{
int mid=(l+r)/;
if(num[mid]<=x)
l=mid+;
else r=mid-;
}
printf("%d\n",l);
}
}
[USACO09DEC]音符Music Notes (二分、STL)的更多相关文章
- 洛谷 P2969 [USACO09DEC]音符Music Notes
P2969 [USACO09DEC]音符Music Notes 题目描述 FJ is going to teach his cows how to play a song. The song cons ...
- Bridging signals(二分 二分+stl dp)
欢迎参加——每周六晚的BestCoder(有米!) Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6 ...
- Can you find it?(二分 二分+STL set map)
Can you find it? Time Limit : 10000/3000ms (Java/Other) Memory Limit : 32768/10000K (Java/Other) T ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- CodeForces - 706B 二分stl
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- 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 ...
- P1540 机器翻译 模拟
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...
- noip2012开车旅行 题解
题目大意: 给出n个排成一行的城市,每个城市有一个不同的海拔.定义两个城市间的距离等于他们的高度差的绝对值,且绝对值相等的时候海拔低的距离近.有两个人轮流开车,从左往右走.A每次都选最近的,B每次都选 ...
- 电信学院第一届新生程序设计竞赛题解及std
首先非常感谢各位同学的参加,还有出题验题同学的辛勤付出 昨天想偷懒就是不想再把我C++11的style改没了,大家看不懂的可以百度一下哦,懒得再写gcc了,毕竟代码是通的 //代表的是行注释,所以那个 ...
随机推荐
- 2,StructuredStreaming的事件时间和窗口操作
推荐阅读:1,StructuredStreaming简介 使用Structured Streaming基于事件时间的滑动窗口的聚合操作是很简单的,很像分组聚合.在一个分组聚合操作中,聚合值被唯一保存在 ...
- java Kafka 简单应用实例
kafka官方中文文档 http://kafka.apachecn.org/ java Kafka 简单应用实例 下面是Linux下的单机模式:https://blog.csdn.net/fct2 ...
- python3 sort list
1. 对元素指定的某一部分进行排序,关键字排序 s = ['release.10.txt','release.1.txt','release.2.txt','release.14.txt','rele ...
- POJ 3911:Internet Service Providers
Internet Service Providers Time Limit: 2MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I ...
- 开源PLM软件Aras详解八 Aras之RelationshipTypes关系类详解
在Aras中,在之前ItemType解析中有提到,Aras中实际ItemType对应的就是一张表,那么,ItemType与ItemType之间是如何关联的呢, 如果我们需要捋清楚ItemType与It ...
- ES6 之 Integer数据类型
1.简介(仅仅是提案) js所有数字都保存成64为浮点数,这就决定了整数的精确程度只能到53个二进制位. 大于这个范围的整数,js是无法精确表示的,这使得js不合适进行科学和金融方面的精确计算. 故引 ...
- Vue-router(3)之 router-link 和 router-view 使用
router 导入 import Vue from 'vue' import Router from 'vue-router' import order from '@/view/New/order. ...
- HDU 1298 T9 字典树+DFS
必须要批评下自己了,首先就是这个题目的迟疑不定,去年做字典树的时候就碰到这个题目了,当时没什么好的想法,就暂时搁置了,其实想法应该有很多,只是居然没想到. 同样都是对单词进行建树,并插入可能值,但是拨 ...
- 浅copy
person=['aaa',['a',bbb'] p1=copy.copy(person) p2=person[:] p3=list(person) p4=person.copy() print(ty ...
- 利用python分析泰坦尼克号数据集
1 引言 刚接触python与大数据不久,这个是学长给出的练习题目.知识积累太少,学习用了不少的时间.尽量详细的写,希望对各位的学习有所帮助. 2 背景 2.1 Kaggle 本次数据集来自于Kagg ...