• 贪心 洛谷P2870 Best Cow Line, Gold


    [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

    题目描述

    FJ is about to take his N (1 ≤ N ≤ 30,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.

    每次只能从两边取,要求取出来之后字典序最小

    输入输出格式

    输入格式:

    • 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

    输出格式:

    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.

    输入输出样例

    输入样例#1:
    6
    A
    C
    D
    B
    C
    B
    输出样例#1:
    ABCBCD

    代码很丑勿喷
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int n;
     7 int head,tail,cnt;
     8 char in[30010];
     9 int main(){
    10     cin>>n;
    11     for(int i=1;i<=n;i++) cin>>in[i];
    12     head=1;
    13     tail=n;
    14     for(int i=1;i<=n;i++){
    15         if(in[head]==in[tail]){
    16             int hhead=head,ttail=tail;
    17             bool check=0;
    18             while(hhead<=ttail){
    19                 if(cnt==80){
    20                     printf("
    ");
    21                     cnt=0;
    22                 }
    23                 if(in[hhead]<in[ttail]){
    24                     printf("%c",in[head]);
    25                     cnt++;
    26                     head++;
    27                     check=1;
    28                     break;
    29                 }
    30                 else if(in[hhead]>in[ttail]){
    31                     printf("%c",in[tail]);
    32                     cnt++;
    33                     tail--;
    34                     check=1;
    35                     break;
    36                 }
    37                 
    38                 hhead++;
    39                 ttail--;
    40             }
    41             if(!check){
    42                 if(cnt==80){
    43                     printf("
    ");
    44                     cnt=0;
    45                 }
    46                 printf("%c",in[head]);
    47                 cnt++;
    48                 head++;
    49                 
    50             }
    51         }
    52         else {
    53             if(cnt==80){
    54                 printf("
    ");
    55                 cnt=0;
    56             }
    57             if(in[head]<in[tail]){
    58                 printf("%c",in[head]);
    59                 cnt++;
    60                 head++;
    61             }
    62             else{
    63                 printf("%c",in[tail]);
    64                 cnt++;
    65                 tail--;
    66             }
    67             
    68         }
    69     }
    70     if(cnt) printf("
    ");
    71     return 0;
    72 }


  • 相关阅读:
    Lucene 全文检索
    Redis 集群
    Redis 初步接触
    Mybatis
    FastJson 介绍
    JAVA微信企业付款到零钱(十分钟搞定),附完整DEMO下载
    持续集成与Devops关系
    GIT命令行统计代码提交行数
    一种简单的REST API接口加密实现,只允许自己的产品调用后台,防止接口被刷
    Beyond Compare 4.X 破解方法(亲测有效)
  • 原文地址:https://www.cnblogs.com/zwube/p/6906502.html
Copyright © 2020-2023  润新知