• CodeForces 474B E(Contest #1)


    题意:
      给你一个数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,这样输入一个数很快就可以输出它所在的区间编号;
      因为区间中的值是有序的,所以还可以采用二分查找法.





    代码如下:

     1 //方法一:
     2 
     3 
     4 #include <iostream>
     5 #include <cstdio>
     6 #include <cstring>
     7 #include <fstream>
     8 #include <ctime>
     9 #include <cmath>
    10 #include <cstdlib>
    11 #include <algorithm>
    12 #include <set>
    13 #include <map>
    14 #include <list>
    15 #include <stack>
    16 #include <queue>
    17 #include <iterator>
    18 #include <vector>
    19 
    20 using namespace std;
    21 
    22 #define LL long long
    23 #define INF 0x3f3f3f3f
    24 #define MON 1000000007
    25 #define MAXN 10000010
    26 #define MAXM 1000010
    27 
    28 const int maxn = 1000005;
    29 int a[maxn];
    30 
    31 int main()
    32 {
    33     int n, m;
    34     while(scanf("%d", &n)==1)
    35     {
    36         memset(a, 0, sizeof(a));
    37         int i, j;
    38         int cnt = 1;
    39         int sum = 0;
    40         int pos = 0;
    41         for(i = 1; i <= n; i++ )
    42         {
    43             pos++;
    44             int x;
    45             scanf("%d", &x);
    46             sum += x;   //sum的最大值为1e6超过了n的最大值,所以数组a的长度应该为1e6,不然会RE
    47             for(j = cnt; j <= sum; j++ )
    48                 a[j] = pos;
    49             cnt = sum+1;
    50         }
    51         scanf("%d", &m);
    52         for(i = 0; i < m; i++ )
    53         {
    54             int y;
    55             scanf("%d", &y);
    56             printf("%d
    ", a[y]);
    57         }
    58     }
    59 
    60     return 0;
    61 }

     
     1 //方法二:
     2 
     3 16:52:262016-07-25
     4 #include <iostream>
     5 #include <cstdio>
     6 #include <cstring>
     7 #include <fstream>
     8 #include <ctime>
     9 #include <cmath>
    10 #include <cstdlib>
    11 #include <algorithm>
    12 #include <set>
    13 #include <map>
    14 #include <list>
    15 #include <stack>
    16 #include <queue>
    17 #include <iterator>
    18 #include <vector>
    19 
    20 using namespace std;
    21 
    22 #define LL long long
    23 #define INF 0x3f3f3f3f
    24 #define MON 1000000007
    25 #define MAXN 10000010
    26 #define MAXM 1000010
    27 
    28 const int maxn = 100005;
    29 int a[maxn], b[maxn];
    30 
    31 int lower_bound(int *A, int x, int y, int v)
    32 {
    33     int mid;
    34     while(x < y)
    35     {
    36         mid = (y-x)/2 + x;
    37         if(v <= A[mid])
    38             y = mid;
    39         else
    40             x = mid+1;
    41     }
    42 
    43     return x;
    44 }
    45 
    46 int main()
    47 {
    48     int n, m;
    49     while(scanf("%d", &n)==1)
    50     {
    51         memset(a, 0, sizeof(a));
    52         int i;
    53         int sum = 0;
    54         int c = 1;
    55         for(i = 0; i < n; i++ )
    56         {
    57             int x;
    58             scanf("%d", &x);
    59             sum += x;
    60             a[c++] = sum;
    61         }
    62         scanf("%d", &m);
    63         for(i = 0; i < m; i++ )
    64         {
    65             int y;
    66             scanf("%d", &y);
    67             int cnt = lower_bound(a, 0, c, y);
    68             printf("%d
    ", cnt);
    69         }
    70     }
    71 
    72     return 0;
    73 }
  • 相关阅读:
    转一篇:arp欺骗的原理和防御
    CF1209A Paint the Numbers
    字符串专题之KMP算法
    CF1209B Koala and Lights
    CF1217A Creating a Character
    CF1217B Zmei Gorynich
    各种模板
    开通博客园拉..
    汇编语言程序设计学习笔记(第一遍学习)第4节:汇编就像HTML一样简单
    我也要学C语言第二十章:结构体类型变量,结构体数组
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4659870.html
Copyright © 2020-2023  润新知