Mohit and his girlfriend are playing a game named 'Ascending sort'. The norms of the game are as follows :
Initialy N integers are taken randomly and are kept on the table. The one who makes these N numbers as acsending numbers wins the game. Both play turn by turn and in a single turn they can add or subtract 1 to any single integer present on the table and they are restricted to change the positions of integers during their turn.
Two numbers a[i], a[i+1] are said to be acending number in the given order if a[i] < a[i+1] or if they are equal.
Mohit wants to get a kiss from his girlfriend, so he plans to lose the game and make his girlfriend win. Mohit needs your help to know who should start first so that he gets a kiss. Help Mohit in acheiving his objective in minimum steps.
Input
- First line contains T the number of test cases.
- Next T lines contains N.
- Next line contains N integers (A[1],A[2],...,A[N]) which are kept on the table.
Output
- Print "MOHIT" or "GFRINED" depending upon who should start first.
Constraints
- 1 <= T <= 10
- 1 <= N <= 5000
- -1000000000 <= A[i] <= 1000000000
Example
Input: 2 2 -5 0 4 3 0 -5 3 Output: MOHIT MOHIT
Explanation
Case 1 : -5 and 0 are already as ascending numbers. Mohit gets a kiss if he starts first. Since, he is unable to make any move.
Case 2 :3 turns required to turn 3 to 0 and then 5 turns to turn -5 to 0 so that they become 0 0 0 3 and are as ascending numbers.So to do this minimum number of turn is 8.
http://www.codechef.com/CDVA2015/problems/CDVS1506/
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
ll dp[5001][5001];
ll x[5001];
ll y[5001];
int main(){
int t;
scanf("%d",&t);
while(t--){
ll n;
cin>>n;
for(int i=0;i<n;++i){
cin>>x[i];<span style="white-space:pre"> </span>
y[i]=x[i];
}
sort(y,y+n); //unique使用前必须先排序
int m=unique(y,y+n)-y;
for(int i=0;i<m;++i)
dp[0][i]=abs(x[0]-y[i]);
for(int i=1;i<n;++i){
ll min=1000000000000000000;
for(int j=0;j<m;++j){
if(dp[i-1][j]<min)//因为y[j]前面的都符合条件,所以可以这样写
min=dp[i-1][j];
dp[i][j]=abs(x[i]-y[j])+min;
}
}
ll min=100000000000000000;
for(int i=0;i<m;++i){
if(dp[n-1][i]<min)
min=dp[n-1][i];
}
if(min%2==1)
cout<<"GFRINED"<<endl;
else
cout<<"MOHIT"<<endl;
}
return 0;
}