【codeforce】A. Group of Students
2013-10-31 23:12
387 查看
原题:
A. Group of Students
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
At the beginning of the school year Berland State University starts two city school programming groups, for beginners and for intermediate coders. The children were tested in order to sort them into groups. According to the results, each student got some score
from 1 to m points. We know that c1 schoolchildren
got 1 point, c2 children
got 2 points, ..., cmchildren
got m points. Now you need to set the passing rate k (integer
from 1 to m): all schoolchildren who got less than k points
go to the beginner group and those who get at strictly least k points go to the intermediate group. We know that if the size of a group is more than y,
then the university won't find a room for them. We also know that if a group has less than x schoolchildren, then it is too small and there's no point in
having classes with it. So, you need to split all schoolchildren into two groups so that the size of each group was from x to y,
inclusive.
Help the university pick the passing rate in a way that meets these requirements.
Input
The first line contains integer m (2 ≤ m ≤ 100).
The second line contains m integers c1, c2,
..., cm, separated
by single spaces (0 ≤ ci ≤ 100).
The third line contains two space-separated integers x and y (1 ≤ x ≤ y ≤ 10000).
At least one ci is
greater than 0.
Output
If it is impossible to pick a passing rate in a way that makes the size of each resulting groups at least x and at most y,
print 0. Otherwise, print an integer from 1 to m — the passing rate you'd like to suggest. If there are multiple possible answers, print any of them.
Sample test(s)
input
output
input
output
input
output
Note
In the first sample the beginner group has 7 students, the intermediate group has 6 of them.
In the second sample another correct answer is 3.
分析:
简单的思维题,先求出总的分数sum和得分不大于ci的人数s
,复杂度为O(n),然后检查s[i]和sum-s[i]是否在[x,y]之内,是的话直接输出i+2,反之输出0,复杂度为O(n).
代码:
总结:看懂题目比较简单。
A. Group of Students
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
At the beginning of the school year Berland State University starts two city school programming groups, for beginners and for intermediate coders. The children were tested in order to sort them into groups. According to the results, each student got some score
from 1 to m points. We know that c1 schoolchildren
got 1 point, c2 children
got 2 points, ..., cmchildren
got m points. Now you need to set the passing rate k (integer
from 1 to m): all schoolchildren who got less than k points
go to the beginner group and those who get at strictly least k points go to the intermediate group. We know that if the size of a group is more than y,
then the university won't find a room for them. We also know that if a group has less than x schoolchildren, then it is too small and there's no point in
having classes with it. So, you need to split all schoolchildren into two groups so that the size of each group was from x to y,
inclusive.
Help the university pick the passing rate in a way that meets these requirements.
Input
The first line contains integer m (2 ≤ m ≤ 100).
The second line contains m integers c1, c2,
..., cm, separated
by single spaces (0 ≤ ci ≤ 100).
The third line contains two space-separated integers x and y (1 ≤ x ≤ y ≤ 10000).
At least one ci is
greater than 0.
Output
If it is impossible to pick a passing rate in a way that makes the size of each resulting groups at least x and at most y,
print 0. Otherwise, print an integer from 1 to m — the passing rate you'd like to suggest. If there are multiple possible answers, print any of them.
Sample test(s)
input
5 3 4 3 2 1 6 8
output
3
input
5 0 3 3 4 2 3 10
output
4
input
2 2 5 3 6
output
0
Note
In the first sample the beginner group has 7 students, the intermediate group has 6 of them.
In the second sample another correct answer is 3.
分析:
简单的思维题,先求出总的分数sum和得分不大于ci的人数s
,复杂度为O(n),然后检查s[i]和sum-s[i]是否在[x,y]之内,是的话直接输出i+2,反之输出0,复杂度为O(n).
代码:
#include <iostream> using namespace std; int Solution(int num[],int n,int x,int y); bool CheckSolution(int m,int sum,int x,int y); int main() { int n; cin >> n; int num ; for(int i=0 ; i<n ; i++){ cin >> num[i]; } int x,y; cin >> x >> y; cout << Solution(num,n,x,y) << endl; return 0; } int Solution(int num[],int n,int x,int y) { int ret = 0; int s ; int sum = num[0]; s[0] = num[0]; for(int i=1 ; i<n ; i++){ sum = sum + num[i]; s[i] = s[i-1] + num[i]; } for(int i=0 ; i<n ; i++){ if(true == CheckSolution(s[i],sum,x,y)){ ret = i+1+1; break; } } return ret; } bool CheckSolution(int m,int sum,int x,int y) { if(m>=x && m<=y && sum-m>=x && sum-m<=y){ return true; } else{ return false; } }
总结:看懂题目比较简单。
相关文章推荐
- 警惕C语言中自由的代价
- poj2689 Prime Distance 有难度 埃拉托斯尼斯筛法的运用
- 做个笔记 usb的批量传输备忘
- Dynamics Webservice Call with Credential
- java--IO
- 【面试系列】把一个字符串里除了大小写字母以外的所有东西都去掉,然后都变成小写,之后按顺序排序
- 【帧中继】EIGRP如何运行在帧中继网络
- LevelDB系列之SSTable(Sorted Strings Table)文件
- Ubuntu Install The Eclipse
- 推荐五星级C语言学习网站
- 分析Java类加载全过程
- angularJS之$watch、$digest和$apply方法
- 无刷新上传文件
- cocos2d-x精灵内存管理
- 设计模式--创建型-AbstractFactory(抽象工厂)
- Oracle笔记系列(二)
- UVA 10910 Marks Distribution
- 设计模式--创建型-FactoryMethod(工厂方法)
- 设计模式--创建型-Singleton(单例单件)
- c++虚函数