罪犯转移
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个人。
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; }
相关文章推荐
- iOS之远程通知使用介绍
- hdu 3006(状态压缩)
- Groovy入门(三)——集合类型List
- Android中LoaderManager:AsyncTaskLoader和CursorLoader的使用
- SEO分析:都是流量惹的祸
- Intent的带对象传值
- 单例模式
- Groovy入门(二)——集合类型-Range
- 【NOIP2015模拟11.3】备用钥匙
- WEB网站常见的受攻击方式及预防手段
- LoadTypeLibEx
- spoj 16891 Character Patterns (Act 8)
- java内存区域与内存溢出异常
- 块级元素和内联元素区别
- android studio 签名并打包
- unity性能分析
- Messenger的基本用法(本质是跨APP的绑定服务)
- RC4加密算法的原理及实现
- Android布局之FrameLayout
- #ifndef 与 #program once 的区别