CodeForces 474B E(Contest #1)
题意:
给你一个数n,代表n段区间,接下来有n个数(a1,a2,...an)代表每段区间的长度,第一段区间为[1,a1],第二段区间为[a1+1,a1+a2],...第i段区间为[ai-1+1,ai-1+ai]
接着输入一个整数m,之后有m个数,问你这m个数,每个数所在的区间编号.
分析:
可以采用计数排序,建立一个数组,将第i段区间[ai-1+1,ai-1+ai]赋值为i,这样输入一个数很快就可以输出它所在的区间编号;
因为区间中的值是有序的,所以还可以采用二分查找法.
代码如下:
//方法一: #include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <iterator>
#include <vector> using namespace std; #define LL long long
#define INF 0x3f3f3f3f
#define MON 1000000007
#define MAXN 10000010
#define MAXM 1000010 const int maxn = ;
int a[maxn]; int main()
{
int n, m;
while(scanf("%d", &n)==)
{
memset(a, , sizeof(a));
int i, j;
int cnt = ;
int sum = ;
int pos = ;
for(i = ; i <= n; i++ )
{
pos++;
int x;
scanf("%d", &x);
sum += x; //sum的最大值为1e6超过了n的最大值,所以数组a的长度应该为1e6,不然会RE
for(j = cnt; j <= sum; j++ )
a[j] = pos;
cnt = sum+;
}
scanf("%d", &m);
for(i = ; i < m; i++ )
{
int y;
scanf("%d", &y);
printf("%d\n", a[y]);
}
} return ;
}
//方法二: 16:52:262016-07-25
#include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <iterator>
#include <vector> using namespace std; #define LL long long
#define INF 0x3f3f3f3f
#define MON 1000000007
#define MAXN 10000010
#define MAXM 1000010 const int maxn = ;
int a[maxn], b[maxn]; int lower_bound(int *A, int x, int y, int v)
{
int mid;
while(x < y)
{
mid = (y-x)/ + x;
if(v <= A[mid])
y = mid;
else
x = mid+;
} return x;
} int main()
{
int n, m;
while(scanf("%d", &n)==)
{
memset(a, , sizeof(a));
int i;
int sum = ;
int c = ;
for(i = ; i < n; i++ )
{
int x;
scanf("%d", &x);
sum += x;
a[c++] = sum;
}
scanf("%d", &m);
for(i = ; i < m; i++ )
{
int y;
scanf("%d", &y);
int cnt = lower_bound(a, , c, y);
printf("%d\n", cnt);
}
} return ;
}
CodeForces 474B E(Contest #1)的更多相关文章
- CodeForces 474B(标记用法)
CodeForces 474B Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- Codeforces 474B Worms 二分法(水
主题链接:http://codeforces.com/contest/474/problem/B #include <iostream> #include <cmath> #i ...
- Codeforces 659B Qualifying Contest【模拟,读题】
写这道题题解的目的就是纪念一下半个小时才读懂题...英文一多读一读就溜号... 读题时还时要静下心来... 题目链接: http://codeforces.com/contest/659/proble ...
- codeforces E. The Contest(最长上升子序列)
题目链接:https://codeforces.com/contest/1257/problem/E 题意:给三个序列k1,k2,k3,每个序列有一堆数,k1是前缀,k3是后缀,k2是中间,现可以从任 ...
- codeforces 659B Qualifying Contest
题目链接:http://codeforces.com/problemset/problem/659/B 题意: n个人,m个区.给出n个人的姓名(保证不相同),属于的区域,所得分数.从每个区域中选出成 ...
- 【37.74%】【codeforces 725D】Contest Balloons
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces 546B C(Contest #1)
Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...
- Codeforces April Fools Contest 2017
都是神题,我一题都不会,全程听学长题解打代码,我代码巨丑就不贴了 题解见巨神博客 假装自己没有做过这套
- CodeForces 474B Worms (水题,二分)
题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...
随机推荐
- 28.zookeeper单机(Standalones模式)和集群搭建笔记
zookeeper单机(Standalones模式)和集群搭建: 前奏: (1).zookeeper也可以在windows下使用,和linux一样可以单机也可以集群,具体就是解压zookeeper-3 ...
- T-SQL编程练习(带注释)
use test; GO /*创建自定义函数的格式: * create function 函数名(参数 数据类型) * returns 返回数据类型 as * begin * 代码 * end */ ...
- .NET WebBroswer内存释放
最近写的小说爬取工具遇到了性能瓶颈,使用多个Webbroswer控件预加载多个网页,内存会不断增加,达到400M左右,不能忍. 失败的例子 首先尝试把Webbroswer对象置为null,wb=nul ...
- python走起之第十二话
1. ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型 ...
- 学习UFT11.5历程(二)
1. QTP对象TO与RO TO: test object. 本地对象库里的封装对象 RO:run object. 运行封装对象 和TO.RO相关的几个函数有: GetTOProperty(“属性名” ...
- java第一天
今天完成的事情: [主线] 1.什么是接口??? 接口(interface)是类与类之间的一种约定,一般而言,实现某个接口,意味着该类必须实现接口中的所有方法. 2.接口的特性. ...
- 使用phpmailer发送smtp邮件时提示 SMTP Error: Could not authenticate 错误
使用phpmailer发送smtp邮件时提示 SMTP Error: Could not authenticate 错误 这个错误是验证出现错误, $mail->Port = 25; //SMT ...
- jieba分词
一.安装 pip 安装 或者 先下载http://pypi.python.org/pypi/jieba/ ,解压后运行python setup.py install 二.功能 1.分词 2.添加自定义 ...
- linux下的nodejs安装
linux下安装nodejs的方式: 1.源码安装 2.nvm安装 这里推荐使用nvm安装,避免下载nodejs源码: 安装步骤: 一.安装git 一般linux系统的git版本 ...
- Execl 使用技巧
1. =COUNTIF(C:C;"*OS7*") 某一列中包含OS7的数量总数138