题意:
  给你一个数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)的更多相关文章

  1. CodeForces 474B(标记用法)

    CodeForces 474B Time Limit:1000MS Memory Limit:262144KB   64bit IO Format:%I64d & %I64u Descript ...

  2. Codeforces 474B Worms 二分法(水

    主题链接:http://codeforces.com/contest/474/problem/B #include <iostream> #include <cmath> #i ...

  3. Codeforces 659B Qualifying Contest【模拟,读题】

    写这道题题解的目的就是纪念一下半个小时才读懂题...英文一多读一读就溜号... 读题时还时要静下心来... 题目链接: http://codeforces.com/contest/659/proble ...

  4. codeforces E. The Contest(最长上升子序列)

    题目链接:https://codeforces.com/contest/1257/problem/E 题意:给三个序列k1,k2,k3,每个序列有一堆数,k1是前缀,k3是后缀,k2是中间,现可以从任 ...

  5. codeforces 659B Qualifying Contest

    题目链接:http://codeforces.com/problemset/problem/659/B 题意: n个人,m个区.给出n个人的姓名(保证不相同),属于的区域,所得分数.从每个区域中选出成 ...

  6. 【37.74%】【codeforces 725D】Contest Balloons

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CodeForces 546B C(Contest #1)

    Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...

  8. Codeforces April Fools Contest 2017

    都是神题,我一题都不会,全程听学长题解打代码,我代码巨丑就不贴了 题解见巨神博客 假装自己没有做过这套

  9. CodeForces 474B Worms (水题,二分)

    题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...

随机推荐

  1. 相机位姿估计1_1:OpenCV:solvePnP二次封装与性能测试

    关键词:OpenCV::solvePnP 文章类型:方法封装.测试 @Author:VShawn(singlex@foxmail.com) @Date:2016-11-27 @Lab: CvLab20 ...

  2. select元素中设置padding效果

    更新2016-9-17:这个bug是在mac os上的发生,在window系统中不存在这个问题! 给select元素增加:padding-left:20px;但是未生效,如下图所示: 在select上 ...

  3. 工具网站gallery

    jQuery各个版本齐全的api 在线编辑器codepen

  4. 慕课网__HTML5 存储

    application cache 只能更新全部,不能更新单独的文件 在更新后,要重新打开浏览器,缓存才会生效, 不能实时生效

  5. 后台启动weblogic成功后,在web浏览器上无法访问

    后台启动weblogic成功后,在web浏览器上无法访问,可尝试重启服务器.

  6. 转:OSGI 实战 Equinox

    http://download.csdn.net/detail/ifmliuzhen/3231590

  7. .net(C#)在vs2010版本下的MVC如何配置才能切换静态页面(html)

    由于vs2010用的人比较多,虽然建mvc项目vs2010可能还不成熟,但鉴于每个人的成长有限,每个地方的资源有限,最主要的是为了解决问题,所以先不管那么多了. 用vs2010为公司网站建站,要求js ...

  8. js流程控制题——如何实现一个LazyMan

    先说一下想要的效果: lazyMan('zz').eat('lunch').sleep('3').eat('dinner')输出: Hi!This is zz! Eat lunch~ //有3s间隔等 ...

  9. mysql 基础篇5(mysql语法---数据)

    6 增删改数据 -- ********一.增删改数据********* --- -- 1.1 增加数据 -- 插入所有字段.一定依次按顺序插入 INSERT INTO student VALUES(1 ...

  10. text/plain && text/html

    text/plain和text/html都是Content-Type; text/plain : 页面以文本形式输出 text/html:  页面以html格式输出