题意:
  给你一个数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. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. js和jquery获取当前对象的子元素

    开发中经常遇到需要获取ul下的il对象,个人总结了js和jquery的方法. HTML片断: <ul class="box"> <li>子元素1</l ...

  3. SQL基础--索引

    索引的概念: 在数据库中索引是用于提升数据库查询操作性能的一种手段,但在频繁更新的表上,索引反而会降低性能. 常用的索引结构: B*树索引 位图索引 B树索引: B书索引是最基本的索引结构,Oracl ...

  4. Apache 403 error, (13)Permission denied: access to / denied问题

    Apache 配置Alias 后,无法访问 CentOS系统 检查了一圈httpd.conf和目录权限,均没有发现问题. 最后,看了这篇文章,发现是因为系统启动了SELINUX导致的. http:// ...

  5. k.APR通道特殊配置

    APR/native specific configuration The following attributes are specific to the APR/native connector. ...

  6. Find命令的用法

    1. 在某目录下查找文件 [root@localhost ~]# find /usr/local -name pci.ids* /usr/local/share/pci.ids.gz [root@lo ...

  7. linux下使用yum安装Apache+php+Mysql+phpMyAdmin

    适用redhat于32位及64位,前提架设好本地源.在这里不再赘述. 1 安装Apache+php+Mysql a.安装Apahce, PHP, Mysql, 以及php连接mysql库组件 yum ...

  8. 了解真实的『REM』手机屏幕适配

    rem 作为一个低调的长度单位,由于手机端网页的兴起,在屏幕适配中得到重用.使用 rem 前端开发者可以很方便的在各种屏幕尺寸下,通过等比缩放的方式达到设计图要求的效果. rem 的官方定义『The ...

  9. biweb后台添加上传下载功能

    1.数据库对应表添加字段 file       varchar(100) 2.对应的后台 模块名/admin/addinfo.php里添加如下代码: //文件上传 if ((($_FILES[&quo ...

  10. ionic 初入门

    ionic ionic 是webapp开发的一个框架 安装 npm install -g cordova ionic ;  我这两个分开装,因为ionic模块拖不下来,所以只好等待时机.这时候科学上网 ...