A. Greed
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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).

Examples
input
23 53 6

output
YES

input
36 8 96 10 12

output
NO

input
50 0 5 0 01 1 8 10 5

output
YES

input
44 1 0 35 2 2 3

output
YES

Note

In the first sample, there are already 2 cans, so the answer is "YES".

n个数据的和,然后在另一个n个数取两个大就行了,我不小心爆了LL

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int a[N];
int main()
{int n;scanf("%d",&n);long long s=0;for(int i=1;i<=n;i++){int x;scanf("%d",&x);s+=x;if(s>int(2e9)){printf("NO");return 0;}}for(int i=1;i<=n;i++)scanf("%d",&a[i]);sort(a+1,a+n+1);printf("%s",(s<=a[n]+a[n-1])?"YES":"NO");return 0;
}

B. Wrath
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
40 1 0 10

output
1

input
20 0

output
2

input
101 1 3 0 0 0 2 1 0 3

output
3

Note

In first sample the last person kills everyone in front of him.

直接暴力下取大

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
ll l[N],ma[N];
int main()
{ios::sync_with_stdio(false);cin.tie(0);ll n;cin>>n;memset(ma,0,sizeof(ma));for(ll i=1;i<=n;i++){cin>>l[i];ll x=max(1LL,i-l[i]);ma[x]=max(i-x,ma[x]);}ll ans=0,f=0;for(ll i=1;i<=n;i++){f=max(f,ma[i]);if(f==0)ans++;f--;}cout<<ans;return 0;
}

C. Pride
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacentelements 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.

Examples
input
52 2 3 4 6

output
5

input
42 4 6 8

output
-1

input
32 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.

这个有1的话就得直接输出啊,n-f

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=2005;
int a[N],b[N];
int main()
{ios::sync_with_stdio(false);cin.tie(0);int n;cin>>n;int f=0;for(int i=1; i<=n; i++){cin>>a[i];if(a[i]==1)f++;}if(f)printf("%d",n-f);else{f=1;for(;;){if(f>888*n){printf("-1");return 0;}for(int i=f+1; i<=n; i++){b[i]=__gcd(a[i-1],a[i]);if(b[i]==1){cout<<f+n-1;return 0;}}f++;for(int i=1; i<=n; i++)a[i]=b[i];}}return 0;
}

D. Gluttony
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 ≤ xi ≤ n, 0 < k < n) the sums of elements on that positions in aand b are different, i. e.

Input

The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.

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

Output

If there is no such array b, print -1.

Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.

If there are multiple answers, print any of them.

Examples
input
21 2

output
2 1 

input
41000 100 10 1

output
100 1 1000 10

Note

An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.

Note that the empty subset and the subset containing all indices are not counted.

这个P金爷讲思路,跟着做就行了

#include<bits/stdc++.h>
using namespace std;
const int N=200;
pair<int,int>a[N];
int ans[N];
int main()
{int n;scanf("%d",&n);for(int i=0; i<n; i++)scanf("%d",&a[i].first),a[i].second=i;sort(a,a+n);for(int i=0; i<n; i++){int to=a[i].second;if(i==0)ans[to]=a[n-1].first;elseans[to]=a[i-1].first;}for(int i=0; i<n; i++){printf("%d ",ans[i]);}return 0;
}

转载于:https://www.cnblogs.com/BobHuang/p/7858250.html

Codeforces Round #446 (Div. 2)相关推荐

  1. Codeforces Round #446 (Div. 1) B. Gluttony 构造 + 补集思想

    传送门 文章目录 题意: 思路: 题意: 给你一个数组aaa,保证aaa中每个数都互不相同,让你构造一个数组bbb,满足对于任意的S=x1,x2,...,xk,1≤xi≤n,0≤k<nS={x_ ...

  2. 【Codeforces Round #446 (Div. 2) C】Pride

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的 ...

  3. Lust(Codeforces Round #446 Div.1-891E)(母函数\生成函数)

    文章目录 题目 思路 代码 题目 你有n个数 a1,a2,...,ana_1,a_2,...,a_na1​,a2​,...,an​ ,要进行 kkk 次操作,每次在 111 ~ nnn 中随机选择一个 ...

  4. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  5. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  6. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  7. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  8. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  9. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

最新文章

  1. [英文面試]如何寫面試後的感謝信
  2. Ubuntu下使用GDB断点Go程序
  3. dotweb——go语言的一个微型web框架(二)启动dotweb
  4. Python--Redis实战:第三章:Redis命令:第七节:其他命令
  5. [Unity 游戏设计的元素]
  6. 茫茫内存,我该如何用 windbg 找到你 ?
  7. java树算法_Java数据结构算法(三)树
  8. c语言指针++_C ++此指针| 查找输出程序| 套装3
  9. System V 信号量
  10. 如何使用Git上传项目代码到github
  11. 马斯克惹麻烦?特斯拉股票一夜暴跌12%,千亿市值蒸发
  12. Num48 boss---02(pdm翻转,持久表现抽取,用户登退,消息提示,修密,自定义拦截器)...
  13. weblogic 解决线程阻塞
  14. C#中引用第三方ocx控件引发的问题以及解决办法
  15. 【Spring笔记】Spring创建hello程序
  16. Android半透明+RGB颜色代码大全
  17. MySQL学习1 基础入门
  18. 基于知识图谱的智能问答机器人技术架构
  19. SmartBI入门(一)介绍和安装
  20. 微信小程序的环境准备

热门文章

  1. 初始化参数之memory_target
  2. SpringMVC json/xml自动转换
  3. PostFix邮件网关无法向公网投递邮件问题分析
  4. storm1.x介绍
  5. linux下msmtp+mutt+shell发送邮件
  6. Oauth协议是否会泄露用户的密码
  7. 2017linux版本号,Linux基本命令 2017-11-27
  8. iOS汉字转拼音,日韩文字转拼音
  9. 让Spring 3中jsp的数据对象使用懒加载(FetchType.LAZY)与Controller的JSR 303并存
  10. SQL前三章知识点测试