zoukankan      html  css  js  c++  java
  • Codeforces Round #390 (Div. 2) A. Lesha and array splitting

    http://codeforces.com/contest/754/problem/A

    题意:

    给出一串序列,现在要把这串序列分成多个序列,使得每一个序列的sum都不为0。

    思路:

    先统计一下不为0的数,只要有一个不为0的数,那么就能分割。

    如果一个数不为0,则让它单独成为一组,如果它后面有0,则把它后面的连续的0也归到这一组中。

    特别要注意一下的是,序列一开始就是0的情况。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<sstream>
     6 #include<vector>
     7 #include<stack>
     8 #include<queue>
     9 #include<cmath>
    10 #include<map>
    11 using namespace std;
    12 typedef long long ll;
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const int maxn=400+5;
    16 
    17 int n;
    18 int a[maxn];
    19 
    20 struct node
    21 {
    22     int l, r;
    23 }ans[maxn];
    24 
    25 int main()
    26 {
    27     //freopen("in.txt","r",stdin);
    28     while(~scanf("%d",&n))
    29     {
    30         int cnt=0;
    31         for(int i=1;i<=n;i++)
    32             scanf("%d",&a[i]);
    33 
    34          for(int i=1;i<=n;i++)
    35              if(a[i]!=0)  cnt++;
    36 
    37         if(cnt==0)   {puts("NO");continue;}
    38 
    39         int i=1;
    40         cnt=0;
    41         while(i<=n)
    42         {
    43             ans[cnt].l=i;
    44             while(a[i]==0)  i++;
    45             while(i<n && a[i+1]==0)   i++;
    46             ans[cnt].r=i;
    47             i++;
    48             cnt++;
    49         }
    50 
    51         puts("YES");
    52         printf("%d
    ",cnt);
    53         for(int i=0;i<cnt;i++)
    54         {
    55             printf("%d %d
    ", ans[i].l, ans[i].r);
    56         }
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    monads-are-elephants(转)
    程序语言简史(转)
    语法的省略不能造成编译器的歧义
    scala getter and setter
    隐式类型转换
    java 调用 scala
    列表的操作
    Scala HandBook
    Scala 高级编程练习
    Net 2.0 C# 专用的只读类Tuple
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/7079015.html
Copyright © 2011-2022 走看看