A - Two Sequences 2

Problem Statement
Snuke has two number sequences
a
and
b
, each of length
N
. The
i
-th number of
a
is
a
i
, and that of
b
is
b
i
.

Using these sequences, he has decided to make a new sequence
c
of length
N
. For each
n
such that
1

n

N
,
c
n
, the
n
-th number of
c
, is the maximum value of
a
i
b
j
for a pair
(
i
,
j
)
such that
1

i

j

n
. Formally, we have
c
n

max
1

i

j

n
a
i
b
j
.

Find
c
1
,
c
2
,

,
c
N
.

Constraints
All values in input are integers.
1

N

2
×
10
5
1

a
i
,
b
i

10
9
Input
Input is given from Standard Input in the following format:

N

a
1

a
2

a
N

b
1

b
2

b
N

Print
Print
N
lines. The
n
-th line from the top should contain
c
n
.

Sample Input 1
Copy
3
3 2 20
1 4 1
Sample Output 1
Copy
3
12
20
We have:

c
1

max
(
a
1
b
1
)

3
;
c
2

max
(
a
1
b
1
,
a
1
b
2
,
a
2
b
2
)

12
;
c
3

max
(
a
1
b
1
,
a
1
b
2
,
a
1
b
3
,
a
2
b
2
,
a
2
b
3
,
a
3
b
3
)

20
.
Sample Input 2
Copy
20
715806713 926832846 890153850 433619693 890169631 501757984 778692206 816865414 50442173 522507343 546693304 851035714 299040991 474850872 133255173 905287070 763360978 327459319 193289538 140803416
974365976 488724815 821047998 371238977 256373343 218153590 546189624 322430037 131351929 768434809 253508808 935670831 251537597 834352123 337485668 272645651 61421502 439773410 621070911 578006919
Sample Output 2
Copy
697457706539596888
697457706539596888
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026

代码

每次在a数组找到前i个字母里面最大的数字,然后找到,然后下标在这个最大的数字后面找到最大的b数组里的最大的数
可以把下边那个放到外边优化成1维
答案一定在前i个字母的最大值和b[i]的乘积里面

#include<iostream>

using namespace std;
const int N=2e5+10;
typedef long long LL;

LL a[N],b[N];

int main()
{
    
    
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
        LL x;
        scanf("%lld",&x);
        a[i]=max(a[i-1],x);
    }
    for(int i=1;i<=n;i++)  scanf("%lld",&b[i]);
    LL maxv=a[1]*b[1];
    for(int i=1;i<=n;i++)
    {
    
    
        LL c=b[i]*a[i];
        maxv=max(maxv,c);
        printf("%lld\n",maxv);
    }
    return 0;
}

B - Mex Boxes
Problem Statement
Snuke has
N
balls, each with an integer written on it. The numbers on the balls are
a
1
,
a
2
,

,
a
N
.

He has decided to put these
N
balls in
K
boxes. Every ball must be in some box, but there may be boxes with zero or multiple balls.

After he put the balls in the boxes, the lid of each box will show an integer. Let
x
be the integer shown on a box.
x
is the minimum non-negative integer such that the box contains no ball with
x
. For example, the lid of an empty box will show
0
; the lid of a box with balls
0
,
1
,
3
,
5
will show
2
; the lid of a box with balls
1
,
2
,
3
will show
0
.

Find the maximum possible sum of the integers the lids show.

Constraints
All values in input are integers.
1

K

N

3
×
10
5
0

a
i
<
N
Input
Input is given from Standard Input in the following format:

N

K

a
1

a
2

a
N

Output
Print the maximum possible sum of the integers the lids show.

Sample Input 1
Copy
4 2
0 1 0 2
Sample Output 1
Copy
4
An optimal solution is to allocate the sets of balls
{
0
,
1
,
2
}
,
{
0
}
to the boxes.
In this case, the lids show
3
,
1
, respectively, for a total of
4
.
Sample Input 2
Copy
5 2
0 1 1 2 3
Sample Output 2
Copy
4
An optimal solution is to allocate the (multi)sets of balls
{
0
,
1
,
1
,
2
,
3
}
,
{
}
to the boxes.
In this case, the lids show
4
,
0
, respectively, for a total of
4
.
Note that we may have empty boxes.
Sample Input 3
Copy
20 4
6 2 6 8 4 5 5 8 4 1 7 8 0 3 6 1 1 8 3 0
Sample Output 3
Copy
11

枚举每一个队伍,如果当前的队伍的这个人用过后,就给这个人的数量减1

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
 
using namespace std;
 
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
 
const int mod = 20010905;
const int PP = 131;
 
inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}
 
const int N = 3e5 + 10;
const int M = 35;
int n, k;
int a[N], cnt[N];
 
signed main(){
	gt(n), gt(k);
	for (int i = 1; i <= n; i ++){
		gt(a[i]);
		cnt[a[i]] ++;
	}  
	
	int ans = 0;
	for (int i = 1; i <= k; i ++){
		for (int j = 0; j <= N; j ++){
			if (!cnt[j]){
				ans += j;
				break;
			}
			else  cnt[j] --;
		}
	}
	
	cout << ans << endl;
	
	return 0;
}```

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/112725936
今日推荐