题意:
  给你一个数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. jQuery事件绑定.on()简要概述及应用

    原文地址:http://www.jquerycn.cn/a_5346     前几天看到事件委托的时候,关于live()方法讲的不是很详细,就去搜了一下关于live()和delegate()的,最后看 ...

  2. rxjava源码分析

    RXjava响应式编程 此文作者大暴雨原创,转载请注明出处. 如果线程的知识不是很丰富,请先查看     rxjava源码中的线程知识  一文 rxjava总结就是:异步实现主要是通过扩展观察者模式 ...

  3. Grunt_1从安装开始创建一个基本的Grunt

    Grunt的介绍:http://www.gruntjs.net/getting-started 文件架构:https://github.com/zhangsai521314/Grunt 1:安装Git ...

  4. angularJS 学习之路

    AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 AngularJS 应用程序.也就是angularjs作用的入口  作用在什么标签或者整个body ...

  5. 萝卜招聘网 http://www.it9s.com 可以发布免费下载简历求职 ,免费!免费!全部免费!找工作看过来 免费下载简历 !

    萝卜招聘网  http://www.it9s.com  可以发布免费下载简历求职 ,免费!免费!全部免费!找工作看过来 免费下载简历 !萝卜招聘网  http://www.it9s.com  可以发布 ...

  6. IRLS(迭代加权最小二乘)

    IRLS用于解决这种目标函数的优化问题(实际上是用2范数来近似替代p范数,特殊的如1范数). 可将其等价变形为加权的线性最小二乘问题: 其中W(t)可看成对角矩阵,每步的w可用下面的序列代替 如果 p ...

  7. 1. AE二次开发——地图的基本操作(加载地图文档,加载shape,加载mdb,地图的保存,缩放,漫游)

    1. 加载数据Icommand方法 ICommand Butdata = new ControlsAddDataCommandClass(); Butdata.OnCreate(axMapContro ...

  8. 常见http status code

    常见http status code 常见的状态码: HTTP: Status200– 服务器成功返回网页 HTTP: Status404– 请求的网页不存在 HTTP: Status503– 服务不 ...

  9. JavaScipt 数据交互

    标准的w3c直接提供了XMLHttpRequest方法,我们主要站在设计的角度来理解,如何设计出低耦合高内聚的代码jquery对Ajax的处理主要体现在对浏览器兼容,数据的处理过滤以及各种事件的封装上 ...

  10. 《精通C#》第十六章-动态类型和动态语言运行时-第一节至第四节

    在.Net4.0中引入了一个关键字dynamic,这是一个动态类型关键字.Net中还有一个关键字是var,这是一个隐式类型,可以定义本地变量,此时var所代表的实际的数据类型有编译器在初次分配时决定, ...