带来两题贪心算法的题。

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贪心的更多相关文章

  1. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  2. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  3. poj 3617 Best Cow Line (字符串反转贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Des ...

  4. POJ 3617 Best Cow Line 贪心算法

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26670   Accepted: 7226 De ...

  5. poj 3617 Best Cow Line 贪心模拟

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42701   Accepted: 10911 D ...

  6. POJ 3069 Saruman's Army(贪心)

     Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  7. poj 3069 Saruman's Army 贪心模拟

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18794   Accepted: 9222 D ...

  8. poj 3617 Best Cow Line

    http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...

  9. poj 3617 Best Cow Line 解题报告

    题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...

随机推荐

  1. XTUOJ 1206 Dormitory's Elevator

    Dormitory's Elevator Time Limit : 1000 MS   Memory Limit : 65536 KB Problem Description The new dorm ...

  2. 洛谷 P1994 有机物燃烧

    P1994 有机物燃烧 题目背景 本来准备弄难点的,还是算了吧 题目描述 输入一种有机物,输出与氧气反应化学方程式中CO2和H2O的系数 输入输出格式 输入格式: 一行,一个字符串,表示有机物 输出格 ...

  3. msp430在ccsv5下出现的问题总结

    一.内存问题 问题描写叙述,报错: program will not fit into available memory.  placement with alignment fails for se ...

  4. Xposed框架之函数Hook学习

    作者:Fly2015 Xposed是Android下Java层的开源Hook框架类似的有cydiasubstrate框架而且据说cydiasubstrate框架能实现Android的Java层和Nat ...

  5. RvmTranslator6.0

    RvmTranslator6.0 eryar@163.com 1. Introduction RvmTranslator can translate the RVM file exported by ...

  6. 中科燕园GIS外包--移动GIS

    移动GIS恰逢其时 得益于移动智能终端的普及和移动互联网的发展,伴随着GIS技术的发展和应用的深入.越来越多的企业和普通消费者開始体会到移动GIS的巨大潜力和价值. 移动GIS轻便灵活,受众面广.随时 ...

  7. uva103 - Stacking Boxes(DAG)

    题目:uva103 - Stacking Boxes(DAG) 题目大意:给出N个boxes, 而且给出这些箱子的维度.要求找一个最长的序列.可以使得以下的箱子一定可以有个维度序列大于上面的那个箱子的 ...

  8. Python 3 下载安装和环境搭建

    Python3 下载 由于博主使用的平台是Windows10,以下方法仅限win10系统 Python 官网:https://www.python.org/ 找到跟系统相应的版本瞎子: Python ...

  9. 洛谷——P3178 [HAOI2015]树上操作

    https://www.luogu.org/problem/show?pid=3178#sub 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 ...

  10. [PWA] Deal with caches in PWA

    The takeway is to know when we should cache the content? When we should clean the caches? 1. When sh ...