1. 程式人生 > >HDU - 6098:Inversion(暴力均攤)

HDU - 6098:Inversion(暴力均攤)

Give an array A, the index starts from 1.
Now we want to know =max ij  Bi=maxi∤jAj , ii≥2 .

InputThe first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integer n : the size of array A.
Next one line contains n integers, separated by space, ith number is

Ai">i  Ai .

Limits
T20 T≤20
2n100000 2≤n≤100000
1Ai1000000000 1≤Ai≤1000000000
n700000 ∑n≤700000
OutputFor each test case output one line contains n-1 integers, separated by space, ith number is
">i+1  Bi+1 .Sample Input

2
4
1 2 3 4
4
1 4 2 3

Sample Output

3 4 3
2 4 4

題意:對於所有的i(i!=1),找最大的a[j],滿足j%i!=0;

思路:沒有什麼對應的演算法,居然是暴力,我們從大到小排序,然後找到第一個滿足題意的即可。

複雜度均攤下來是線性的,顯然過得去。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
struct in{ int id,num; friend bool operator <(in w,in v) {return w.num>v.num;} }s[100010]; int main() { int T,N; scanf("%d",&T); while(T--){ scanf("%d",&N); rep(i,1,N) scanf("%d",&s[i].num),s[i].id=i; sort(s+1,s+N+1); rep(i,2,N) { rep(j,1,N){ if(s[j].id%i!=0){ printf("%d ",s[j].num); break; } } } puts(""); } return 0; }