ZOJ Problem Set - 1171 Sorting the Photos
2013-07-09 16:36
288 查看
ZOJ Problem Set - 1171
Sorting the Photos
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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.
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.
Output
Single integer number - minimal number of flip operations required to sort the photos pile.
Sample Input
1
5
UU D
UU
Sample Output
2
AC代码:
Sorting the Photos
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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.
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.
Output
Single integer number - minimal number of flip operations required to sort the photos pile.
Sample Input
1
5
UU D
UU
Sample Output
2
AC代码:
#include<iostream> #include<stdio.h> using namespace std; int main() { int cas; cin>>cas; while(cas--) { int n; cin>>n; char *pt=new char ; for(int i=0;i<n;i++)cin>>pt[i]; char tag=pt[0]; int count=0; for(int i=1;i<n;i++) { if(tag!=pt[i]) { tag=pt[i]; count++; } } cout<<count<<endl; if(cas!=0)cout<<endl; } }
相关文章推荐
- ZOJ Problem Set - 1171 Sorting the Photos
- ZOJ Problem Set–1951 Goldbach's Conjecture
- ZOJ Problem Set - 1003Crashing Balloon
- ZOJ Problem Set–1860 Dog & Gopher
- ZOJ Problem Set - 1045 & ZOJ Problem Set - 1048
- ZOJ Problem Set - 1512 (Water Treatment Plants)
- ZOJ Problem Set - 1002 Fire net
- ZOJ Problem Set - 3956 Course Selection System
- ZOJ Problem Set - 1067 Color Me Less
- ZOJ Problem Set - 1151 Word Reversal
- ZOJ Problem Set - 1004-Anagrams by Stack
- ZOJ Problem Set - 1730 Crazy Tea Party
- ZOJ Problem Set - 1003
- ZOJ Problem Set - 1365 Mileage Bank
- ZOJ Problem Set - 2724 Windows Message Queue
- ZOJ Problem Set - 1056
- ZOJ Problem Set - 1005 Jugs
- ZOJ Problem Set - 1007
- ZOJ Problem Set - 2388 Beat the Spread!
- ZOJ Problem Set - 3787