Codeforces Round #171 (Div. 2)

A题  看懂题意之后   简单模拟   

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int f[100005];
 8 
 9 int main( )
10 {
11     int x,y,w,t,x1,y1,count;
12     scanf("%d%d",&x1,&y1);
13     x = y = w = 0,t = 1,count = 0;
14     while( true )
15     {
16         if( t == 1 )
17         {
18             w++;
19             x += w;count++;
20             if( y == y1 && x1 <= x && x1 >= x-w )
21             break;
22             t = 2;
23         }
24         else
25         if( t == 2 )
26         {
27             y += w;  count++;
28             if( x == x1 && y1 <= y && y1 >= y-w )
29             break;
30             t = 3;
31         }
32         else
33         if( t == 3 )
34         {
35             w++;
36             x -= w;count++;
37             if( y == y1 && x1 >= x && x1 <= x+w )
38             break;
39             t = 4;
40         }
41         else
42         if( t == 4 )
43         {
44             y -= w;count++;
45             if( x == x1 && y1 >= y && y1 <= y+w )
46             break;
47             t = 1;
48         }
49     }
50     printf("%d\n",count-1);
51     return 0;
52 }

B 题   简单模拟  

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int f[100005];
 8 
 9 int main( )
10 {
11     int i,N,T,l,r,val;
12     scanf("%d%d",&N,&T);
13     for( i = 1; i <= N; i++ )
14         scanf("%d",&f[i]);
15     int ans = 0; l = 0; r = 1;val = 0;
16     while( r <= N )
17     {
18         val += f[r];
19         while( val  > T  )
20             val -= f[++l];
21         if( ans < r-l )
22             ans = r-l;
23         r++;
24     }
25     cout<<ans<<endl;
26     return 0;
27 }

C 题  就是   求一个峰值;多做几个标记 就行了

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

int arr[100005];
int res[100005];

int main( )
{
   int i,u,v,N,M,lt;
   while( scanf("%d%d",&N,&M) != EOF )
   {
      for( i = 1; i <= N; i++ )
           scanf("%d",&arr[i]);arr[0] = -1;
      bool fell = false;  lt = 1;
      for( i = 1; i <= N; i++ )
      {
          if( arr[i] == arr[i-1] )  res[i] = lt;
          else
          if( arr[i] >  arr[i-1]  )
          {
              if( !fell ) res[i] = lt;
              else
              {
                   lt = i - 1;
                   bool fall = false;
                   while( arr[i - 1] == arr[lt] )
                   {
                        fall = true;
                         --lt;
                   }
                   if( fall ) ++lt;
                   res[i] = lt;
                   fell = false;
              }
           }
           else  res[i] = lt,fell = true;
      }
      for( i = 1; i <= M; i++ )
      {
          scanf("%d%d",&u,&v);
          if( res[v] <= u )printf("Yes\n");
          else             printf("No\n");
      }
   }
   return 0;
}

D 题  状态压缩   还没有想出来

E 题  考虑两种方式去获取最优值   第一种就是  直接  ( 1<<i ) 位  还有一种 就是   (1<<k)  - ( 1<<j ) 在这种情况下会出现这            种情况  1111110000   假如会得到这么一个结果  而想要的是  1110110000 因此 只需要再增加一步 (1<<i)便可以实现  所以 需要注意   1101  和  1011 的情况   而为什么不是  101 这种情况呢!这种情况可能只要两步就实现了   但看看第二种方法,,他需要三步;所以   01010  要拿开考虑  ~~;

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int main( )
 8 {
 9      char str[1000006];
10      int i,ans,t;
11      while( scanf("%s",&str) != EOF )
12      {
13          int len = strlen( str );
14          ans = 0;
15          if( len >= 4 )
16          {
17              for( i = 1; i < len; i++ )
18              {
19                  if( str[i] == '0' && str[i-1] == '1' && str[i+1] == '1' )
20                  {
21                      if( i - 2 >= 0 && str[i-2] == '1' ) 
22                      {
23                          ans++;
24                          str[i] = '1';
25                      }
26                      else if( i + 2 < len && str[i+2] == '1' )
27                      {
28                          ans++;
29                          str[i] = '1';
30                      }
31                  }
32              }
33          }
34          i = 0; str[len] = '0'; t = 0;
35          while( i <= len )
36          {
37              bool fell = false;
38              if( str[i] == '1' ) t++;
39              else
40              {
41                  fell = true;
42                  if( t == 1 ) ans += 1;
43                  else         ans += 2;
44                  t = 0;
45                  while( i <= len && str[i] == '0' )
46                  i++;
47              }
48              if( !fell ) i++;
49          }
50          printf("%d\n",ans);
51      }
52      return 0;
53 }

 

posted on 2013-03-13 22:37  浪舟  阅读(160)  评论(0编辑  收藏  举报

导航