CodeForces 474B Worms (水题,二分)

题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里。

析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound.

代码如下:

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
int a[maxn];

int main(){
    int n, m;
    cin >> n;
    for(int i = 1; i <= n; ++i)  scanf("%d", &a[i]);
    for(int i = 2; i <= n; ++i) a[i] += a[i-1];
    cin >> m;
    int x;
    while(m--){
        scanf("%d", &x);
        int t = lower_bound(a, a+n, x) - a;
        printf("%d\n", t);
    }
    return 0;
}
时间: 2025-01-13 21:27:31

CodeForces 474B Worms (水题,二分)的相关文章

Codeforces 474B Worms (二分查找)

题目链接:Codeforces 474B Worms 题意:给出一串数字比如2 7 3 4 9. 表示第一堆编号是[1,2].第二堆编号是[3,9].第三堆编号是[10,12].第四堆编号是[13,16].第五堆编号是[17,25]. 预处理出每堆的上界二分查找答案. AC代码: </pre><pre name="code" class="cpp">#include<stdio.h> #include<map> #in

CodeForces 20B Equation 水题

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10000000 #define l

CodeForces 705A Hulk (水题)

题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring

Codeforces 474B. Worms

It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are label

codeforces 377A. Puzzles 水题

A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/337/A Description The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to p

zoj 3726 水题+二分

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

hdu - 6276,2018CCPC湖南全国邀请赛A题,水题,二分

题意: 求H的最大值,  H是指存在H篇论文,这H篇被引用的次数都大于等于H次. 思路:题意得,  最多只有N遍论文,所以H的最大值为N, 常识得知H的最小值为0. 所以H的答案在[0,N]之间,二分搜一下,如果满足就提高下限,不满足则降低上限. 嗯就这样!!!! AC code: #include<bits/stdc++.h> using namespace std; int n; vector<int> cc(200005); int main() { bool check(i

CodeForces 709A Juicer (水题, 模拟)

题意:给定 n 个桔子的大小,一个杯子的容积,一个最大限度,挨着挤桔子汁,如果大小大于限度,扔掉,如果不杯子满了倒掉,问你要倒掉多少杯. 析:直接按要求模拟就好,满了就清空杯子. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath&g

CodeForces 474A Keyboard (水题)

题意:给定一个键盘,然后一行字母,和一个字符,代表把那一行字母在键盘上左移还是右移一位. 析:没什么好说的,直接暴力就好. 代码如下: #include<bits/stdc++.h> using namespace std; typedef long long LL; char s1[] = "qwertyuiop"; char s2[] = "asdfghjkl;"; char s3[] = "zxcvbnm,./"; char s