Painting The Fence(贪心+优先队列)

Painting The Fence(贪心+优先队列)

题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1".

解题思路:贪心的思路,优先选择数量多的先填,这样会让最后剩余相同的数字数量最少,所以我们优先选数量最多的两种数字填,最后剩下的(某一种)就填到它前面的位置去,一定是和相同的填在一起,这里就不证明了,自己画下就可以得到。优先队列模拟即可。

AC_Code

  1 /* */
  2 #include <bits/stdc++.h>
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <ctime>
  6 #include <cmath>
  7 #include <utility>
  8 #include <algorithm>
  9 #include <cstring>
 10 #include <queue>
 11 #include <vector>
 12 #include <queue>
 13 #include <map>
 14 #include <set>
 15 #include <stack>
 16 using namespace std;
 17 #define mem0(a) memset(a,0,sizeof(a))
 18 #define mem1(a) memset(a,1,sizeof(a))
 19 typedef long long ll;
 20 typedef unsigned long long ull;
 21 const ll mod = 1e9+7;
 22 const int inf=0x3f3f3f3f;
 23 const int INF=0x3f3f3f3f3f3f3f3f;
 24 const double eps=0.000001;
 25 const double pi=acos(-1.0);
 26 
 27 int n,m,k;
 28 int a[200010],s[200010];
 29 
 30 struct node///重载,从大到小排序
 31 {
 32     int bianhao,num;
 33     bool operator<(const node &a) const {
 34         return num<a.num;
 35     }
 36 };
 37 
 38 priority_queue<node>q;
 39 
 40 int main()
 41 {
 42     while( ~ scanf("%d%d%d",&n,&m,&k))
 43     {
 44         mem0(a);
 45         mem0(s);
 46         while(!q.empty()) q.pop();
 47         for(int i=1; i<=m;i++)
 48         {
 49             node cur;
 50             scanf("%d",&cur.num);
 51             cur.bianhao=i;
 52             q.push(cur);
 53         }
 54         int cnt=0;
 55         while( !q.empty() )
 56         {
 57             if( q.size()>=2 )
 58             {
 59                 node n1=q.top(); q.pop();
 60                 node n2=q.top(); q.pop();
 61                 a[cnt++]=n1.bianhao;
 62                 a[cnt++]=n2.bianhao;
 63                 n1.num--;
 64                 n2.num--;
 65                 if( n1.num ) q.push(n1);
 66                 if( n2.num ) q.push(n2);
 67             }
 68             else
 69             {
 70                 if( a[cnt-1]!=q.top().bianhao )
 71                 {
 72                     node cur=q.top();q.pop();
 73                     a[cnt++]=cur.bianhao;
 74                     cur.num--;
 75                     if(cur.num) q.push(cur);
 76                 }
 77                 break;
 78             }
 79         }
 80         for(int i=0; i<cnt&&!q.empty(); i++)
 81         {
 82             if( a[i]==q.top().bianhao )
 83             {
 84                 s[i]+=min(q.top().num,k-1);
 85                 node cur=q.top();q.pop();
 86                 cur.num-=min(cur.num,k-1);
 87                 if( cur.num ) q.push(cur);
 88                 else break;
 89             }
 90         }
 91         if( !q.empty()) printf("-1\n");
 92         else
 93         {
 94             for(int i=0;i<cnt;i++)
 95             {
 96                 for(int j=0;j<=s[i]; j++)
 97                 {
 98                     printf("%d ",a[i]);
 99                 }
100             }
101             printf("\n");
102         }
103     }
104     return 0;
105 }

猜你喜欢

转载自www.cnblogs.com/wsy107316/p/11714494.html