E-Eating Together(POJ 3670)
Eating Together
Description The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. The trouble starts when they line up at the barn to enter the feeding area. Each cow i carries with her a small card upon which is engraved Di (1 ≤ Di ≤ 3) indicating her dining group membership. The entire set of N (1 ≤ N ≤ 30,000) cows has lined up for dinner but it's easy for anyone to see that they are not grouped by their dinner-partner cards. FJ's job is not so difficult. He just walks down the line of cows changing their dinner partner assignment by marking out the old number and writing in a new one. By doing so, he creates groups of cows like 111222333 or 333222111 where the cows' dining groups are sorted in either ascending or descending order by their dinner cards. FJ is just as lazy as the next fellow. He's curious: what is the absolute mminimum number of cards he must change to create a proper grouping of dining partners? He must only change card numbers and must not rearrange the cows standing in line. Input * Line 1: A single integer: N Output * Line 1: A single integer representing the minimum number of changes that must be made so that the final sequence of cows is sorted in either ascending or descending order Sample Input 5 Sample Output 1 Source |
跟F题差不多,就是变成了三个数,变成了升序或降序。。。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 30005 int d[MAXN][] = {};
int cow[MAXN];
int n;
int get_min()
{
d[][] = d[][] = d[][] = ;
d[][cow[]] = ;
repu(i, , n + )
if(cow[i] == ) {
d[i][] = d[i - ][];
d[i][] = min(d[i - ][], d[i - ][]) + ;
d[i][] = min(d[i - ][], min(d[i - ][], d[i - ][])) + ;
}
else if(cow[i] == ) {
d[i][] = d[i - ][] + ;
d[i][] = min(d[i - ][], d[i - ][]);
d[i][] = min(d[i - ][], min(d[i - ][], d[i - ][])) + ;
}
else {
d[i][] = d[i - ][] + ;
d[i][] = min(d[i - ][], d[i - ][]) + ;
d[i][] = min(d[i - ][], min(d[i - ][], d[i - ][]));
}
return min(d[n][], min(d[n][], d[n][]));
}
int main()
{
int minn;
scanf("%d", &n);
repu(i, , n + ) scanf("%d", &cow[i]);
minn = get_min();
repu(i, , n / + ) swap(cow[i], cow[n - i + ]);
minn = min(minn, get_min());
printf("%d\n", minn);
return ;
}
E-Eating Together(POJ 3670)的更多相关文章
- POJ 3670 Eating Together (DP,LIS)
题意:给定 n 个数,让你修改最少的数,使得它变成一个不下降或者不上升序列. 析:这个就是一个LIS,但是当时并没有看出来...只要求出最长LIS的长度,用总数减去就是答案. 代码如下: #inclu ...
- POJ 3670 Eating Together 二分解法O(nlgn)和O(n)算法
本题就是一题LIS(最长递增子序列)的问题.本题要求求最长递增子序列和最长递减子序列. dp的解法是O(n*n),这个应该大家都知道.只是本题应该超时了. 由于有O(nlgn)的解法. 可是因为本题的 ...
- POJ 3670 Eating Together(LIS)
Description The cows are so very silly about their dinner partners. They have organized themselves i ...
- POJ 3670 , 3671 LIS
题意:两题意思差不多,都是给你一个序列,然后求最少需要改变多少个数字,使得成为一个最长不升,或者最长不降子序列. 当然3671是只能升序,所以更简单一点. 然后就没有什么了,用二分的方法求LIS即可. ...
- poj 3670(LIS)
// File Name: 3670.cpp // Author: Missa_Chen // Created Time: 2013年07月08日 星期一 21时15分34秒 #include < ...
- POJ 3670 DP LIS?
权值为1~3 好了 此题是水题-- i表示到了第i个数,j表示结尾的数是j f[i][j]=min(f[i][j],f[i-1][k]+(a[i]!=j)) 1<=k<=j 最长上升的. ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
随机推荐
- NoSQL数据库:数据的一致性
NoSQL数据库:数据的一致性 读取一致性 强一致性 在任何时间访问集群中任一结点,得到的数据结果一致: 用户一致性 对同一用户,访问集群期间得到的数据一致: 解决用户一致性:使用粘性会话,将会话绑定 ...
- STORM_0006_第二个storm_topology:WordCountTopology的代码与运行
我先试试这个Open Live Writer能不能用. 再在ScribeFire中修改一下已经发布的文章试试看. 这两个写博客的地方都没有原始的编辑器方便,可以插入代码,选择文章的分类.所以以后还有这 ...
- CSS笔记(十四)CSS3之动画
参考:http://www.w3school.com.cn/css3/css3_animation.asp 通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 Ja ...
- 详解公用表表达式(CTE)
简介 对于SELECT查询语句来说,通常情况下,为了使T-SQL代码更加简洁和可读,在一个查询中引用另外的结果集都是通过视图而不是子查询来进行分解的.但是,视图是作为系统对象存在数据库中,那对于结果集 ...
- iOS - UIApplication
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIApplication : UIResponder @available(iOS 2.0, *) public ...
- 用python实现把数字人民币金额转换成大写的脚本程序
# -*- coding: utf-8 -*- def Num2MoneyFormat( change_number ): """ .转换数字为大写货币格式( forma ...
- Java中List的使用
package ch8; import java.util.*; /** * Created by Jiqing on 2016/11/27. */ public class ListTest { p ...
- [css] haslayout
原文:http://blog.sina.com.cn/s/blog_51048da701018o29.html IE的表现与其他浏览器不同的原因之一就是,显示引擎使用一个称为布局(layout)的内部 ...
- C++ typedef 四个用途
第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // 这多数不符合我们的意图,它只声明了一个指向字符变量的指针, ...
- 阿里云+wordpress搭建个人博客网站
[正文] 在阿里云上搭建使用个人博客主要分为以下几个步骤: 1.购买阿里云ECS主机 2.购买域名 3.申请备案 4.环境配置 5.安装wordpress 6.域名解析 声明一下,本人对服务器端的知识 ...