总是很羡慕边上的人的各种优秀,尝试着去模仿别人,可是这样真的好累,好好做自己、、、
在看一本书,于是打算把这本书的习题,都mark一下、、、希望今天开始,努力起来、、、
1、 Best Cow Line 【贪心】 链接:http://poj.org/problem?id=3617
找出最小的字符,放在最末尾即可、同时要考虑首尾一样的字符时,要接着看下一位,在for 语句那边
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n;
char tt[2100];
int main()
{
while(~scanf("%d",&n))
{
for(int i = 0; i < n; i ++)
{
getchar();
scanf("%c",&tt[i]);
}
int l = 0, r = n - 1;
int cnt = 0;
while(l <= r)
{
bool ok = false;
for(int i = 0; l + i <= r; i ++)
{
if(tt[l + i] < tt[r - i])
{
ok = true;
break;
}
else if(tt[l + i] > tt[r - i])
{
ok = false;
break;
}
}
cnt ++;
if(ok)
printf("%c",tt[l ++]);
else
printf("%c",tt[r --]);
if(cnt % 80 == 0) // 注意一下,小细节、
printf("\n");
}
printf("\n");
}
}
2、 Saruman's Army 【贪心】 链接:http://poj.org/problem?id=3069
一开始是想着遍历的,然后列举了一些情况,觉得遍历加判断加标记好像不能解决这个问题,最后,还是忍不住地看了书,觉得思路差不多啊!!
从最左边开始,尽可能地去覆盖右边的点,如果不可以,计数cnt + 1, 就是所谓的贪心, 最后的cnt 的值 即为 答案。
突然发现,这边好喜欢用while 、、
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int tt[1010];
int main()
{
int n, r;
while(~scanf("%d%d",&r,&n))
{
if(r == -1 && n == -1)
break;
memset(tt, 0, sizeof(tt));
for(int i = 0; i < n; i ++)
scanf("%d", &tt[i]);
sort(tt , tt + n);
int cnt = 0;
int i = 0;
while(i < n)
{
int s = tt[i ++];
while(i < n && tt[i] <= s + r)
i ++;
int p = tt[i - 1]; // 可以理解为中间点,既照顾左边,又可以在下一个语句中尽量去照顾右边的点、
while(i < n && tt[i] <= p + r) // 尽量去覆盖右边的点、
i ++;
cnt ++;
}
printf("%d\n", cnt);
}
}