P2870 [USACO07DEC]最佳牛線,黃金Best Cow Line, Gold
poj 3617 http://poj.org/problem?id=3617ios
題目描述
FJ is about to take his N (1 ≤ N ≤ 500,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.app
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.ide
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.this
Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.spa
每次只能從兩邊取,要求取出來以後字典序最小3d
輸入格式
* Line 1: A single integer: Ncode
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.blog
樣例ci
輸入 6 A C D B C B 輸出 ABCBCD
簡單的說就是從輸入字符串隊首隊尾抽取字符串組成輸出 要求輸出字符串字典序最小
算法1
咱們來貪心選取 隊首隊尾哪一個字典序小 就選擇哪一個。
關鍵在隊首隊尾相同那麼就看次一級的字母的字典序 可是要考慮諸如 aca vav aa 等邊界狀況。
須要處理好
吐槽下 POJ 的提交和沒有錯誤提示。 洛谷卻是不錯 就是數據量變大了 把我代碼卡成TLE了
C++ 代碼
#include <iostream> #include <queue> #include <string> using namespace std; char input[500010]; char output[500010]; int idx; int SelectCopy(int l, int r) { if (l >= r) return 1; if (input[l] < input[r]) { return -1; } else if (input[l] > input[r]) { return 1; } else { int copyl = l + 1; int copyr = r - 1; return SelectCopy(copyl, copyr); } return 0; } void Select(int l, int r) { if (l == r) { output[idx] = input[l]; idx++; return; } if (l > r) return; if (input[l] < input[r]) { output[idx] = input[l]; idx++; l++; } else if (input[l] > input[r]) { output[idx] = input[r]; idx++; r--; } else { //相等 int copyl = l + 1; int copyr = r - 1; int selectidx = SelectCopy(copyl, copyr); if (-1 == selectidx) { output[idx] = input[l]; idx++; l++; } else if (1 == selectidx) { output[idx] = input[r]; idx++; r--; } } if (l <= r) { Select(l, r); } } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { char t; cin >> t; input[i] = t; } int l = 0; int r = N - 1; Select(l, r); for (int i = 0; i < idx; i++) { if (i % 80 == 0 && i>=80) cout << endl; cout << output[i]; } return 0; } 做者:defddr 連接:https://www.acwing.com/blog/content/178/ 來源:AcWing 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。