码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3617 Best Cow Line

时间:2016-03-10 20:15:31      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

简单贪心。注意输出格式,每到80个字符就换一行。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn = 2000 + 10;
int a[maxn], ans[maxn];
int n;

void read()
{
    for (int i = 1; i <= n; i++)
    {
        char s[5]; scanf("%s", s);
        a[i] = s[0] - A + 1;
    }
}

void work()
{
    int first = 1, last = n;
    for (int i = 1; i <= n; i++)
    {
        int tmp = last - first + 1;
        bool f = 0;
        for (int j = 0; j<tmp / 2; j++)
        {
            if (a[first + j]>a[last - j])
            {
                ans[i] = a[last], last--, f = 1;
                break;
            }
            else if (a[first + j]<a[last - j])
            {
                ans[i] = a[first], first++, f = 1;
                break;
            }
        }
        if (f == 0) ans[i] = a[first], first++;
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        printf("%c", ans[i] - 1 + A); cnt++;
        if (cnt == 80)
        {
            printf("\n");
            cnt = 0;
        }
    }
    printf("\n");
}

int main()
{
    while (~scanf("%d", &n))
    {
        read();
        work();
    }
    return 0;
}

 

POJ 3617 Best Cow Line

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5263137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!