zoj 1171 - Sorting the Photos

题目:n个叠放在一起的卡片,求使得他们同向的最小翻转次数,每次只能翻转最上面的k个。

分析:数学,贪心。每次翻转必然是不同情况(正,反)交接的地方(否则只会变得更糟);

记交接的地方的个数为m,那么翻转后交接的地方变为m-1或m;

(上面部分两端相同则为m-1,不同则为m)

因此一次翻转最多使得交接点减少1;所以每次翻转最上面的交接点即可。

说明:如果都变成向上或者向下就是dp了(⊙_⊙)。(2011-09-21 11:50)

#include<iostream>
#include<cstdlib>
using namespace std;

char photo[ 100001 ];
int  numbe[ 100001 ];

int main()
{
    int n,m;cin >> n;
    while ( n -- )
    {
        cin >> m;
        for ( int i = 0 ; i < m ; ++ i )
            cin >> photo[ i ];
        numbe[ m-1 ] = 0;
        for ( int i = m-2 ; i >= 0 ; -- i )
            if ( photo[ i ] == photo[ i+1 ] )
                numbe[ i ] = numbe[ i+1 ];
            else {
                numbe[ i ] = numbe[ i+1 ] + 1;
            }
        cout << numbe[ 0 ] << endl;
        if ( n ) cout << endl;
    }
    return 0;
}
时间: 2024-08-06 01:00:27

zoj 1171 - Sorting the Photos的相关文章

poj 1094 / zoj 1060 Sorting It All Out

Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26876   Accepted: 9271 Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from sm

zoj 1060 Sorting It All Out

这题题目的意思我跟hdu的确定比赛名次的要求搞混了,其实很容易. 确定比赛名次的题意是在不确定顺序的时候,不能确定的顺序按照字母升序排列 这边给出一些大小关系,而你的任务就是理清这些关系. 然后就有一个全序,偏序的概念. 具体的理论部分可以见:http://blog.csdn.net/dm_vincent/article/details/7714519 要注意的就是题目中的要求是 环和全序得出结论后,就不再进行后面数据的处理. 在不确定顺序的情况下,环是可以存在的. 1 #include <st

zoj 1060 Sorting It All Out(拓扑排序)

#include<bits/stdc++.h> using namespace std; vector< vector<char> >v; int n,m,use[30],cnt[30]; char ans[30],s[10]; int topsort(int x) { int i,j,t[30],f=1,r,c; memset(ans,0,sizeof(ans)); for(i=0; i<30; i++) t[i]=cnt[i]; c=0; r=0; while

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

HDU 1988 &amp; ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1988 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1990 Problem Description The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As

ZOJ 3612 Median (multiset)

Median Time Limit: 5 Seconds      Memory Limit: 65536 KB The median of m numbers is after sorting them in order, the middle one number of them if m is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at fi