zoukankan      html  css  js  c++  java
  • [构造] F. Maximum Balanced Circle

    There are nn people in a row. The height of the ii-th person is aiai. You can choose any subset of these people and try to arrange them into a balanced circle.

    A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than 11. For example, let heights of chosen people be [ai1,ai2,…,aik][ai1,ai2,…,aik], where kk is the number of people you choose. Then the condition |aij−aij+1|≤1|aij−aij+1|≤1should be satisfied for all jj from 11 to k−1k−1 and the condition |ai1−aik|≤1|ai1−aik|≤1 should be also satisfied. |x||x| means the absolute value of xx. It is obvious that the circle consisting of one person is balanced.

    Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.

    Input

    The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of people.

    The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the height of the ii-th person.

    Output

    In the first line of the output print kk — the number of people in the maximum balanced circle.

    In the second line print kk integers res1,res2,…,reskres1,res2,…,resk, where resjresj is the height of the jj-th person in the maximum balanced circle. The condition |resj−resj+1|≤1|resj−resj+1|≤1 should be satisfied for all jj from 11 to k−1k−1 and the condition |res1−resk|≤1|res1−resk|≤1 should be also satisfied.

    题意:

    找出一个环, 使得相邻元素最大差值为1,且环最长

    思路: bucket计数

    尺取记录符合条件的最大区间, 左一右一中间多即可

    代码:

    /*
        Zeolim - An AC a day keeps the bug away
    */
    
    //pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <bitset>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/assoc_container.hpp>
    using namespace std;
    //using namespace __gnu_pbds;
    //typedef tree <long long, null_type, less <long long>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    const ld PI = acos(-1.0);
    const ld E = exp(1.0);
    const int INF = 0x3f3f3f3f;
    const int MAXN = 1e6 + 10;
    const ll MOD = 1e9 + 7;
    
    int cnt[MAXN] = {0};
    
    int main()
    {
        //ios::sync_with_stdio(false);
        //cin.tie(0);     cout.tie(0);
        //freopen("D://test.in", "r", stdin);
        //freopen("D://test.out", "w", stdout);
    	
    	int n, ma = 0;
    
    	cin >> n;
    
    	for(int i = 0, x; i < n; ++i)
    	{
    		cin >> x;
    		++cnt[x];
    		x > ma ? ma = x : x;
    	}
    
    	int ans = 0, pa = 0, pb = 0, sum = 0;
    
    	for(int i = 1, j; i <= ma; ++i)
    	{
    		sum = 0;
    		
    		if(cnt[i])
    		{
    			if(cnt[i] == 1)
    				sum = 1;
    				
    			else sum = cnt[i];
    			
    			j = i;
    			
    			while(cnt[j + 1] >= 2) sum += cnt[j + 1], ++j;
    			
    			if(cnt[j + 1]) ++sum, ++j;
    			
    			if(sum > ans)
    			{
    				ans = sum, pa = i, pb = j;
    			}
    			
    			if(j != i)
    				i = j - 1;
    		}
    	}
    	
    	deque <int> DQ;
    
    	for(int i = pb; i >= pa; i--)
    	{
    		while(cnt[i])
    		{
    			if(cnt[i] & 1)
    				DQ.push_back(i);
    			else
    				DQ.push_front(i);
    			--cnt[i];
    		}
    	}
    
    
    	cout << ans << '
    ';
    
    	for(auto x : DQ)
    		cout << x << ' ';
    		
        return 0;
    }
  • 相关阅读:
    「ruby/MiniMagick」用MiniMagick处理图片
    「thunar」给thunar增加搜索文件功能
    Software--Architecture--SOA Factory
    DataArchitecture--数据结构与算法 (Java)
    Software--Architecture--SOA 面向服务体系结构
    Software--Develop -- WCF Setting
    Industry--OPC UA (OPC Unified Architecture) 统一架构
    Software--IoC 依赖倒置 控制反转
    虚拟表dual。字符串函数UPPER,LOWER。&变量。INITCAP,LENGTH,SUBSTR
    运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270368.html
Copyright © 2011-2022 走看看