Best Cow Line

POJ - Best Cow Line(贪心) Best Cow Line 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 阅读详情

Best Cow Line

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

C++编写:

贪心算法
第一种(个人觉得相对较简单):

#include<iostream>
using namespace std;
const int MAX_N=2000;
char Cow[MAX_N+10];     //记录初始顺序
char Cow1[MAX_N+10];    //记录字典序最小的顺序

void sort_print(int x)
{
    int k=0;          
    int a=0,b=x-1;
    while(a<=b)
    {
        bool left = false;
        for(int i=0;a+i<=b;i++)
        {
            if(Cow[a+i] < Cow[b-i])
            {
                left = true; 
                break;
            }
            else if(Cow[a+i] > Cow[b-i])
            {
                left = false;
                break;
            }
        }
        if(left)    
            Cow1[k++] = Cow[a++];
        else
            Cow1[k++] = Cow[b--];            
    }  
    for(int i=0;i<x;i++)
    {
        cout<<Cow1[i];
        if(i%80==79)  cout<<endl;
    }
    if(x % 80)  cout<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    int N;
    while(cin>>N)
    {
        for(int i=0;i<N;i++)
            cin>>Cow[i];
        sort_print(N);
    }
}

第二种(其实也差不多)

#include<iostream>
using namespace std;
const int MAX_N=2000;
char Cow[MAX_N+10];      //记录初始顺序
char Cow1[MAX_N+10];     //记录字典序小的顺序
int N;

void sort_print()
{     
    for(int i=0;i<N;i++)
        cin>>Cow[i];
    int a=0,b=N-1;
    for(int i=0;i<N;i++)
    {
        if(Cow[a] > Cow[b])   Cow1[i]=Cow[b--]; 
        else if (Cow[a] < Cow[b])   Cow1[i]=Cow[a++];
        else
        {
            int j=0;
            bool flag = false;
            while (Cow[a+j] == Cow[b-j] && a+j <= b-j) 
                j++;
            flag = (Cow[a+j] < Cow[b-j]);
            if (flag) Cow1[i] = Cow[a++];
            else  Cow1[i] = Cow[b--];
        }
    }  
    for(int s=0;s<N;s++)
    {
        cout<<Cow1[s];
        if(s % 80 == 79)  
        cout<<endl;
    }
    if(N % 80)  cout<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>N)
        sort_print();
    return 0;
}
【题解】POJ 3617 Best Cow Line(贪心) POJ 3617 Best Cow Line 原题 https://vjudge.net/problem/POJ-3617 FJ准备带他的N(1≤N≤2000)头奶牛参加一年一度的“年度农民”比赛。在这场比赛中,每个农民把他的奶牛排成一排,并把它们赶过裁判。 比赛的组织者今年采用了一种新的登记方案:只要按照每头奶牛出现的顺序登记它们的首字母即可。,如果FJ按此顺序接收贝西、西尔维娅和朵拉,他... 阅读详情

相关推荐

Modbus TCP 通信(C#)

【代码】Modbus TCP 通信(C#)

weixin_56229359的博客 1150

Best Cow Line--POJ3617

《挑战程序设计竞赛第二版》

小菜鸟博客 339

中国裁判文书网案例批量爬取

本文介绍了某法院网站数据采集的技术研究过程,主要包含JS逆向分析和数据解密方法。通过开发者工具分析发现页面数据经过加密处理,重点研究了ciphertext等加密参数的生成逻辑。文章详细记录了定位加密函数、扣取加密代码、解密数据处理的全流程,并验证了解密方法的有效性。研究过程中强调了技术使用的法律边界,明确声明仅用于合法技术研究,禁止任何非法数据爬取行为。最终实现了加密数据的成功解密,并解释了各请求参数的作用,为后续数据清洗和存储奠定了基础。

2303_80825459的博客 816

BEST COW LINE(贪心)

BEST COW LINE(贪心) 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 schem

m0_50690578的博客 245

用angr找到strcpy的栈溢出漏洞

这篇文章其实是分析angr文档里给出的一个挖漏洞的小例子。 原文链接在此:https://docs.angr.io/examples#beginner-vulnerability-discovery-example-strcpy_find 这是第一个用angr在二进制里找可利用条件的教程。第一个例子是一个非常简单的程序。这个angr脚本会从程序的entry到strcpy找到一条路径。(只有我们能够控制strcpy的参数的时候) 为了找到正确的路径,angr需要去算一个密码参数,算这个密码,angr只要2s

^_^ 1220

【FPGA】AD9694调试注意事项

基于FPGA的AD9694设计及其注意事项,包括同步的设计注意事项

weixin_41246509的博客 2902

【POJ 3617 --- Best Cow Line】

【POJ 2376 --- Cleaning Shifts】 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 he...

来日浅谈的博客 471

PCL SAC-IA 初始配准算法【2025最新版】

采样一致性初始配准算法,简称SAC_IA。博客长期更新,本文最新更新时间为:2025年1月1日。新增参数设置建议

点云侠的博客 37万+

POJ 3617 Best Cow Line 贪心算法

题目链接:Best Cow Line 贪心策略是:一直从S字串两侧寻找小的字符,特殊情况是:如果两侧字符一样,向内侧比较并获取答案。(不过感觉这步做了很多无用功,尤其是回文串的情况) 代码如下: View on Github #include #include #include #include using namespace std; vector S; bool operat

Kiritow的学园 565

ADES1831和ADBMS6830

1、上电默认进入STANDBY模式,SPI发送0xFF也可以进入STANDBY模式2、看门狗定时器超时后,进入SLEEP状态3、配置寄存器中的REFON位使用WRCFGA命令将组A设置为1进入REFUP状态,此状态比待机状态更快地启动ADC转换。i<TOTAL_IC;i++)注意:读AFE数据时会随着AFE片数增加而增加,原因是唤醒时间和AFE片数密切相关,如果需要缩短时间,那么减少唤醒频次。

m0_61973119的博客 266

面向高速Serdes的信号完整性优化:从封装 - 板级互连局限到封装内互连技术演进(一)

本文针对高速SerDes互连链路中的关键电磁效应展开研究:1)揭示了封装-主板互连结构在毫米波频段易激发谐振效应,导致53GHz传输陷波和阻抗偏移;2)提出基于本征模分析的谐振抑制方法,通过优化接地过孔排布使带宽突破95GHz;3)系统比较了方形/六边形BGA排布下23种差分对朝向的串扰特性,证实六边形阵列可将功率和串扰降低30%以上;4)论证了100GHz以上带宽需采用封装内互连架构,其较传统方案减少50%阻抗不连续节点。

小孟同学的博客 230

PKU 3617 Best Cow Line和PKU 3623 Best Cow Line 解题报告

PKU 3617和PKU 3623是一样的题,只是数据范围不同。3617是2000个字符,而3623是30000个字符。只是3623的时限比较松,贪心也可以过。本题有两种解法,分别是贪心O(n^2)和后缀数组O(nlogn)后缀数组法:/*  Title: Best Cow Line  Author: Jeff  Time:  2008/10/30  Complexity: O(Nlog(N));

ldyhot的专栏 1281

某头部金融APP防护机制逆向实录:从反调试到TEE加密的全链路攻防拆解

金融APP安全防护体系深度破解分析 本文详细拆解了某头部金融APP的三重防御机制破解过程。首先通过Frida绕过反调试技术,包括ptrace自我绑定、TracerPid检测和debuggerd监控;随后针对12种Root检测方法(如su文件检查、Magisk检测等)进行环境伪装,通过Hook系统调用伪造安全环境;最后逆向分析TEE加密模块,解密关键接口参数。整个破解过程涉及静态分析、动态调试和代码注入技术,揭示了金融级APP安全防护的实现原理与突破方法(注:所有操作均获授权,仅用于安全研究)。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1400

贪心算法训练(九)——Best Cow Line(字典序最小问题)

贪心算法训练(九)——Best Cow Line(字典序最小问题) 原题链接:Best Cow Line 1. 问题描述 2. 输入 6 A C D B C B 3. 输出 ABCBCD 4.思路分析   不断地取原字符串 S 中开头和末尾比较小的字符串放到 T 的末尾   特殊情况:S 的开头和末尾...

Nikki_o3o的博客 642

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

上一篇: Curling 2.0
下一篇: Saruman's Army
Slatter
博客等级 码龄8年 104粉丝 128原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值