2016ACM暑假集训 - Sacrament of the sum
来源:互联网 发布:mac系统画流程图 编辑:程序博客网 时间:2024/06/04 06:03
Description
— The Brother of mine, the Head of Monastic Order wants to know tomorrow about the results long-term researches. He wants to see neither more nor less than the Summering Machine! Even moreover, he wants our Machine — only a machine — to demonstrate its comprehension of the Sacrament of the Sum as deeply as it is possible. He wants our Machine to find two numbers that give the sum equal to the Sacred Number 10 000.
— Tsh-sh-sh! This is madness that borders on blasphemy! How can the Machine calculate the Sacred Number? Twenty seven years we work on it, but we've could teach it to tell if the sum of two introduced numbers greater or lower than 10 000. Can an ordinary mortal find two numbers that there sum will be equal to 10 000?
— But we'll have to do it with the help of our Machine, even if it is not capable. Otherwise we'll have... let's say, big problems, if it is possible to call boiling oil like this. However, I have an idea. Do you remember, last week we've entered two numbers -7 and 13 into the Machine, and it answered that their sum is lower than 10 000. I don't know how to check this, but nothing's left for us than to believe to the fruit of our work. Let's enter now a greater number than -7 and start up the Machine again. We'll do like this again and again until we find a number that being added to 13 will give us 10 000. The only thing we are to do is to prepare an ascending list of numbers.
— I don't believe in this... Let's start with the sum that is obviously greater than the Sacred Number and we'll decrease one of the summand. So we have more chances to avoid boilin... big problems.
Haven't come to an agreement, the Brothers went away to their cells. By next day everyone of them has prepared a list of numbers that, to his opinion, could save them... Can both of the lists save them together?
Your program should decide, if it is possible to choose from two lists of integers such two numbers that their sum would be equal to 10 000.
Input
You are given both of these lists one by one. Format of each of these lists is as follows: in the first line of the list the quantity of numbers Ni of the i-th list is written. Further there is an i-th list of numbers each number in its line (Ni lines).The following conditions are satisfied: 1 <= Ni <= 50 000, each element of the lists lays in the range from -32768 to 32767. The first list is ascending and the second one is descending.
Output
You should write "YES" to the standard output if it is possible to choose from the two lists of integers such two numbers that their sum would be equal to 10 000. Otherwise you should write "NO".
Sample Input
4-17519191042438951-424-788
YES
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
解题思路:
由于两个序列一个递增一个递减,因此可以通过设定两个标签值进行调整,当两者相加大于10000时,递减数列加一,小于10000时递增数列加一。
代码:
#include <cstdio>#include <algorithm>using namespace std;int a[40005];int b[40005];int main(){int n1, n2;int k1, k2;while(scanf("%d", &n1) != EOF){for(int i = 0; i < n1; i++){scanf("%d", &a[i]);}scanf("%d", &n2);for(int i = 0; i < n2; i++){scanf("%d", &b[i]);}k1 = 0;k2 = 0;bool t = false;while(k1 < n1 && k2 < n2){if(a[k1] + b[k2] == 10000){t = true;break;}else if(a[k1] + b[k2] > 10000){k2++;} else if(a[k1] + b[k2] < 10000){k1++;}}if(t){printf("YES\n");}else{printf("NO\n");}}return 0;}
- 2016ACM暑假集训 - Sacrament of the sum
- Sacrament of the sum
- acm解题报告 POJ 2366 Sacrament of the sum
- 1021. Sacrament of the Sum
- C - Sacrament of the sum
- poj 2366 Sacrament of the sum
- POJ - 2366 Sacrament of the sum
- poj 2366 Sacrament of the sum
- POJ 2366 Sacrament of the sum
- POJ 2366 Sacrament of the sum
- POJ 2366 Sacrament of the sum
- day2 POJ 2366 Sacrament of the sum
- poj 2366 Sacrament of the sum
- poj 2366 Sacrament of the sum
- 【二分查找】[POJ2366]Sacrament of the sum
- 2016ACM暑假集训 - 4 Values whose Sum is 0
- 【模拟】Sacrament of Sum
- 【Hash】Sacrament of Sum
- jvm参数解析
- Retrofit解析网页Json数据简单实例
- codeforces 632C
- 关于Fragment重叠问题分析和解决
- Fragment的声明周期整理
- 2016ACM暑假集训 - Sacrament of the sum
- 【ReactNative】真机上无法调试 could not connect to development server
- 进程间通信(IPC)(1)—— 管道、共享内存
- L2-003. 月饼-PAT团体程序设计天梯赛GPLT
- NYOJ118 修路方案 次小生成树
- LeetCode-JAVA-Valid Parentheses
- LeetCode-83:Remove Duplicates from Sorted List
- Spring事务管理(5)-开启事务
- POJ 3150 Cellular Automaton