zoukankan      html  css  js  c++  java
  • 洛谷P1360 [USACO07MAR]黄金阵容均衡题解

    题目

    不得不说这个题非常毒瘤。

    简化题意

    这个题的暴力还是非常好想的,完全可以过(50\%)的数据。但是(100\%)就很难想了。

    因为数据很大,所以我们需要用(O(sqrt n))的时间求出每个时间的前面第一个跟它满足均衡区间的时间。

    此时我们会很快速的想到(Hash)(Map),他们的时间复杂度是小于(O(sqrt n)的)所以满足要求,因此一般看到寻找相同得值的时候,最好还是想想怎么优化吧

    (Code)

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <map>
    #include <vector>
    using namespace std;
    int n, m, a, ans;
    map <vector <int>, int>dp;//表示每个数组所对应的第一次出现的天数。 
    signed main()
    {		
        scanf("%d%d", &n, &m);
        vector <int> sum(m + 3);//sum[j]表示j这个数的能力值和1这个数的能力值差值。 
        dp[sum] = 0;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &a);
            for (int j = 1; j <= m; j++)	
            	if (a & (1 << (j - 1))) 
            		sum[j]++;
            if (a % 2 == 1)//如果该能力提升数字增加了一这个数,那所有sum值都减少1。 
            	for (int j = 1; j <= m; j++)//这里用来保证状态有可能会有重复的,	
            		sum[j]--;
           for (int i = 1; i <= m; i++)	
            	printf("%d ", sum[i]);
        	if (dp.count(sum)) 
    			ans = max(ans, i - dp[sum]); 
    //			printf("%d %d
    ", i, dp[sum]);               
    		else dp[sum] = i;
        }
        printf("%d", ans);
    }	
    /*
    7 3
    7
    6
    7
    2
    5
    4
    2
    */
    
  • 相关阅读:
    细说javascripe事件传播流程
    由浅入深的讲述Get和Post的区别
    使用windowsAPI 加载shellcode
    从PE资源加载和执行Shellcode
    DLL注入
    shellcode注入原理
    asp.net学习--ashx一句话木马
    asp.net学习--asmx一句话木马
    asp.net学习--svc一句话木马
    php--莫客服系统代码审计(已申请CNVD)
  • 原文地址:https://www.cnblogs.com/liuwenyao/p/10245413.html
Copyright © 2011-2022 走看看