题意:
  给你一个数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. 读《编写可维护的JavaScript》第七章总结

      第七章 事件处理 7.1 典型用法 作者首先给了个我们一个处理事件的方法.看起来也没啥俩样,不过后来给出的优化方法很值得学习: // 不好的写法 function handleClick(even ...

  2. ucos中的三种临界区管理机制

    熟悉ucos,或者读过Jean.J.Labrosse写过的ucos书籍的人,一定会知道ucos中著名的临界区管理宏:OS_ENTER_CRITICAL()和OS_EXIT_CRITICAL(). 同样 ...

  3. avalon复杂绑定

    样式操作:ms-css-样式名=“样式值”,ms-class ms-css-width="prop"(自动补px) ms-css-height="{{prop}}%&qu ...

  4. dispatch a action with a timeout

    程序入口文件添加依赖: import { createStore, applyMiddleware } from 'redux' import thunk from 'redux-thunk' // ...

  5. 人工神经网络ANNs

    参考: 1. Stanford前向传播神经网络Wiki 2. Stanford后向传播Wiki 3. 神经网络CSDN blog 4. 感知器 5. 线性规划 6. Logistic回归模型 内容: ...

  6. js 睡觉

    js睡觉 function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMilli ...

  7. ubuntu12.04 安装 QQ

    友情提示:QQ对于第三方平台封杀已经到了丧心病狂的程度,所以不要妄想在linux的QQ能像win系统下的效果,只能祈求能用就好 1.下载QQ安装包 http://pan.baidu.com/s/1ge ...

  8. Linux查找

    如果你想在当前目录下 查找"hello,world!"字符串,可以这样: grep -rn "hello,world!" * * : 表示当前目录所有文件,也可 ...

  9. struts 标签的使用<转>

    Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的 ...

  10. a.redhat系统如何卸载默认jdk

    Redhat系统安装之后,会默认有openjdk在安装(下图已经是卸载掉了) 这个openjdk下面的需要进行卸载,你可以通过命令进行卸载,例如首先查看JDK,然后默认直接通过rpm命令进行卸载L 卸 ...