CF1799B Equalize by Divide(模拟)

Problem - B - Codeforces

给定那个数,可以使用操作选择i,j两个位置使得a[i] = (a[i] / a[j])(上取整),求出操作数,和每次操作的位置
#include <bits/stdc++.h>
#include <iostream>
#include<unordered_map>
#include<unordered_set>
#define x first
#define y second
#define  ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

using namespace std;

typedef  long  long LL ;
typedef  unsigned long  long ULL ;
typedef pair<int,int> PII ;
typedef pair<LL,int> PLI ;
const int N = 2e5+  10 ;
const LL INF2 = 1e17;

int n,m,q;



inline void solve()
{
    cin >> n;
    vector<int> a(n);
    int cnt1 =0 ;
    for(int &x:a) {
        cin >> x; cnt1 += (x == 1 );
    }

    if(a == vector<int>(n,a[0]))//每个数都一样,不用操作
    {
        cout << 0 << "\n";
        return;
    }

    if(cnt1){cout << -1 << "\n";return;} //有1个1其他数不完全是1,其他数无论怎么操作结果都是大于 1

    vector<PII> ans;

    while(a != vector<int>(n,a[0]))
    {
        int x=0,y=1;
        while(a[y] == a[x]) y ++;

        if(a[x] < a[y]) swap(x,y);
        a[x] = (a[x] + a[y] -1 ) / a[y];
        ans.push_back({x+1,y+1});
    }
    cout<<ans.size() << "\n";
    for(PII t :ans) cout<<t.x << " " << t.y << '\n';

}
signed main()
{
    ios

    int T=1;
    cin>>T;
    while(T -- )
    {
        solve();

    }


    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值