版本6和7间的区别
于2006-04-21 20:52:55修订的的版本6
大小: 2523
编辑: czk
备注:
于2008-02-23 15:36:59修订的的版本7
大小: 2523
编辑: localhost
备注: converted to 1.6 markup
未发现区别!

Sorting the Photos

http://acm.zju.edu.cn/show_problem.php?pid=1171

Time limit: 1 Seconds

Memory limit: 32768K

Imagine you have a pile of 1 <= N <= 10^5 photos. Some of them are faced upwards and the others faced downwards. Your goal is to sort them so all the photos are faced the same direction. The only operation you are allowed to do is to take any amount of the photos from the top into the separate pile, turn that pile upside-down as the whole and put it back over the rest of original pile.

Write the program that calculates the minimum number of such operations needed to complete the sorting goal.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

1. Input

First line of the input contains the only number. Then, characters going possibly separated by newline and/or space characters "D" - faced down or "U" - faced up.

2. Output

Single integer number - minimal number of flip operations required to sort the photos pile.

3. Sample Input

1

5
UU D
UU

4. Sample Output

2


   1 /*Time Limit Exceeded
   2 Written by czk*/
   3 #include <iostream>
   4 using namespace std;
   5 
   6 int main()
   7 {
   8   int n;
   9   cin >> n;
  10   for(int i = 0; i < n; i++) {
  11     int m;
  12     cin >> m;
  13     char state = 'N';
  14     int flip = 0;
  15     for(int j = 0; j < m; j++) {
  16       char c;
  17       cin >> c;
  18       if(state == 'N')
  19         state = c;
  20       else if(state != c) {
  21         state = c;
  22         flip++;
  23       }
  24     }
  25     cout << flip << '\n';
  26     if( i != n-1)
  27       cout << '\n';
  28   }
  29 }

   1 /*Accepted Version
   2 Written by czk*/
   3 #include <stdio.h>
   4 #include <ctype.h>
   5 
   6 int main()
   7 {
   8   int n;
   9   scanf("%d", &n);
  10   for(int i = 0; i < n; i++) {
  11     int m;
  12     scanf("%d", &m);
  13     char state = 'N';
  14     int flip = 0;
  15     for(int j = 0; j < m; j++) {
  16       char c;
  17       while(isspace(c=getchar()));
  18       if(state == 'N')
  19         state = c;
  20       else if(state != c) {
  21         state = c;
  22         flip++;
  23       }
  24     }
  25     printf("%d\n", flip);
  26     if( i != n-1)
  27       putchar('\n');
  28   }
  29 }

zju1171 (2008-02-23 15:36:59由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.