zoukankan      html  css  js  c++  java
  • 洛谷P2870 [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

    【题解】
    贪心即可。
    正解sa,但我暴力了,留着以后学了sa再写

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <cstring>
      5 
      6 const int INF = 0X3f3f3f3f;
      7 const int MAXN = 3000000 + 10;
      8 
      9 char s[MAXN];
     10 int n, cnt;
     11 
     12 int main()
     13 {
     14     scanf("%d", &n);char ch;
     15     for(register int i = 1;i <= n;++ i) 
     16     {
     17         ch = getchar();
     18         while(ch < 'A' || ch > 'Z')ch = getchar();
     19         s[i] = ch;
     20     }
     21     int l = 1, r = n;
     22     while(l < r - 1)
     23     {
     24         if(s[l] < s[r]) 
     25         {
     26             printf("%c", s[l]), ++ l;
     27             ++ cnt;
     28             if(cnt == 80)
     29             {
     30                 printf("
    ");
     31                 cnt = 0;
     32             }
     33         }
     34         else if(s[l] > s[r])
     35         {
     36             printf("%c", s[r]), -- r;
     37             ++ cnt;
     38             if(cnt == 80)
     39             {
     40                 printf("
    ");
     41                 cnt = 0;
     42             }
     43         }
     44         else
     45         {
     46             int p = l, q = r;
     47             while(p < q - 1 && s[p] == s[q])++ p, -- q;
     48             if(( p == q) || (p == q - 1 && s[p] == s[q]) )
     49             {
     50                 char ch = s[l];
     51                 for(;l < p && s[l] == ch;++ l)
     52                 {
     53                     printf("%c", s[l]);
     54                     ++ cnt;
     55                     if(cnt == 80)
     56                     {
     57                         printf("
    ");
     58                         cnt = 0;
     59                     }
     60                 }
     61             }
     62             else if(s[p] < s[q])
     63             {
     64                 char ch = s[l];
     65                 for(;l < p && s[l] == ch;++ l)
     66                 {
     67                     printf("%c", s[l]);
     68                     ++ cnt;
     69                     if(cnt == 80)
     70                     {
     71                         printf("
    ");
     72                         cnt = 0;
     73                     }
     74                 }
     75             }
     76             else if(s[p] > s[q])
     77             {
     78                 char ch = s[r];
     79                 for(;r > q && s[r] == ch;-- r)
     80                 {
     81                     printf("%c", s[r]);
     82                     ++ cnt;
     83                     if(cnt == 80)
     84                     {
     85                         printf("
    ");
     86                         cnt = 0;
     87                     }
     88                 }
     89             }
     90         }
     91     }
     92     if(l == r)printf("%c", s[l]);
     93     else if(s[l] < s[r])
     94     {
     95         printf("%c", s[l]);
     96         ++ cnt;
     97         if(cnt == 80)
     98         {
     99             printf("
    ");
    100             cnt = 0;
    101         }
    102         printf("%c", s[r]);
    103     }
    104     else 
    105     {
    106         printf("%c", s[r]);
    107         ++ cnt;
    108         if(cnt == 80)
    109         {
    110             printf("
    ");
    111             cnt = 0;
    112         }
    113         printf("%c", s[l]);
    114     }
    115     return 0;
    116 }
    洛谷P2870 Best Cow Line
  • 相关阅读:
    hdu 4563
    CPU中的主要的寄存器
    ROC曲线及AUC
    light oj 1231 dp 多重背包
    light oj 1422 区间dp
    light oj 1098 数学规律
    light oj 1095 组合数学
    DRAM & SRAM
    C-static
    C-枚举
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7397653.html
Copyright © 2011-2022 走看看