码迷,mamicode.com
首页 > 其他好文 > 详细

Beautiful Walls

时间:2016-03-13 00:40:30      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

  • [1553] Beautiful Walls

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • To simplify the building process, XadillaX built some template on the ground. The template is a very big wall and the height of each unit may be different.

    8Mao and Hungar have to choose any part of this wall as their own wall.

    技术分享

    The part(i, j) means the wall between unit(i) and unit(j) with their heights.
    What Hungar thinks a beautiful wall is that the height of each unit is unique.
    Now give you a wall-template, you should tell Hungar that how many ways he can choose to copy his own wall?
  • 输入
  • This problem contains several cases, ends with EOF.
    The first line of each case is one integer N (0 < N ≤ 100000) which indicates the side length (number of units) of the wall-template.
    The second line contains N integers which indicate the height of each unit. (0 < height <= 100000)
  • 输出
  • For each case, you should output the number of ways that Hungar can choose.
  • 样例输入
  • 5
    3 4 5 5 2
    3
    1 2 3
    
  • 样例输出
  • 9
    6
    
  • 提示
  • 来源
  • cjl
  • 思路:尺取法,组合数学;
  • 用尺取法取区间,在这个区间内所有的数都不同,那么可行方案数为Cn2,两个区间还会相交,所以还得减去重复算的,比如1 2 3 4 5 3 2 1
  • 前一个区间为[1,5],后一个为[4,1];所以[4,5]相交,所以去重.那么关键是记录相交的地方,我们可以这样,记录上个区间的结束,然后根据尺取法可知道当前
  • 区间的开头,这样就可以算出了。复杂O(n);
  •  1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<string.h>
     5 #include<stdlib.h>
     6 #include<math.h>
     7 using namespace std;
     8 typedef long long LL;
     9 int uu[1000005];
    10 int flag[1000005];
    11 int yy[1000005];
    12 int main(void)
    13 {   int i,j,k;
    14     while(scanf("%d",&k)!=EOF)
    15     {   memset(flag,0,sizeof(flag));
    16         memset(yy,0,sizeof(yy));
    17         for(i=0;i<k;i++)
    18         {
    19             scanf("%d",&uu[i]);
    20         }
    21         LL ans=1;long long sum=0;int vv=0;
    22         int l=0;int r=0;flag[uu[0]]=1;int kp=0;
    23         while(l<=r+1&&r<k-1)
    24          {
    25              while(r<k-1)
    26              {
    27                  r++;
    28                  if(flag[uu[r]]==1)
    29                  {LL yyk=0;
    30                      if(ans>0&&!yy[r-1])
    31                      {
    32                          yy[r-1]=1;
    33                          if(kp>l)
    34                          {
    35                               yyk=kp-l+1;
    36                          }
    37                          sum+=ans*(ans-1)/2;if(yyk>0)sum-=yyk*(yyk-1)/2;
    38                          kp=r-1;
    39                      }ans++;flag[uu[r]]++;break;
    40                  }
    41                  else if(!flag[uu[r]])
    42                  {
    43                      ans++;flag[uu[r]]++;
    44                  }
    45              }
    46              if(r==k-1&&flag[uu[r]]==1)
    47                 break;
    48              else {flag[uu[r]]--;flag[uu[l]]--;ans-=2;r--;l++;}
    49          }
    50          if(ans>0){sum+=ans*(ans-1)/2;sum+=(LL)k;LL yyk=kp-l+1;if(yyk>0){sum-=yyk*(yyk-1)/2;}}
    51          printf("%I64d\n",sum);
    52         }
    53     return 0;
    54 }

     

Beautiful Walls

标签:

原文地址:http://www.cnblogs.com/zzuli2sjy/p/5270962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!