初出茅庐 【 题集 】 (一) 更新 ing ......

    总是很羡慕边上的人的各种优秀,尝试着去模仿别人,可是这样真的好累,好好做自己、、、

    在看一本书,于是打算把这本书的习题,都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);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值