#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=100+5,M=1000+5,Z=1e9+7,ms63=1061109567;
int n,k;
int a[N];
pair<int,int>b[M];
int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
int m;
for(m=1;m<=k;++m)
{
int minv=1e9;
int maxv=-1e9;
int minp,maxp;
for(int j=1;j<=n;++j)
{
if(a[j]<minv){minv=a[j];minp=j;}
if(a[j]>maxv){maxv=a[j];maxp=j;}
}
if(maxv-minv>=2){++a[minp];--a[maxp];b[m]=MP(maxp,minp);}
else break;
}--m;
sort(a+1,a+n+1);
printf("%d %d\n",a[n]-a[1],m);
for(int i=1;i<=m;++i)printf("%d %d\n",b[i].first,b[i].second);
}
return 0;
}
/*
【题意】
有n(100)个数。每个数的数值都在 [1,1e4]之间。
我们最多可以做k次操作。每次操作把一个数的数值减少1,并加到另外一个数上。
但是要有一个原则需要保证,就是——
一个数的数值如果减过,之后就一定不能再加了。
我们希望在若干次操作后,使得权值最大的数与权值最小的数的差值尽可能小,并输出这个差值。
【类型】
贪心。
【分析】
这题我们只要不断做贪心就好啦。
就是把当前最大的数,减少1,移动到当前最小的数上。
首先要保证次数不超过k。
而同时,为了保证不破打题目给定的原则。
我们发现,只有在最大数-最小数的差值>=2时才做操作。
于是这道题就做完啦!
【时间复杂度&&优化】
O(nk)
*/
【Codeforces Round 274 (Div 2)B】【贪心】Towers 若干次移数后使得最大差值尽可能小
最新推荐文章于 2024-08-05 21:04:36 发布