【codechef】Kisses(dp,技巧题)

47 篇文章 0 订阅

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;     
}   


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值