HDU ACM 3183 A Magic lamp->rmq st algorithm or greedy

Source: Internet
Author: User

Analysis:

1, greedy, the array to scan, if a[i]>a[i+1] to delete the a[i], until the removal of m; if the number of deleted after the scan is not enough m, then the number of the end of the list is deleted until the total deletion of M, the remaining number of the array is the result from start to finish.

2, RMQ algorithm to solve, assuming that the length of the array is Len, the number of numbers to delete is M, then the final array will be left len-m, so you can find the first in the array to m+1 the smallest number as the final result of the first position, and assume that the minimum number is labeled I, and then find the i+1 to the first m+ The minimum number of 2 as the final result of the second, subscript for J, and then from the first j+1 to M+3 to find a third, and so on, find out.

#include <iostream>using namespace Std;int dp[1010][33];char num[1010];char res[1010];void getrmq (int n)            // Note that the DP array holds subscript {int i,j;for (i=0;i<=n;i++) dp[i][0]=i;for (j=1; (1<<j) <=n;j++) for (i=0;i+ (1<<J)-1 <n;i++) dp[i][j]=num[dp[i][j-1]]<=num[dp[i+ (1<< (j-1))][j-1]]?dp[i][j-1]:d p[i+ (1<< (j-1))][j-1] ;} int Query (int l,int r) {int K=0;while (1<< (k+1) <=r-l+1) K++;return num[dp[l][k]]<=num[dp[r-(1<<k) +1] [K]]? Dp[l][k]:d p[r-(1<<k) +1][k];} int main () {int m,pos,k,i;while (scanf ("%s%d", num,&m) ==2) {pos=0;k=0;i=0; GETRMQ (strlen (num)), while (M+i<strlen (num)) {pos=query (pos,m+i); res[k++]=num[pos++];i++;} for (i=0;i<k;i++) if (res[i]!= ' 0 ') break;if (i==k) Putchar (' 0 '); else{for (; i<k;i++) Putchar (Res[i]);} Putchar (' \ n ');} return 0;}


HDU ACM 3183 a Magic lamp->rmq st algorithm or greedy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.