特判方程迭代

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = 100000 + 7;
int D[maxn];
int B[maxn];
int OP[3] = { 0,1,-1 };
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        long long sum = 0;
        for (int i = 0; i < n; i++)
        {
            scanf("%d", D + i);
            sum += D[i];
        }
        if (sum%n)
        {
            cout << "NO" << endl;
        }
        else
        {
            if (n == 1)
            {
                cout << "YES" << endl;
            }
            else if (n == 2)
            {
                if (abs(D[0] - D[1]) == 2|| abs(D[0] - D[1])==0)
                {
                    cout << "YES" << endl;
                    if (D[0] == D[1])
                    {
                        cout << 0 << endl;
                    }
                    else
                    {
                        cout << 1 << endl;
                    }
                    if (D[0] > D[1])
                    {
                        cout << 1 << " " << 2 << endl;
                    }
                    else if (D[0] < D[1])
                    {
                        cout << 2 << " " << 1 << endl;
                    }
                }
                else
                {
                    cout << "NO" << endl;
                }
            }
            else
            {
                long long ave = sum / n;
                for (int i = 0; i < n; i++)
                {
                    D[i] -= ave;
                }
                int i;
                for (i = 0; i < 3; i++)
                {
                    B[n - 1] = OP[i];
                    int j;
                    B[0] = D[0] + B[n - 1];
                    if (B[0] < -1 || B[0]>1)
                        continue;
                    for (j = 1; j < n - 1; j++)
                    {
                        B[j] = D[j] + B[j - 1];
                        if (B[j] < -1 || B[j]>1)
                        {
                            break;
                        }
                    }
                    if (j == n - 1 && B[n - 1] == D[n - 1] + B[n - 2])
                    {
                        break;
                    }
                }
                if (i == 3)
                {
                    cout << "NO" << endl;
                }
                else
                {
                    cout << "YES" << endl;
                    int cnt = 0;
                    for (int k = 0; k < n; k++)
                    {
                        if (B[k]!=0)
                        {
                            cnt++;
                        }
                    }
                    cout << cnt << endl;
                    for (int k = 0; k < n; k++)
                    {
                        int p = k + 1, q = k + 2;
                        if (q == n + 1)
                            q = 1;
                        if (B[k] > 0)
                        {
                            printf("%d %d\n", p, q);
                        }
                        else if(B[k]<0)
                        {
                            printf("%d %d\n",q,p);
                        }
                    }
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值