您的位置:首页 > 其它

罪犯转移

2016-04-18 20:42 274 查看
题目描述

C市现在要转移一批罪犯到D市,C市有n名罪犯,按照入狱时间有顺序,另外每个罪犯有一个罪行值,值越大罪越重。现在为了方便管理,市长决定转移入狱时间连续的c名犯人,同时要求转移犯人的罪行值之和不超过t,问有多少种选择的方式?

输入描述:

第一行数据三个整数:n,t,c(1≤n≤2e5,0≤t≤1e9,1≤c≤n),第二行按入狱时间给出每个犯人的罪行值ai(0≤ai≤1e9)

输出描述:

一行输出答案。

输入例子:

3 100 2

1 2 3

输出例子:

2

解题思路

累加到c个罪犯的值,判断是否小于t,是则方法加1,否则删掉已经加的第一个,再加新的,因为是要连续的c个人。

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan  = new Scanner(System.in);
while(scan.hasNextLine()){
String in[] =  scan.nextLine().split("\\s");
String criminalString[] = scan.nextLine().split("\\s");

System.out.println(getNumMoves(criminalString,Integer.parseInt(in[0]),Integer.parseInt(in[1]),Integer.parseInt(in[2])));
}
}
public static int getNumMoves(String criminal[],int n, int t, int c){
int sum= 0;
int i,j;
int tmp = 0,num = 0;
Queue<Integer> q = new LinkedList<Integer>();
for(i = 0; i < n; i++){
if (q.size() < c) {
tmp = Integer.parseInt(criminal[i]);
num += tmp;
q.add(tmp);
}else{
if (num <= t) {
sum++;
}
num -= q.poll();
tmp = Integer.parseInt(criminal[i]);
num += tmp;
q.add(tmp);
}

}
if (q.size() == c && num <= t) {
sum++;
}

return sum;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: