题意有三个条件
1.每个人交换的时候只能交换自己独有的颜色
2.每个人不能接受自己已经有的颜色或者自己的颜色即使当前手中已经没有自己的颜色了
3.每个人每次交换的时候就互换一下各自的卡片
优先队列+bfs 类似于一个模拟的过程,这里比较巧妙的用到了make_pair()函数,相当于一个first second的结构体
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define N 222222
#define ll __int64
#define INF 0x3f3f3f
using namespace std;
typedef pair<int,int>pp;
pp ans[N];
int a,b;
int x;
int n,s;
int main()
{
cin>>n>>s;
priority_queue<pp>q;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
q.push(make_pair(x,i));
}
// for(int i=1;i<=n;i++)//先按first从大到小排序,再按second从大到小排序
// {
// pp a;
// a=q.top();
// q.pop();
// cout<<a.first<<" "<<a.second<<endl;
// }
int num=0;
while(!q.empty())
{
pp p=q.top();
q.pop();
priority_queue<pp>t;
while(p.first!=0)
{
if(q.empty())
{
puts("No");
return 0;
}
pp v=q.top();
q.pop();
ans[num++]=make_pair(p.second,v.second);
p.first--;
v.first--;
if(v.first)
t.push(v);
}
while(!t.empty())
{
q.push(t.top());
t.pop();
}
}
puts("Yes");
cout<<num<<endl;
for(int i=0;i<num;i++)
{
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
return 0;
}