Cow code

package Practice;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;


public class Practice {


public int[] Cow;
public int N;
public int M;
public int C;
public List<Net> NetList = new ArrayList();
public int Result = 0;

public void Init(Scanner sc)
{
N = sc.nextInt();
M = sc.nextInt();
C = sc.nextInt();
Cow = new int[C];
for(int i = 0; i < C; i++)
{
Cow[i] = sc.nextInt();
if(i >= 1)
{
int t = Cow[i] - Cow[i-1];
Net n = new Net(Cow[i-1], t);
NetList.add(n);
}
}
}

public void Compute()
{
ComparatorNet cn = new ComparatorNet();
Collections.sort(NetList, cn);
List<Net> NetListSub = new ArrayList();
NetListSub = NetList.subList(0, N-1);
//System.out.println(NetListSub.size());
ComparatorNetPn cnp = new ComparatorNetPn();
Collections.sort(NetListSub, cnp);
int pre = Cow[0];
for(int i = 0; i < N-1; i++)
{
Result = Result + ((Net)NetListSub.get(i)).pn.intValue() - pre + 1;
for(int j = 0; j < C; j++)
{
if(Cow[j] > ((Net)NetListSub.get(i)).pn.intValue())
{
pre = Cow[j];
break;
}
}
}
Result = Result + Cow[C-1] - pre + 1;
}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Practice pr = new Practice();
pr.Init(sc);
pr.Compute();
System.out.println(pr.Result);
}

public class ComparatorNet implements Comparator
{
public int compare(Object arg0, Object arg1)
{
Net net0 = (Net)arg0;
Net net1 = (Net)arg1;
int flag = net1.interval.compareTo(net0.interval);
return flag;
}
}

public class ComparatorNetPn implements Comparator
{
public int compare(Object arg0, Object arg1)
{
Net net0 = (Net)arg0;
Net net1 = (Net)arg1;
int flag = net0.pn.compareTo(net1.pn);
return flag;
}
}

public class Net
{
private Integer pn;
private Integer interval;
private int used;
public Net(int pn, int interval)
{
this.pn = pn;
this.interval = interval;
used = 0;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章