一开始想用优先队列,但维护最大堆最小堆太麻烦,转而想到用一个set来做,
该set的begin()就是当前最低的塔
该set的rbegin()就是当前最高的塔
用erase对选中的元素出队,ran'd
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
struct node{
int pos,val;
node(int pos,int val):pos(pos),val(val){}
bool operator < (const node& b) const{
return val<b.val||(val==b.val&&pos<b.pos);
}
};
int main(){
int n,k;
scanf("%d %d",&n,&k);
int temp;
set<node> st;
for(int i=0;i<n;i++)
{
scanf("%d",&temp);
st.insert(node(i,temp));
}
int i;
queue<pair<int,int>> ans;
for(i=0;i<k;i++){
auto it1 = st.begin();
auto it2 = st.rbegin();
if(it1->val==it2->val){
break;
}
ans.push(make_pair(it2->pos+1,it1->pos+1));
st.insert({it1->pos,it1->val+1});
st.insert({it2->pos,it2->val-1});
st.erase(*it1);
st.erase(*it2);
}
int s = st.rbegin()->val-st.begin()->val;
cout<<s<<" "<<i<<endl;
while(!ans.empty()){
auto t = ans.front();
ans.pop();
cout<<t.first<<" "<<t.second<<endl;
}
return 0;
}
京公网安备 11010502036488号