• POJ 3617 Best Cow Line 贪心


    Best Cow Line
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 46960   Accepted: 11949

    Description

    FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

    The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows' names.

    FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

    FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.

    Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

    Input

    * Line 1: A single integer: N
    * Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line

    Output

    The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.

    Sample Input

    6
    A
    C
    D
    B
    C
    B

    Sample Output

    ABCBCD

    Source

     
     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 const int max_n=2000+20;
     7 
     8 int n;
     9 char a[max_n];
    10 
    11 void solve()
    12 {
    13     int left=0,right=n-1,cnt=0;
    14     while(left<=right)
    15     {
    16         ++cnt;
    17         bool flag=true;
    18 
    19         for(int i=0;i<=(right-left)/2;++i)
    20         {
    21             if(a[left+i]<a[right-i])
    22             {
    23                 break;
    24             }
    25             else if(a[left+i]>a[right-i])
    26             {
    27                 flag=false;
    28                 break;
    29             }
    30         }
    31 
    32         if(flag)
    33         {
    34             putchar(a[left]);
    35             ++left;
    36         }
    37         else
    38         {
    39             putchar(a[right]);
    40             --right;
    41         }
    42 
    43         if(cnt%80==0)
    44         {
    45             putchar('
    ');
    46         }
    47     }
    48 }
    49 
    50 int main()
    51 {
    52     scanf("%d",&n);
    53     for(int i=0;i<n;++i)
    54     {
    55         scanf("%s",&a[i]);
    56     }
    57     //a[n]='';
    58     //cout<<a<<endl;
    59     solve();
    60     return 0;
    61 }
  • 相关阅读:
    UICollectionView中使用 UICollectionViewFlowLayout进行布局(模仿苹果相册)
    使用CocoaPods被卡住:Updating local specs repositories
    【原】iOS 同时重写setter和getter时候报错:Use of undeclared identifier '_name';did you mean 'name'
    iOS 设置不同的字体颜色
    使用java代码,动态给TextView设置drawable
    格式化浮点数(保留指定位数)
    监听输入法的出现和隐藏
    dp和px的转换
    获取状态栏高度
    获取在attr.xml中声明的主题样式
  • 原文地址:https://www.cnblogs.com/jishuren/p/12244946.html
Copyright © 2020-2023  润新知