题目链接:https://cn.vjudge.net/problem/CodeForces-892A

Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai  ≤  bi).

Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!

Input

The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola cans.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — volume of remaining cola in cans.

The third line contains n space-separated integers that b1, b2, ..., bn (ai ≤ bi ≤ 109) — capacities of the cans.

Output

Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes).

You can print each letter in any case (upper or lower).

Example

Input
2
3 5
3 6
Output
YES
Input
3
6 8 9
6 10 12
Output
NO
Input
5
0 0 5 0 0
1 1 8 10 5
Output
YES
Input
4
4 1 0 3
5 2 2 3
Output
YES

贼JR的水,根本不需要题解……

只是想贴一个记录一下O(n)得到数组内最大和次大的for循环代码,免得以后什么时候脑抽忘记了下不出来僵掉了……

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n;
int main()
{
scanf("%d",&n);
LL sum=;
for(int i=,tmp;i<=n;i++)
{
scanf("%d",&tmp);
sum+=tmp;
}
LL max1=-,max2=-;
for(int i=,tmp;i<=n;i++)
{
scanf("%d",&tmp);
if(max2<=max1 && max1<=tmp) max2=max1,max1=tmp;
else if(max2<tmp && tmp<=max1) max2=tmp;
}
if(max1+max2 >= sum) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}

————————————————————————————————————————————————————————————————

题目链接:https://cn.vjudge.net/problem/CodeForces-892B

Hands that shed innocent blood!

There are n guilty people in a line, the i-th of them holds a claw with length Li. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the i-th person kills the j-th person if and only if j < i and j ≥ i - Li.

You are given lengths of the claws. You need to find the total number of alive people after the bell rings.

Input

The first line contains one integer n (1 ≤ n ≤ 106) — the number of guilty people.

Second line contains n space-separated integers L1, L2, ..., Ln (0 ≤ Li ≤ 109), where Li is the length of the i-th person's claw.

Output

Print one integer — the total number of alive people after the bell rings.

Example

Input
4
0 1 0 10
Output
1
Input
2
0 0
Output
2
Input
10
1 1 3 0 0 0 2 1 0 3
Output
3

题意:

对于一排人,第i个人有长度为L[i]的爪子,他会杀死所有第j个人,其中j满足 i - L[i] <= j 且 j < i ;

求最后存活下来的有几个人;

题解:

一看n的范围,最大到1e6,看来只能用O(n)的做法;

那么,考虑从后往前遍历,对于第i个人会不会被杀掉:

我们用mini来维护所有 i+1 ~ n 的人会杀掉的最前面的人是哪个,也就是所有 i+1 ~ n 的人的 i - L[i] 最小值;

那么,如果i >= mini,那么第i个人会被干掉;

这样一来,就能O(n)的得到被杀的人数了;

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n,L[+];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&L[i]);
int mini=INF,cnt=;
for(int i=n;i>=;i--)
{
if(i>=mini) cnt++;
mini=min(mini,i-L[i]);
}
cout<<n-cnt<<endl;
}

————————————————————————————————————————————————————————————————

题目链接:https://cn.vjudge.net/problem/CodeForces-892C

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.

What is the minimum number of operations you need to make all of the elements equal to 1?

Input

The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.

The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.

Example

Input
5
2 2 3 4 6
Output
5
Input
4
2 4 6 8
Output
-1
Input
3
2 6 9
Output
4

Note

In the first sample you can turn all numbers to 1 using the following 5 moves:

  • [2, 2, 3, 4, 6].
  • [2, 1, 3, 4, 6]
  • [2, 1, 3, 1, 6]
  • [2, 1, 1, 1, 6]
  • [1, 1, 1, 1, 6]
  • [1, 1, 1, 1, 1]

We can prove that in this case it is not possible to make all numbers one using less than 5 moves.

题意:

题解:

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n;
vector<int> a;
int gcd(int a,int b){return b==?a:gcd(b,a%b);}
int main()
{
scanf("%d",&n);
int cnt_1=;
for(int i=,tmp;i<=n;i++)
{
scanf("%d",&tmp);
a.push_back(tmp);
if(tmp==) cnt_1++;
} if(cnt_1>)
{
cout<<n-cnt_1<<endl;
return ;
} if(n== && a[]!=)
{
cout<<"-1"<<endl;
return ;
} int cnt=,ok=;
vector<int> tmp;
while()
{
cnt++;
tmp.clear();
for(int i=;i<a.size()-;i++)
{
if(gcd(a[i],a[i+])==)
{
ok=;
break;
}
tmp.push_back(gcd(a[i],a[i+]));
} if(ok)
{
cout<<cnt+n-<<endl;
return ;
} if(tmp.size()== && tmp[]!=)
{
cout<<"-1"<<endl;
return ;
} a.clear();
for(int i=;i<tmp.size();i++) a.push_back(tmp[i]);
}
}

codeforces 892 - A/B/C的更多相关文章

  1. Codeforces 892 C.Pride

    C. Pride time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  2. Codeforces 892 D.Gluttony

    D. Gluttony time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. Codeforces 892 B.Wrath

    B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  4. Codeforces 892 A.Greed

    A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  5. codeforces #446 892A Greed 892B Wrath 892C Pride 891B Gluttony

    A  链接:http://codeforces.com/problemset/problem/892/A 签到 #include <iostream> #include <algor ...

  6. [CodeForces 892A] Greed (Java中sort实现从大到小排序)

    题目链接:http://codeforces.com/problemset/problem/892/A 具体的Java 中 sort实现降序排序:https://www.cnblogs.com/you ...

  7. Codeforces 892C/D

    C. Pride 传送门:http://codeforces.com/contest/892/problem/C 本题是一个关于序列的数学问题——最大公约数(GCD). 对于一个长度为n的序列A={a ...

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. ios7注意事项随笔

    1,修改状态栏的样式和隐藏. 首先,需要在Info.plist配置文件中,增加键:UIViewControllerBasedStatusBarAppearance,并设置为YES: 然后,在UIVie ...

  2. 解决Android 6.0(api 23) SDK,不再提供org.apache.http.*

    Eclipse 解决办法 libs中加入 org.apache.http.legacy.jar 上面的jar包在:**\android-sdk\platforms\android-23\optiona ...

  3. nyoj 1239 引水project (河南省第八届acm程序设计大赛)

    题目1239 pid=1239" style="color:rgb(55,119,188)">题目信息 pid=1239" style="col ...

  4. linux 网卡配置信息

    vi /etc/sysconfig/network-scripts/ifcfg-eth0

  5. MongoDB(三)-- 执行JS、界面工具

    一.执行Js脚本 1.开启mongod服务 2.连接mongodb客户端,./mongo --host 192.168.80.128 --port 27017 3.创建数据库:use testdb1 ...

  6. Github上star数超1000的Android列表控件

    Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...

  7. 在iOS中使用icon font

    博文转载至 http://www.cocoachina.com/industry/20131111/7327.html 在开发阿里数据iOS版客户端的时候,由于项目进度很紧,项目里的所有图标都是用最平 ...

  8. 《Lua程序设计》9.3 以协同程序实现迭代器 学习笔记

    例:编写一个迭代器,使其可以遍历某个数组的所有排列组合形式.代码如下: function permgen(a, n) n = n or #a -- 默认n为a的大小 then -- 还需要改变吗? p ...

  9. MyEclipse 10 下在线安装插件

    昨天不知道怎么就删除了电脑中的eclipse 我x,还原不回来了. 今天就安装了最新版本的myeclipse10,大家都知道,MyEclipse 中有一个烦人的 Software and Worksp ...

  10. /etc/fstab文件损坏的补救措施

    最近乱搞,把/etc/fstab弄坏了,导致无法进入图形界面,而且所有文件都是只读的(简直郁闷到底啊),查了好多资料什么的终于弄好了,也走了不少弯路 恩,我不喜欢扯太多东西,这个是补救的帖子,还是希望 ...