BZOJ 1640 [Usaco2007 Nov]Best Cow Line 队列变换:贪心【字典序最小】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1640
题意:
给你一个长度为n的字符串。
你可以将原串的首字母或尾字母移动到新串的末尾。
让你输出字典序最小的新串。
题解:
贪心。
三种情况:
(1)c[head] < c[tail]
输出c[head],head++。
(2)c[head] > c[tail]
输出c[tail],tail--。
(3)c[head] == c[tail]
选head和tail并不等价。
比如原串为"CBAC",明显选tail更优,因为选tail能够更快地选到'A',让字典序更小。
所以需要不断地"head++,tail--",直到c[head] != c[tail]的时候停止,然后选小的。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 2005 using namespace std; int n;
int head,tail;
char c[MAX_N]; int main()
{
cin>>n;
head=;
tail=n-;
for(int i=;i<n;i++)
{
cin>>c[i];
}
for(int i=;i<n;i++)
{
if(i!= && i%==) cout<<endl;
if(c[head]<c[tail]) cout<<c[head++];
else if(c[head]>c[tail]) cout<<c[tail--];
else
{
int lef=head;
int rig=tail;
int res=;
while(lef<=rig)
{
if(c[lef]<c[rig])
{
res=-;
break;
}
if(c[lef]>c[rig])
{
res=;
break;
}
lef++;
rig--;
}
if(res== || res==-) cout<<c[head++];
else cout<<c[tail--];
}
}
}
BZOJ 1640 [Usaco2007 Nov]Best Cow Line 队列变换:贪心【字典序最小】的更多相关文章
- BZOJ 1640: [Usaco2007 Nov]Best Cow Line 队列变换
Description FJ打算带着他可爱的N (1 ≤ N ≤ 2,000)头奶牛去参加"年度最佳老农"的比赛.在比赛中,每个农夫把他的奶牛排成一列,然后准备经过评委检验. 比赛 ...
- 1640: [Usaco2007 Nov]Best Cow Line 队列变换
1640: [Usaco2007 Nov]Best Cow Line 队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 543 Solved: 2 ...
- bzoj:1692 [Usaco2007 Dec]队列变换&&1640 [Usaco2007 Nov]Best Cow Line 队列变换
Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席 ...
- BZOJ1640: [Usaco2007 Nov]Best Cow Line 队列变换
1640: [Usaco2007 Nov]Best Cow Line 队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 493 Solved: 2 ...
- bzoj1640[Usaco2007 Nov]Best Cow Line 队列变换*&&bzoj1692[Usaco2007 Dec]队列变换*
bzoj1640[Usaco2007 Nov]Best Cow Line 队列变换 bzoj1692[Usaco2007 Dec]队列变换 题意: 有一个奶牛队列.每次可以在原来队列的首端或是尾端牵出 ...
- BZOJ 1707 [Usaco2007 Nov]tanning分配防晒霜(扫描线+贪心+优先队列)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1707 [题目大意] 每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值 防晒霜的作 ...
- BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )
直接floyd.. ---------------------------------------------------------------------------- #include<c ...
- BZOJ 1692: [Usaco2007 Dec]队列变换( 贪心 )
数据 n <= 30000 , 然后 O( n² ) 的贪心也过了..... USACO 数据是有多弱啊 = = ( ps : BZOJ 1640 和此题一模一样 , 双倍经验 ) ------ ...
- BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛
题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec Memory Limit: 64 MB Description FJ's N ( ...
随机推荐
- jsp中jquery用法一步刷新 验证用户名是否存在
<script type="text/javascript"> /* $(document).ready(function(){ var id="ha&quo ...
- Xshell 一款很养眼的配色方案推荐
Xshell 是个很好用的在 windows 下登陆 liunx 的终端原生支持中文,配合 Xftp 管理文件,同是免费软件可远比 Putty 好用多了面对枯燥的代码,我们需要一款很养眼的配色方案来保 ...
- VueJS锚定
锚定函数 指令定义函数提供了几个钩子函数(可选): bind: 只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个在绑定时执行一次的初始化动作. inserted: 被绑定元素插入父节 ...
- typedef,结构体,共用体,联合体
typedef的用途: 封装数据类型,方便移植 简化函数指针的定义 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/ ...
- Windows 命令集合
查看端口占用 查看8080端口使用情况: C:\>netstat -aon|findstr "8080" 结果:TCP 0.0.0.0:8080 0 ...
- Mongodb之备份恢复脚本
本分脚本: !/bin/bash #备份文件执行路径 which mongodump DUMP= #临时备份目录 OUT_DIR= #本分存放目录 TAR_DIR= #获取当前系统时间==> 2 ...
- Chrome自带恐龙小游戏的源码研究(二)
在上一篇<Chrome自带恐龙小游戏的源码研究(一)>中实现了地面的绘制和运动,这一篇主要研究云朵的绘制. 云朵的绘制通过Cloud构造函数完成.Cloud实现代码如下: Cloud.co ...
- 【Python基础】之for循环、数组字典
一. for循环实例 1.循环字符串 Python Shell: for i in "hello": print(i) h e l l o 2.循环数组Python Shell: ...
- Android 繪圖白板元件,有畫筆和板擦的功能 (转)
package com.example.drawboard; import java.util.ArrayList; import java.util.List; import android.con ...
- HDU 5338(ZZX and Permutations-用线段树贪心)
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...