DIFFMED - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: Utkarsh Gupta
Testers: Abhinav Sharma, Venkata Nikhil Medam
Editorialist: Nishank Suresh

DIFFICULTY:

1408

PREREQUISITES:

Observation

PROBLEM:

Given N, construct a permutation of \{1, 2, 3, \ldots, N\} such that no two adjacent prefixes have the same median.

Note that the median of a set of size 2K is its K-th element in sorted order.

EXPLANATION:

We can make the following observations:

  • Suppose A is a set of odd size and median M. If we add another element, say x, to A:
    • If x \geq M, the median of A\cup \{x\} still remains M
    • If x \lt M, the median of A\cup \{x\} is not M
  • Suppose A is a set of even size and median M. If we add x to A:
    • If x \leq M, the median of the new set is still M
    • If x \gt M, the median of the new set is not M

This tells us the following, if we have fixed the first i elements of the permutation:

  • If i is odd, the i+1-th element must be smaller than the current median
  • If i is even, the i+1-th element must be larger than the current median

One construction that satisfies the above properties is as follows:

Let K = \left\lfloor \frac{N}{2} \right\rfloor. Then,

  • Place K+1, K+2, \ldots, N in the odd positions
  • Place K, K-1, \ldots, 1 in the even positions

For example, if N = 11 (and so K = 5), the permutation looks like [6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11].

TIME COMPLEXITY:

\mathcal{O}(N) per test case.

CODE:

Setter (C++)
//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
long long readInt(long long l,long long r,char endd){
    long long x=0;
    int cnt=0;
    int fi=-1;
    bool is_neg=false;
    while(true){
        char g=getchar();
        if(g=='-'){
            assert(fi==-1);
            is_neg=true;
            continue;
        }
        if('0'<=g && g<='9'){
            x*=10;
            x+=g-'0';
            if(cnt==0){
                fi=g-'0';
            }
            cnt++;
            assert(fi!=0 || cnt==1);
            assert(fi!=0 || is_neg==false);

            assert(!(cnt>19 || ( cnt==19 && fi>1) ));
        } else if(g==endd){
            if(is_neg){
                x= -x;
            }

            if(!(l <= x && x <= r))
            {
                cerr << l << ' ' << r << ' ' << x << '\n';
                assert(1 == 0);
            }

            return x;
        } else {
            assert(false);
        }
    }
}
string readString(int l,int r,char endd){
    string ret="";
    int cnt=0;
    while(true){
        char g=getchar();
        assert(g!=-1);
        if(g==endd){
            break;
        }
        cnt++;
        ret+=g;
    }
    assert(l<=cnt && cnt<=r);
    return ret;
}
long long readIntSp(long long l,long long r){
    return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
    return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
    return readString(l,r,'\n');
}
string readStringSp(int l,int r){
    return readString(l,r,' ');
}
int sumN=0;
void solve()
{
    int N=readInt(2,1000,'\n');
    sumN+=N;
    assert(sumN<=1000);
    int low=1,high=N;
    for(int i=1;i<=N;i++)
    {
        if(i%2==1)
            cout<<(high--)<<' ';
        else
            cout<<(low++)<<' ';
    }
    cout<<'\n';
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int T=readInt(1,30,'\n');
    while(T--)
        solve();
    assert(getchar()==-1);
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
Tester (nikhil_medam, C++)
// Tester: Nikhil_Medam
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"

int t, n;
int32_t main() {
    cin >> t;
    while(t--) {
        cin >> n;
        int small = 1, large = n;
        for(int i = 1; i <= n; i++) {
            if(i & 1) {
                cout << large-- << " ";
            }
            else {
                cout << small++ << " ";
            }
        }
        cout << endl;
    }
	return 0;
}
Editorialist (Python)
for _ in range(int(input())):
    n = int(input())
    a = []
    L, R = 1, 2
    for i in range(n):
        if i%2 == 1:
            # smaller
            a.append(L)
            L -= 1
        else:
            # Larger
            a.append(R)
            R += 1
    m = min(a)
    print(' '.join(str(x-m+1) for x in a))
2 Likes