贪心 Codeforces Round #289 (Div. 2, ACM ICPC Rules) B. Painting Pebbles

时间:2024-01-06 11:53:32

题目传送门

 /*
题意:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色去填充所有存在的pebbles,
使得任意两个piles,用颜色c填充的pebbles数量之差 <= 1。
如果不填充某种颜色,就默认数量为0。
1. 贪心:如果个数之间超过k个,那么填充什么颜色都会大于1,巧妙地思维
详细解释:http://blog.csdn.net/haoliang94/article/details/43672617
2. 比较每种填充颜色在n组里使用最多和最少的,若差值<=1,则YES,否则NO
详细解释:http://www.cnblogs.com/windysai/p/4265469.html
*/
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std; const int MAXN = + ;
const int INF = 0x3f3f3f3f;
int a[MAXN]; int main(void)
{
#ifndef ONLINE_JUDGE
freopen ("B.in", "r", stdin);
#endif int n, k;
while (~scanf ("%d%d", &n, &k))
{
int mx = -; int mn = INF;
for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]);
if (mx < a[i]) mx = a[i];
if (mn > a[i]) mn = a[i];
} if (mx - mn <= k)
{
puts ("YES");
for (int i=; i<=n; ++i)
{
int t = ;
for (int j=; j<=a[i]; ++j)
{
printf ("%d%c", ++t, (j==a[i]) ? '\n' : ' ');
if (t == k) t = ;
}
}
}
else
puts ("NO");
} return ;
}

 #include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>
using namespace std; const int MAXN = + ;
const int INF = 0x3f3f3f3f;
int a[MAXN];
int cnt[MAXN];
vector <int> v[MAXN]; int main(void)
{
#ifndef ONLINE_JUDGE
freopen ("B.in", "r", stdin);
#endif int n, k;
while (~scanf ("%d%d", &n, &k))
{
for (int i=; i<=MAXN; ++i)
v[i].clear (); for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]);
memset (cnt, , sizeof (cnt));
int t = ;
for (int j=; j<=a[i]; ++j)
{
cnt[++t]++;
if (t == k) t = ;
}
for (int j=; j<=k; ++j)
{
v[j].push_back (cnt[j]);
}
} bool flag = true;
vector<int>:: iterator it1, it2;
for (int i=; i<=k; ++i)
{
sort (v[i].begin (), v[i].end ());
it1 = v[i].end () - ;
it2 = v[i].begin ();
if (*it1 - *it2 > )
{
flag = false; break;
}
} if (flag)
{
puts ("YES");
for (int i=; i<=n; ++i)
{
int t = ;
for (int j=; j<=a[i]; ++j)
{
printf ("%d%c", ++t, (j==a[i]) ? '\n' : ' ');
if (t == k) t = ;
}
}
}
else
puts ("NO");
} return ;
}

解法2