1. 程式人生 > 實用技巧 >A Magic Lamp HDU - 3183

A Magic Lamp HDU - 3183

題目:https://vjudge.net/problem/HDU-3183#author=0

題意:給定一個數字求刪除N為數字後的最小數字。

思路:貪心的思想每次刪除第一個a[i]>a[i+1],如果不存在就刪除最後一個。

  1 #include<time.h>
  2 #include <set>
  3 #include <map>
  4 #include <stack>
  5 #include <cmath>
  6 #include <queue>
  7 #include <cstdio>
  8
#include <string> 9 #include <vector> 10 #include <cstring> 11 #include <utility> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 #include <list> 16 using namespace std; 17 #define eps 1e-10 18 #define PI acos(-1.0) 19
#define lowbit(x) ((x)&(-x)) 20 #define zero(x) (((x)>0?(x):-(x))<eps) 21 #define mem(s,n) memset(s,n,sizeof s); 22 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);} 23 typedef long long ll; 24 typedef unsigned long long ull; 25 const int maxn=1e6+5; 26 const int Inf=0x7f7f7f7f
; 27 const ll Mod=999911659; 28 //const int N=3e3+5; 29 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判斷一個數是不是 2 的正整數次冪 30 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//對 2 的非負整數次冪取模 31 int getBit(int a, int b) { return (a >> b) & 1; }// 獲取 a 的第 b 位,最低位編號為 0 32 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 為 0,否則為 -1 33 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); } 34 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} 35 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} 36 int Abs(int n) { 37 return (n ^ (n >> 31)) - (n >> 31); 38 /* n>>31 取得 n 的符號,若 n 為正數,n>>31 等於 0,若 n 為負數,n>>31 等於 -1 39 若 n 為正數 n^0=n, 數不變,若 n 為負數有 n^(-1) 40 需要計算 n 和 -1 的補碼,然後進行異或運算, 41 結果 n 變號並且為 n 的絕對值減 1,再減去 -1 就是絕對值 */ 42 } 43 ll binpow(ll a, ll b,ll c) { 44 ll res = 1; 45 while (b > 0) { 46 if (b & 1) res = res * a%c; 47 a = a * a%c; 48 b >>= 1; 49 } 50 return res%c; 51 } 52 void extend_gcd(ll a,ll b,ll &x,ll &y) 53 { 54 if(b==0) { 55 x=1,y=0; 56 return; 57 } 58 extend_gcd(b,a%b,x,y); 59 ll tmp=x; 60 x=y; 61 y=tmp-(a/b)*y; 62 } 63 ll mod_inverse(ll a,ll m) 64 { 65 ll x,y; 66 extend_gcd(a,m,x,y); 67 return (m+x%m)%m; 68 } 69 ll eulor(ll x) 70 { 71 ll cnt=x; 72 ll ma=sqrt(x); 73 for(int i=2;i<=ma;i++) 74 { 75 if(x%i==0) cnt=cnt/i*(i-1); 76 while(x%i==0) x/=i; 77 } 78 if(x>1) cnt=cnt/x*(x-1); 79 return cnt; 80 } 81 char str[maxn]; 82 int num[maxn]; 83 int main() 84 { 85 int n,j,i; 86 while(~scanf("%s %d",str,&n)) 87 { 88 mem(num,0); 89 int len=strlen(str); 90 for( i=0;i<len;i++) num[i]=str[i]-'0'; 91 if(n>len) 92 { 93 puts("0"); 94 continue; 95 } 96 while(n--) 97 { 98 for( i=0;i<len;i++) 99 { 100 if(num[i]>0) 101 { 102 for( j=i+1;j<len;j++) 103 { 104 if(num[j]>=0) break; 105 } 106 if(num[i]>num[j]) 107 { 108 num[i]=-1; 109 break; 110 } 111 } 112 } 113 } 114 bool flag=false; 115 for(int i=0;i<len;i++) 116 { 117 if(num[i]==-1) continue; 118 if(num[i]>0) flag=1; 119 if(flag) printf("%d",num[i]); 120 } 121 if(!flag) cout<<"0"; 122 cout<<endl; 123 } 124 return 0; 125 }
View Code

如何證明這種貪心的正確性呢?

如果每次都不刪除a[i]那麼最終a[i]肯定在剩下數字中,又因為a[i]>a[i+1]那麼我們完全可以刪除a[i]來達到最小數字。