【TOJ 4493】Remove Digits(單調棧貪心)
阿新 • • 發佈:2018-09-08
dig 描述 har etc 貪心 digi its The 輸出
描述
Given an N-digit number, you should remove K digits and make the new integer as large as possible.
輸入
The first line has two integers N and K (1 ≤ K<N≤500000).
The next line has a N-digit number with no leading zero.
輸出
Output the largest possible integers by removing K digits.
樣例輸入
4 2
2835
樣例輸出
85
題意:
給定一個n位數字,刪去k個數字使之最大。
思路:
利用單調棧進行貪心,即保持棧為降序的過程中,記錄刪除數字的個數,刪到k個即止。
#include<bits/stdc++.h> #define MAX 500005 using namespace std; int main() { char st[MAX],ch; int top=-1,n,k,i; cin>>n>>k; st[++top]=‘9‘; getchar();for(i=0;i<n;i++) { ch=getchar(); while(ch>st[top]&&k) { top--; k--; } st[++top]=ch; } printf("%s",st+1); return 0; }
【TOJ 4493】Remove Digits(單調棧貪心)