Educational Codeforces Round 22 A. The Contest
来源:互联网 发布:黑帽seo如何赚钱 编辑:程序博客网 时间:2024/05/17 06:39
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx ≤ T ≤ rx.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
The first line contains one integer n (1 ≤ n ≤ 1000) — the number of problems. The second line contains n integers ai (1 ≤ ai ≤ 105)— the time Pasha needs to solve ith problem.
The third line contains one integer m (0 ≤ m ≤ 1000) — the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 ≤ lj < rj ≤ 105) — the starting and the ending moment of jth period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets).
23 421 47 9
7
1511 4
-1
1511 5
5
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period.
这题其实就是把题一起做完了 然后一起交就好
看注释吧
#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>typedef long long ll;using namespace std;ll l[1005]; //这题不用ll也可以 只是习惯ll r[1005];int main(){ ll i,j,k,m,n; scanf("%lld",&n); ll sum=0; for(i=0;i<n;i++) { scanf("%lld",&k); sum+=k; } scanf("%lld",&m); if(m==0) //没有时间段可以交题 { printf("-1"); return 0; } for(i=0;i<m;i++) { scanf("%lld%lld",&l[i],&r[i]); } if(sum<l[0]) //之前就是在这里WA on test 11了 要考虑在web第一次开放之前就已经做完题的情况 { printf("%lld\n",l[0]); return 0; } for(i=0;i<m;i++) { if(sum>=l[i]&&sum<=r[i]) //在web开放时间段恰好做完 { printf("%lld\n",sum); return 0; } if(i+1<m&&sum>r[i]&&sum<l[i+1]) //在两个开放时间段之前做完 { printf("%lld\n",l[i+1]); return 0; } } printf("-1"); return 0;}
- Educational Codeforces Round 22 A. The Contest
- Educational Codeforces Round 4 A. The Text Splitting
- Educational Codeforces Round 5 A
- Educational Codeforces Round 20 A
- Educational Codeforces Round 21 A
- Educational Codeforces Round 23 A
- Educational Codeforces Round 22
- Codeforces Educational Codeforces Round 22
- codeforces Educational Codeforces Round 21 A
- Educational Codeforces Round 22 B. The Golden Age(枚举)
- Educational Codeforces Round 22 C. The Tag Game dfs
- Educational Codeforces Round 22-C. The Tag Game-搜索,贪心
- Educational Codeforces Round 22 C. The Tag Game(思维)
- Educational Codeforces Round 22 C. The Tag Game 搜索
- codeforce : Educational Codeforces Round 22
- Educational Codeforces Round 22 题解
- Educational Codeforces Round 5 C. The Labyrinth
- Educational Codeforces Round 5 C. The Labyrinth
- Python网络编程--Echo服务
- RxJava2+Retrofit2精讲
- JavaScript实时校验用户输入
- 后缀数组 poj. 1743. Musial Theme
- Jmeter 跨线程组传递参数
- Educational Codeforces Round 22 A. The Contest
- JavaScript
- Zookeeper可以干什么
- c++迭代器失效问题
- Matlab中设置运行时间
- leetcode520. Detect Capital
- 数据持久化
- Java学习笔记---增强for循环
- 蓝桥-ALGO-33-数列