zoukankan      html  css  js  c++  java
  • AGC016B Colorful Hats(构造)

    题目大意:

    给定n和n个数,每个数a[i]代表除了i外序列中颜色不同的数的个数,问能否构造出来这个数列。

    比较简单,首先先求出来a数列的最大值Max,

    如果有数小于Max-1,那么显然是不存在的

    接下来就是有m个数等于Max-1,n-m个数等于Max

    那么可以知道m个数中每个数肯定是有且只有一种颜色

    所以m<Max,剩下的必须至少有2个,所以条件就是m<Max && m + 2*(Max-m) <= n

    特殊情况:所有数都等于Max,这时候有2种情况,一种是每个数都是不同的,一种是2*Max <= n,判断一下就好

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <map>
    using namespace std;
    int n;
    int a[1000000];
    map<int, int>M;
    int main()
    {
        cin>>n;
        for(int i = 0; i < n; i++) cin>>a[i];
        int Max = 0, t = 0;
        for(int i = 0; i < n; i++){
            if(!M[a[i]]) { M[a[i]] = 1; t++; Max = max(Max, a[i]); }
        }
        for(int i = 0; i < n; i++) if(a[i] < Max-1) { cout<<"No"<<endl; return 0; }
        if(t > 2) { cout<<"No"<<endl; }
        else {
            int m = 0;
            for(int i = 0; i < n; i++) if(a[i] == Max-1) m++;
            if(m == 0) {
                if(2*(Max-m) <= n) cout<<"Yes"<<endl;
                else if(Max == n-1) cout<<"Yes"<<endl;
                else cout<<"No"<<endl;
                return 0;
            }
            if(m < Max && (m + 2*(Max-m) <= n)) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
  • 相关阅读:
    leetcode-Binary Tree Inorder Traversal
    leetcode- Isomorphic Strings
    Ascii码表对应(摘至百度)
    leetcode-Happy Number
    leetcode-Bulls and Cows
    leetcode-Group Anagrams
    14、排序:插入类排序和交换类排序
    13、自平衡二叉查找树AVL
    11、创建Huffman树,生成Huffman编码
    10、二叉树的遍历+查找
  • 原文地址:https://www.cnblogs.com/Saurus/p/7048324.html
Copyright © 2011-2022 走看看