赞
踩
在一个数轴上,有一个机器人要从 x = 0 x = 0 x=0 处移动到 x = n x = n x=n 处。机器人身上有两种电池,第一种是普通电池,第二种是太阳能可充电电池,普通电池的容量上限为 b b b 点电量,太阳能电池的容量上限为 a a a 点电量。
定义数轴上的第 i i i 段线段代表左端点为 x = i − 1 x = i - 1 x=i−1,右端点为 x = i x = i x=i 的线段。
这 n n n 条线段中,有一些线段可以被太阳照射到。
当机器人向右移动一个单位时,它会消耗一点电量。
当机器人走到一个可以被太阳照射到的线段上时,如果他是使用普通电池走到这条线段的并且太阳能电池的电量不满,则可以增加一点电量。这里的线段特指长度为 1 1 1 的线段。即如果它从一条被照射到的线段上走到另一条被照射的线段上,依然有可能增加电量。
机器人总电量为 0 0 0 或到达终点时会停下。现在请你求出机器人最远可以走多远。
第一行三个整数代表 n , b , a n,~b,~a n, b, a
下面一行 n n n 个整数,第 i i i 个整数 s i s_i si 描述第 i i i 条线段是否被阳光照射,如果被照射则为 1 1 1,否则为 0 0 0。
一行一个整数,代表答案。
1 ≤ n , b , a ≤ 2 × 1 0 5 1~\leq~n,~b,~a~\leq~2~\times~10^5 1 ≤ n, b, a ≤ 2 × 105
∀ i ∈ [ 1 , n ] , 0 ≤ s i ≤ 1 \forall~i~\in~[1,~n],~0~\leq~s_i~\leq~1 ∀ i ∈ [1, n], 0 ≤ si ≤ 1
There is a robot staying at X = 0 X=0 X=0 on the O x Ox Ox axis.
He has to walk to X = n X=n X=n .
You are controlling this robot and controlling how he goes.
The robot has a battery and an accumulator with a solar panel.
The i i i -th segment of the path (from X = i − 1 X=i-1 X=i−1 to X = i X=i X=i ) can be exposed to sunlight or not.
The array s s s denotes which segments are exposed to sunlight: if segment i i i is exposed, then s i = 1 s_i = 1 si=1 , otherwise s i = 0 s_i = 0 si=0 .
The robot has one battery of capacity b b b and one accumulator of capacity a a a .
For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator).
If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can’t use the battery if its charge is zero).
And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can’t use the accumulator if its charge is zero).
If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can’t become higher than it’s maximum capacity).
If accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not.
You understand that it is not always possible to walk to X = n X=n X=n .
You want your robot to go as far as possible.
Find the maximum number of segments of distance the robot can pass if you control him optimally.
The first line of the input contains three integers n , b , a n, b, a n,b,a ( 1 ≤ n , b , a ≤ 2 ⋅ 1 0 5 1 \le n, b, a \le 2 \cdot 10^5 1≤n,b,a≤2⋅105 ) — the robot’s destination point, the battery capacity and the accumulator capacity, respectively.
The second line of the input contains n n n integers s 1 , s 2 , … , s n s_1, s_2, \dots, s_n s1,s2,…,sn ( 0 ≤ s i ≤ 1 0 \le s_i \le 1 0≤si≤1 ), where s i s_i si is 1 1 1 if the i i i -th segment of distance is exposed to sunlight, and 0 0 0 otherwise.
Print one integer — the maximum number of segments the robot can pass if you control him optimally.
5 2 1
0 1 0 1 0
5
6 2 1
1 0 0 1 0 1
3
In the first example the robot can go through the first segment using the accumulator, and charge levels become b = 2 b=2 b=2 and a = 0 a=0 a=0 .
The second segment can be passed using the battery, and charge levels become b = 1 b=1 b=1 and a = 1 a=1 a=1 .
The third segment can be passed using the accumulator, and charge levels become b = 1 b=1 b=1 and a = 0 a=0 a=0 .
The fourth segment can be passed using the battery, and charge levels become b = 0 b=0 b=0 and a = 1 a=1 a=1 .
And the fifth segment can be passed using the accumulator.
In the second example the robot can go through the maximum number of segments using battery two times and accumulator one time in any order.
#include<bits/stdc++.h> using namespace std; const int N=3e5+10; typedef long long ll; int n,a,b,s[N]; map<int,int>p; int main(){ cin>>n>>b>>a; for(int i=1;i<=n;i++){ scanf("%d",&s[i]); } int i=0; int b1=b,a1=a; while((a1||b1)&&i++<=n){ if(s[i]==0){ if(a1) a1--; else b1--; } else if(s[i]==1){ if(b1&&a1<a) b1--,a1++; else a1--; } if(i==n) break; } cout<<i<<endl; return 0; }
有n个打乱的点的x,y轴坐标(坐标点x,y都被打乱)现在告诉你这2*n个值,问最小的矩形面积能覆盖住n个点且矩形长和宽分别与x,y轴平行。
Pavel made a photo of his favourite stars in the sky.
His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes.
Strictly speaking, it makes a photo of all points with coordinates ( x , y ) (x, y) (x,y) , such that x 1 ≤ x ≤ x 2 x_1 \leq x \leq x_2 x1≤x≤x2 and y 1 ≤ y ≤ y 2 y_1 \leq y \leq y_2 y1≤y≤y2 , where ( x 1 , y 1 ) (x_1, y_1) (x1,y1) and ( x 2 , y 2 ) (x_2, y_2) (x2,y2) are coordinates of the left bottom and the right top corners of the rectangle being photographed.
The area of this rectangle can be zero.
After taking the photo, Pavel wrote down coordinates of n n n of his favourite stars which appeared in the photo.
These points are not necessarily distinct, there can be multiple stars in the same point of the sky.
Pavel has lost his camera recently and wants to buy a similar one.
Specifically, he wants to know the dimensions of the photo he took earlier.
Unfortunately, the photo is also lost.
His notes are also of not much help; numbers are written in random order all over his notepad, so it’s impossible to tell which numbers specify coordinates of which points.
Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes.
As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle.
The first line of the input contains an only integer n n n ( 1 ≤ n ≤ 100 000 1 \leq n \leq 100\,000 1≤n≤100000 ), the number of points in Pavel’s records.
The second line contains 2 ⋅ n 2 \cdot n 2⋅n integers a 1 a_1 a1 , a 2 a_2 a2 , …, a 2 ⋅ n a_{2 \cdot n} a2⋅n ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai≤109 ), coordinates, written by Pavel in some order.
Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel’s records.
4
4 1 3 2 3 2 1 3
1
3
5 8 5 5 7 5
0
In the first sample stars in Pavel’s records can be ( 1 , 3 ) (1, 3) (1,3) , ( 1 , 3 ) (1, 3) (1,3) , ( 2 , 3 ) (2, 3) (2,3) , ( 2 , 4 ) (2, 4) (2,4) .
In this case, the minimal area of the rectangle, which contains all these points is 1 1 1 (rectangle with corners at ( 1 , 3 ) (1, 3) (1,3) and ( 2 , 4 ) (2, 4) (2,4) ).
#include<bits/stdc++.h> using namespace std; const int N=4e5+10; typedef long long ll; ll n,a[N],k; int main(){ scanf("%lld",&n); for(int i=1;i<=2*n;i++){ scanf("%lld",&a[i]); } sort(a+1,a+1+n+n); k=(a[n]-a[1])*(a[2*n]-a[n+1]); for(int i=2;i<=n+1;i++){ k=min(k,(a[2*n]-a[1])*(a[i+n-1]-a[i])); } cout<<k<<endl; return 0; }
给出一个只包含小写字母的串 s s s,询问满足以下条件的子序列的个数:
1、这个子序列中的所有字符都是小写字母 a a a。
2、如果这个子序列的长度大于 1 1 1,那么这个子序列中的每两个字母之间一定要包含一个小写字母 b b b。
答案模 1 0 9 + 7 10^9 + 7 109+7。
∣ s ∣ ≤ 1 0 5 |s| \leq 10^5 ∣s∣≤105。
The Fair Nut found a string s s s .
The string consists of lowercase Latin letters.
The Nut is a curious guy, so he wants to find the number of strictly increasing sequences p 1 , p 2 , … , p k p_1, p_2, \ldots, p_k p1,p2,…,pk , such that:
The Nut is upset because he doesn’t know how to find the number. Help him.
This number should be calculated modulo 1 0 9 + 7 10^9 + 7 109+7 .
The first line contains the string s s s ( 1 ≤ ∣ s ∣ ≤ 1 0 5 1\leq |s| \leq 10^5 1≤∣s∣≤105 ) consisting of lowercase Latin letters.
In a single line print the answer to the problem — the number of such sequences p 1 , p 2 , … , p k p_1, p_2, \ldots, p_k p1,p2,…,pk modulo 1 0 9 + 7 10^9 + 7 109+7 .
abbaa
5
baaaa
4
agaa
3
In the first example, there are 5 5 5 possible sequences. [ 1 ] [1] [1] , [ 4 ] [4] [4] , [ 5 ] [5] [5] , [ 1 , 4 ] [1, 4] [1,4] , [ 1 , 5 ] [1, 5] [1,5] .
In the second example, there are 4 4 4 possible sequences. [ 2 ] [2] [2] , [ 3 ] [3] [3] , [ 4 ] [4] [4] , [ 5 ] [5] [5] .
In the third example, there are 3 3 3 possible sequences. [ 1 ] [1] [1] , [ 3 ] [3] [3] , [ 4 ] [4] [4]
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
typedef long long ll;
string a;
int s,x;
int main(){
cin>>a;
for(int i=0;i<a.size();i++){
if(a[i]=='a') s=(s+x+1)%1000000007;
if(a[i]=='b') x=s;
}
cout<<s<<endl;
return 0;
}
定义一个序列的“美丽度”为这个序列前 m m m 大的数的和。现在有一个长度为 n n n 的序列,你需要把它分成 k k k 个长度至少为 m m m 的连续子序列,每个元素不重不漏,使得这些子串的“美丽度”之和最大。而且要求输出划分方案。
1 ≤ n ≤ 2 × 1 0 5 , m ≥ 1 , k ≥ 2 , m × k ≤ n 1\le n\le 2\times 10^5,m\ge 1,k\ge 2,m\times k\le n 1≤n≤2×105,m≥1,k≥2,m×k≤n。
An array b b b is called to be a subarray of a a a if it forms a continuous subsequence of a a a , that is, if it is equal to a l a_l al , a l + 1 a_{l + 1} al+1 , … \ldots … , a r a_r ar for some l , r l, r l,r .
Suppose m m m is some known constant.
For any array, having m m m or more elements, let’s define it’s beauty as the sum of m m m largest elements of that array.
For example:
You are given an array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an , the value of the said constant m m m and an integer k k k . Your need to split the array a a a into exactly k k k subarrays such that:
The first line contains three integers n n n , m m m and k k k ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2≤n≤2⋅105 , 1 ≤ m 1 \le m 1≤m , 2 ≤ k 2 \le k 2≤k , m ⋅ k ≤ n m \cdot k \le n m⋅k≤n ) — the number of elements in a a a , the constant m m m in the definition of beauty and the number of subarrays to split to.
The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 −109≤ai≤109 ).
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print k − 1 k-1 k−1 integers p 1 , p 2 , … , p k − 1 p_1, p_2, \ldots, p_{k-1} p1,p2,…,pk−1 ( 1 ≤ p 1 < p 2 < … < p k − 1 < n 1 \le p_1 < p_2 < \ldots < p_{k-1} < n 1≤p1<p2<…<pk−1<n ) representing the partition of the array, in which:
If there are several optimal partitions, print any of them.
9 2 3
5 2 5 2 4 1 1 3 2
21
3 5
6 1 4
4 1 3 2 2 3
12
1 3 5
2 1 2
-1000000000 1000000000
0
1
In the first example, one of the optimal partitions is [ 5 , 2 , 5 ] [5, 2, 5] [5,2,5] , [ 2 , 4 ] [2, 4] [2,4] , [ 1 , 1 , 3 , 2 ] [1, 1, 3, 2] [1,1,3,2] .
The sum of their beauties is 10 + 6 + 5 = 21 10 + 6 + 5 = 21 10+6+5=21 .
In the second example, one optimal partition is [ 4 ] [4] [4] , [ 1 , 3 ] [1, 3] [1,3] , [ 2 , 2 ] [2, 2] [2,2] , [ 3 ] [3] [3] .
#include<bits/stdc++.h> using namespace std; const int N=3e5+10; typedef long long ll; int n,a[N],m,k,b[N]; map<int,int>p; int main(){ cin>>n>>m>>k; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+1+n); ll s=0; for(int i=n,j=1;j<=(k*m);i--,j++){ p[b[i]]++; s+=b[i]; } cout<<s<<endl; for(int i=1,now=0,q=0;i<=n;i++){ if(p[a[i]]){ now++; p[a[i]]--; } if(now>=m){ printf("%d ",i); q++; now=0; } if(q==k-1){ return 0; } } return 0; }
V o v a Vova Vova先生的家可以看作一个 n × 1 n \times 1 n×1的矩形,寒冷的冬天来了, V o v a Vova Vova先生想让他的家里变得暖和起来。现在我们给你 V o v a Vova Vova先生家的平面图,其中 1 1 1表示这个地方是加热炉,0表示这个地方什么也没有。所有加热器都有一个加热半径 r r r,一个位于 a i a_i ai加热器可以加热[ a i − r + 1 , a i + r − 1 a_i-r+1,a_i+r-1 ai−r+1,ai+r−1]的范围。现在, V o v a Vova Vova先生想让他的整个家都变得暖和,一开始所有的加热器都是关闭的,请你求出 V o v a Vova Vova先生最少要开几个加热器才能使整个家变得暖和
第一行:两个整数 n , r ( 1 ≤ n , r ≤ 1000 ) n,r(1≤n,r≤1000) n,r(1≤n,r≤1000),含义如上
第二行,n个整数,表示 V o v a Vova Vova家的地图
一个整数,表示 V o v a Vova Vova先生至少要打开几个加热器
Vova’s house is an array consisting of n n n elements (yeah, this is the first problem, I think, where someone lives in the array).
There are heaters in some positions of the array.
The i i i -th element of the array is 1 1 1 if there is a heater in the position i i i , otherwise the i i i -th element of the array is 0 0 0 .
Each heater has a value r r r ( r r r is the same for all heaters).
This value means that the heater at the position p o s pos pos can warm up all the elements in range [ p o s − r + 1 ; p o s + r − 1 ] [pos - r + 1; pos + r - 1] [pos−r+1;pos+r−1] .
Vova likes to walk through his house while he thinks, and he hates cold positions of his house.
Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova’s target is to warm up the whole house (all the elements of the array), i.e.
if n = 6 n = 6 n=6 , r = 2 r = 2 r=2 and heaters are at positions 2 2 2 and 5 5 5 , then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 3 3 3 elements will be warmed up by the first heater and the last 3 3 3 elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn’t like to pay much for the electricity.
So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
The first line of the input contains two integers n n n and r r r ( 1 ≤ n , r ≤ 1000 1 \le n, r \le 1000 1≤n,r≤1000 ) — the number of elements in the array and the value of heaters.
The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an ( 0 ≤ a i ≤ 1 0 \le a_i \le 1 0≤ai≤1 ) — the Vova’s house description.
Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
6 2
0 1 1 0 0 1
3
5 3
1 0 0 0 1
2
5 10
0 0 0 0 0
-1
10 3
0 0 1 1 0 1 0 0 0 1
3
In the first example the heater at the position 2 2 2 warms up elements [ 1 ; 3 ] [1; 3] [1;3] , the heater at the position 3 3 3 warms up elements [ 2 , 4 ] [2, 4] [2,4] and the heater at the position 6 6 6 warms up elements [ 5 ; 6 ] [5; 6] [5;6] so the answer is 3 3 3 .
In the second example the heater at the position 1 1 1 warms up elements [ 1 ; 3 ] [1; 3] [1;3] and the heater at the position 5 5 5 warms up elements [ 3 ; 5 ] [3; 5] [3;5] so the answer is 2 2 2 .
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position 3 3 3 warms up elements [ 1 ; 5 ] [1; 5] [1;5] , the heater at the position 6 6 6 warms up elements [ 4 ; 8 ] [4; 8] [4;8] and the heater at the position 10 10 10 warms up elements [ 8 ; 10 ] [8; 10] [8;10] so the answer is 3 3 3 .
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; typedef long long ll; int n,a[N],k,r; int main(){ scanf("%d%d",&n,&r); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); if(a[i]) a[i]=i; else a[i]=a[i-1]; } for(int i=n+1;i<=n+r;i++){ a[i]=a[i-1]; } int x=a[r]+r-1,k=1; while(x<n&&k<N) k++,x=a[x+r]+r-1; if(a[r]==0||k==N){ puts("-1"); return 0; } cout<<k<<endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。