POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题。
1.给定长度为N的字符串S,要构造一个长度为N的字符串T。起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部。2.从S的尾部删除一个字符,加到T的尾部。求你任意采取这两个步骤后能得到的最小字符串T
2.直线上有N个点,点i的位置是xi,这N个点中选择若干个做上标记,对于每个点,在他们距离为R的区域内必须带有标记点,求在满足这个条件的情况下,所需要标记点的最少个数。
1.POJ 3617 Best Cow Line
http://poj.org/problem?id=3617
题目大意:给定长度为N的字符串S,要构造一个长度为N的字符串T。起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部。2.从S的尾部删除一个字符,加到T的尾部。求你任意采取这两个步骤后能得到的最小字符串T
思路:
很容易想到贪心算法,不断的取S的开头和末尾中较小的一个字符放到T的末尾。
很快写完,WA。
因为相等的情况你没有讨论!我们总希望下一个拿到的也是最小的,故要继续比较下一个。
改了然后恶心的PE,80个字母要一个换行。
#include<cstdio>
#include<cstring>
#include<iostream>
const int MAXN=2000+10;
char a[MAXN];
int main()
{
int n;
char temp;
while(~scanf("%d",&n))
{
getchar();
for(int i=0;i<n;i++)
scanf("%c%c",&a[i],&temp);
int cnt=0;
for(int i=0;i<n;)
{
if(a[i]<a[n-1])
printf("%c",a[i++]);
else if(a[i]>a[n-1])
{
printf("%c",a[n-1]);
n--;
}
else //a[i]==a[n-1]
{
int L=i+1,R=n-2;
while(L<R && a[L]==a[R])
{
L++;R--;
}
if(a[L]<a[R])
printf("%c",a[i++]);
else
{
printf("%c",a[n-1]);
n--;
}
}
if(++cnt==80)
{
printf("\n");
cnt=0;
}
}
printf("\n");
}
return 0;
}
2.POJ 3069 Saruman's Army
http://poj.org/problem?id=3069
题目大意:
直线上有N个点,点i的位置是xi,这N个点中选择若干个做上标记,对于每个点,在他们距离为R的区域内必须带有标记点,求在满足这个条件的情况下,所需要标记点的最少个数。
思路:
贪心,每次尽量选择向右边的。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1000+10;
int a[MAXN];
int main()
{
int n,r;
while(scanf("%d%d",&r,&n),n!=-1,r!=-1)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]); sort(a,a+n);
int ans=0,i=0;
while(i<n)
{
int x=a[i++];
while(i<n && x+r>=a[i]) i++;
int p=a[i-1];
while(i<n && p+r>=a[i]) i++;
ans++;
}
printf("%d\n",ans);
}
return 0;
}
POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心的更多相关文章
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- poj 3617 Best Cow Line (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- POJ 3069 Saruman's Army(贪心)
Saruman's Army Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3069 Saruman's Army 贪心模拟
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18794 Accepted: 9222 D ...
- poj 3617 Best Cow Line
http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...
- poj 3617 Best Cow Line 解题报告
题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...
随机推荐
- CF(438D) The Child and Sequence(线段树)
题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...
- DSP开发中遇到的问题 - 类指针未初始化后果
收到RECEIVE_REQ_MSG消息时会运行以下的代码,这里由于某种原因m_receiverSlaverController的值仍为NULL,并没有指向详细的CReceiverSlaverContr ...
- android 图片特效处理之锐化效果
这篇将讲到图片特效处理的锐化效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理: 一.简单算法:分别获取当前像素点和八个周围像素点的RGB值,先求出当前像素点的RGB值与八个像素点RGB值的 ...
- python Tricks —— list 镜像复制与 list comprehension 列表解析的顺序
0. 对 list 镜像复制,a = [1, 2, 3] ⇒ [1, 2, 3, 3, 2, 1] a*2 ⇒ a = [1, 2, 3, 1, 2, 3] a.extend(reversed(a)) ...
- LinkedIn微服务框架rest.li
linkedin/rest.li https://github.com/linkedin/rest.li LinkedIn微服务框架rest.li摘要:Rest.li是一款REST+JSON框架,使 ...
- 【深入篇】Andorid中常用的控件及属性
TextView android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/al ...
- 使用Redis配置JAVA_环境
配置环境变量 1.安装完成后,右击"我的电脑",点击"属性",选择"高级系统设置": 2.选择"高级"选项卡,点击&qu ...
- ZJOI2017线段树
ZJOI2017线段树 题意: 给你一颗广义线段树,太长了,自己去看. 题解: 直接上zkw那一套,把闭区间换成开区间,就是把取\([l,r]\),变成取\([l-1,l-1],[r+1,r+ ...
- Resource Access Based on Multiple Credentials
A collection of multiple user credentials each associated with one of multiple different users is ob ...
- Windows 7 系统的旧版IE浏览器升级到IE11
Windows 7 系统的旧版IE浏览器升级到IE11 2016年1月12日微软全面停止对IE8.IE9.IE10浏览器的支持,不再提供安全服务,如果继续使用将会造成安全隐患,因此 Windows 7 ...