特判方程迭代

来源:互联网 发布:python 幂函数 编辑:程序博客网 时间:2024/05/10 02:57
#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);                        }                    }                }            }        }    }}
0 0