zoukankan      html  css  js  c++  java
  • HDU 1029 基础dp

    题目链接:Ignatius and the Princess IV

    大意:就是在N个数里找出唯一一个至少出现过(N+1)/ 2 次的数。 1 <= N <= 999999。

    hash:

     1 /*
     2   题意好懂。就是在N个数里找出唯一一个至少出现过(N+1)/ 2 次的数。 1 <= N <= 999999.
     3   如果我用一个map 标记的话。大概也只是10^6吧。
     4   然而还是卡了cin 和 cout。
     5  */
     6 
     7 #include <stdio.h>
     8 #include <string.h>
     9 #include <iostream>
    10 #include <map>
    11 using namespace std;
    12 
    13 map<int, int>mp;
    14 
    15 int main() {
    16     int n;
    17     int num;
    18     int ans;
    19 
    20     while(~scanf("%d", &n)) {
    21         mp.clear();
    22         for (int i=0; i<n; ++i) {
    23             scanf("%d", &num);
    24             mp[num]++;
    25             if (mp[num] >= (n+1)/2) { // 找到了也不能break。因为可能还没有输入完、
    26                 ans = num;
    27             }
    28         }
    29         printf("%d
    ", ans);
    30     }
    31     return 0;
    32 }
    View Code

    dp:

     1 /*
     2  get到一个很巧妙的方法。按照题意。我求的那个数是素有数里出现的次数最多的,
     3  每次只拿当前的数和前面的所有数里出现次数最多的数比较。
     4  从过程来看?就是这道题的动态规划?
     5  */
     6 
     7 #include <stdio.h>
     8 #include <string.h>
     9 #include <string.h>
    10 #include <iostream>
    11 using namespace std;
    12 
    13 int main() {
    14     int n;
    15     int num, cnt, rem;
    16     while(~scanf("%d", &n)) {
    17         cnt = 0;
    18         for (int i=0; i<n; ++i) {
    19             scanf("%d", &num);
    20             if (cnt == 0) { // 计数
    21                 rem = num;
    22                 cnt = 1;
    23             }
    24             else if (num == rem) {
    25                 cnt++;
    26             }
    27             else {
    28                 cnt--;
    29             }
    30         }
    31         printf("%d
    ", rem);
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    Less35、Less36【无闭合注入,mysql_real_escape_string()】
    Less34【POST方式宽字符注入】
    Less29,30,31【jsp环境搭建、WAF】
    Less28、28a【select、union、空格过滤】
    Less27、27a【select、union、空格过滤】
    Less26,26a【空格符号过滤】
    Less25,25a【and/or过滤】
    Less24【二次注入】
    Less23【报错注入】
    NFC
  • 原文地址:https://www.cnblogs.com/icode-girl/p/5186697.html
Copyright © 2011-2022 走看看