ZOJ Problem Set - 1171 Sorting the Photos

本文介绍了一个算法问题,旨在通过最少次数的操作使一堆照片全部朝向同一方向。每张照片初始状态可能朝上或朝下,允许的操作是将部分或全部照片翻转。文章提供了一个示例输入输出,并附带了实现这一算法的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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代码:

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int cas;
	cin>>cas;
	while(cas--)
	{
		int n;
		cin>>n;
		char *pt=new char[n];
		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;
	} 
} 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值