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!
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.
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).
2
3 5
3 6
YES
3
6 8 9
6 10 12
NO
5
0 0 5 0 0
1 1 8 10 5
YES
4
4 1 0 3
5 2 2 3
YES
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; }
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.
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.
Print one integer — the total number of alive people after the bell rings.
4
0 1 0 10
1
2
0 0
2
10
1 1 3 0 0 0 2 1 0 3
3
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; }
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?
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.
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.
5
2 2 3 4 6
5
4
2 4 6 8
-1
3
2 6 9
4
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; }
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.
![](http://codeforces.com/predownloaded/7c/a6/7ca65d0d19301f7cb19a7b32b8537ad7e266b357.png)
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.
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.
2
1 2
2 1
4
1000 100 10 1
100 1 1000 10
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; else ans[to]=a[i-1].first; } for(int i=0; i<n; i++) { printf("%d ",ans[i]); } return 0; }