HDU4310_acm_Hero

Problem Description
When playing DotA with god-like rivals and pig-like team members, you have to face an embarrassing situation: All your teammates are killed, and you have to fight 1vN.

There are two key attributes for the heroes in the game, health point (HP) and damage per shot (DPS). Your hero has almost infinite HP, but only 1 DPS.

To simplify the problem, we assume the game is turn-based, but not real-time. In each round, you can choose one enemy hero to attack, and his HP will decrease by 1. While at the same time, all the lived enemy heroes will attack you, and your HP will decrease by the sum of their DPS. If one hero's HP fall equal to (or below) zero, he will die after this round, and cannot attack you in the following rounds.

Although your hero is undefeated, you want to choose best strategy to kill all the enemy heroes with minimum HP loss.
 

Input
The first line of each test case contains the number of enemy heroes N (1 <= N <= 20). Then N lines followed, each contains two integers DPSi and HPi, which are the DPS and HP for each hero. (1 <= DPSi, HPi <= 1000)
 

Output
Output one line for each test, indicates the minimum HP loss.
 

Sample Input
1 10 2 2 100 1 1 100
 

Sample Output
20 201

本題提供兩種語言的解決方案,首先參考網上的C代碼寫出了註釋版的C代碼,再把它轉化爲了Java代碼。

在轉化中發現C與Java的區別還是挺大的,但是既然爲一個算法題,能用C解決的就一定也能用Java,只不過是實現的方法不盡相同罷了,原理還是共通的。

C中使用結構體的方法定義了Hero的數據類型,而Java中是使用類Class的方法定義Hero;

C中排序的方法是用類庫中的sort()函數,而Java則需要實現Comparator接口了,通過Arrays中的sort()方法對對象排序;

在排序函數/方法的編寫中,兩者的區別在於C中使用指針來傳參數,Java通過中間變量Object 傳遞,返回的類型與計算方法一樣。


本題的方法: 先對Hero對象數組進行排序,按照優先攻擊的順序排序。 排序方法是 DPSi / HPi 的大小,越大的攻擊優先級越高。

對結構體或者Java中的對象進行排序,可以使用類庫中帶的排序方法。對於C是sort ,對於Java則是調用Arrays中的sort。

其中排序的規則,C是自己寫一個函數作爲sort的參數即可,Java需要實現comparator接口,再將其作爲參數傳遞到sort即可。

排序完成三個for循環,第一個循環  從0-N 把所有存活的Hero循環一遍

第二個循環  把所有HP>0的Hero.HPi 減 1 循環

第三個循環  每次把剩下的Hero(即第一個循環的 i 變量)

最後把每次造成的傷害累加到sum中

		//快速排序 四個參數
		//1.待排序數組首地址
		//2.數組中待排序元素數量
		//3.各元素佔用空間大小
		//4.指向函數的指針,用於確定排序的順序
		//對結構體進行排序

#include<stdio.h>
#include<stdlib.h>
struct In{
	double bi;
	int dps;
	int hp;
}hero[100];
int cmp(const void *a,const void *b){
	//比較函數
	struct In *c = (In*)a;
	struct In *d = (In*)b;
	/*
	(a->dps / a->hp) - (b->dps / b->hp) 
	a->dps * b->hp - b->dps * a->hp
	-------------------------------- 
	a->hp * b->hp
	
	計算機除法會出現數據不準確,不可預知的錯誤
	所以改成乘法可以正確運行
	*/
	return (d->dps * c->hp - c->dps * d->hp);
}

int main(){
	int n,i,j,k;
	int sum;
	while (scanf("%d",&n) != EOF ){ //要輸入的數據條數
		for(i=0; i<n; i++){
			scanf("%d %d",&hero[i].dps,&hero[i].hp);
			//轉換成浮點數
			//hero[i].bi = hero[i].dps * 1.000 / hero[i].hp * 1.000;
		}
		qsort(hero, n, sizeof(hero[0]),cmp); 
		sum = 0;
		for(i=0; i<n; i++){//輸入的n個數據
			for(j=hero[i].hp; j>0; j--){//每個數據的 生命值減一枚舉
				for(k=i; k<n; k++){//i--n 所有存活的傷害累加
					sum = sum + hero[k].dps;
				}//for3
			}//for2
		}//for1
		//輸出傷害
		printf("%d\n",sum);
	
	}//end while
	return 0;
}//end main
JAVA代碼
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
    
    public static void main(String[] args) {
    	//輸入
    	Scanner sc = new Scanner(System.in);
   		int N = sc.nextInt();
    	Hero[] hero = new Hero[N];
    	ArrayList arrayList = new ArrayList();
    	for(int i=0; i<N; i++){
    		hero[i] = new Hero();
    		hero[i].DPSi = sc.nextInt(); 
    		hero[i].HPi  = sc.nextInt();	
    		arrayList.add(hero[i]);
    	}
    	
    	//排序
    	Arrays.sort(hero,0,N-1,new Com_hero());
    	
    	//計算輸出
    	int DSP_Count = 0;
    	for(int i=0; i<N; i++){
    		//
    		for( ; hero[i].HPi>0; hero[i].HPi --){
    			//
    			for(int k=i; k<N; k++){
    				DSP_Count += hero[k].DPSi;
    			}
    		}
    	}
    	System.out.println(DSP_Count);
    	
    }
}
//對象聲明
class Hero{
	int HPi;
	int DPSi;
	Hero(){}
}
class Com_hero implements Comparator{
	public int compare(Object com1,Object com2){
		Hero h1 = (Hero)com1;
		Hero h2 = (Hero)com2;
		if(h1.DPSi * h2.HPi < h2.DPSi * h1.HPi )
			return 1;
		else return -1;		
	}
}




#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct A
{
	int t,v;
}E[30];
int cmp(const void *a,const void *b)
{
	A *c,*d;
	c=(A *)a;
	d=(A *)b;
	return (c->t*d->v)-(d->t*c->v);
}

int main()
{
	int n;
	int i,l;
	int base,ans;
	while(scanf("%d",&n)!=-1)
	{
		base=0;
		for(i=0;i<n;i++)	{scanf("%d%d",&E[i].v,&E[i].t);base+=E[i].v;}
		qsort(E,n,sizeof(E[0]),cmp);

		ans=0;
		for(i=0;i<n;i++)
		{
			ans+=base*E[i].t;
			base-=E[i].v;
		}
		printf("%d\n",ans);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章