Educational Codeforces Round 96 (Rated for Div. 2) C. Numbers on Whiteboard (greedy algorithm (water problem))

Topic link: Post the portal
topic:
Insert picture description here

Meaning of the question:, give you 1-n numbers, you can combine the numbers in two different positions, such as a and b, the composite number becomes (a+b)/2, and the result is rounded. Put it at the end of the array and delete the original a and b. For example,
Insert picture description herejust merge them two by two. After the final synthesis, there must be one number left in the array. Ask how you can merge to make the last number the smallest, output the smallest number, and output every Two numbers combined

Idea: Every time from the end, merge in pairs, which is definitely the smallest, and the minimum value of this merge is the only answer (I don’t know why, just write a few examples and try it manually) and the minimum number is fixed at 2.
Insert picture description here

#include<bits/stdc++.h>
namespace mySpace{
    
    
    typedef long long int ll;
    typedef long double ld;
    typedef double db;
    //typedef __int64 int bi;
    //nth_element(first,pos,end) =>STL function ,求第pos小的元素
    #define fori(a,b,c) for(int a=b;a<=c;++a)
    #define me(a) memset(a,0,sizeof a)
    #define Mod 1000000009
    #define exp 1e-8
    #define fi first
    #define se second
    #define sc_int(x) scanf("%d",&x)
    #define sc_db(x) scanf("%lf",&x)
    #define sc_str(x) scanf("%s",x)
    #define sc_bi(x) scanf("%I64d",&x)
    #define pr_int(x) printf("%d\n",x)
    #define pr_bi(x) printf("%lld\n",x)
    const int INF = 0x7fffffff;
    const int MAX1 = 2e5+10;
    const int MAX2 = 1e6+10;
    const int MAX3 = 2e6+10;
    //#define IS std::ios::sync_with_stdio(false)
    //#define OS std::cin.tie(NULL)
    void readFoler()
    {
    
    
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    }
    void closeFoler()
    {
    
    
        fclose(stdin);
        fclose(stdout);
    }
}
using namespace std;
using namespace mySpace;
ll a[MAX1];
int main()
{
    
    
	int t,n;
	scanf("%d",&t);
	while(t--)
    {
    
    
        scanf("%d",&n);
        puts("2");
        printf("%d %d\n",n,n-1);
        for(int i=n;i>=3;--i)
            printf("%d %d\n",i,i-2);
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/YSJ367635984/article/details/109058354