备注 :此题解的[]为数学中的集合表示。
A. Least Product
题意:一共有n个数字,想要求最小的乘积。可以做任意次操作,如果a[i]是负数,可以将此数修改成[a[i] , 0]。如果a[i]是正数,可以将此数修改成[0 , a[i]] 。
题解:如果没有负数,那么最小的数字是将一个数字修改成0。如果有零,那么直接输出0,。如果有负数,是偶数的话,将随便一个数字改成0。是奇数的话,直接输出0即可。
代码:
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 4e5 + 7 ;
inline ll read(){
ll x = 0 , f = 1 ;
char c = getchar() ;
while(c > '9' || c < '0'){
if(c == '-')
f = -1 ;
c = getchar() ;
}
while(c >= '0' && c <= '9'){
x = x * 10 + c - '0' ;
c = getchar() ;
}
return x * f ;
}
char s[maxn] , S[maxn] ;
ll t , n , k , q , a[maxn] , c[maxn] ;
bool vis[maxn] ;
ll b[maxn] , d[maxn] ;
void solve(){
n = read() ;
for(int i = 1 ; i <= n ; i ++){
a[i] = read() ;
}
int fu = 0 , zheng = 0 ;
for(int i = 1 ; i <= n ; i ++){
if(a[i] == 0){
cout << 0 << endl ;
return ;
}
if(a[i] < 0){
fu ++ ;
}
if(a[i] > 0){
zheng ++ ;
}
}
if(fu == 0){
cout << 1 <<endl ;
cout << 1 << " " << 0 << endl ;
return ;
}
if(fu %2 == 1){
cout << 0 << endl ;
return ;
}
else{
for(int i = 1 ; i <= n ; i ++){
if(a[i] < 0){
cout << 1 << endl ;
cout << i << " " << 0 << endl ;
return ;
}
}
}
}
int main(){
t = read() ;
while(t --){
solve() ;
}
return 0 ;
}
B. Erase First or Second Letter
题意:给你一个字符串,每次可以删去一个数字,可以选择删第一个数组或者第二个数字。问一共可以构成多少个非空的不同字符串。
题解:这明显是一道逻辑思维题,很容易得出规律,每个字符串新出现贡献1,重复出现没有贡献,累加即可。
代码:
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 4e5 + 7 ;
inline ll read(){
ll x = 0 , f = 1 ;
char c = getchar() ;
while(c > '9' || c < '0'){
if(c == '-')
f = -1 ;
c = getchar() ;
}
while(c >= '0' && c <= '9'){
x = x * 10 + c - '0' ;
c = getchar() ;
}
return x * f ;
}
char s[maxn] , S[maxn] ;
ll t , n , k , q , a[maxn] , c[maxn] ;
bool vis[maxn] ;
ll b[maxn] , d[maxn] ;
void solve(){
n = read() ;
scanf("%s" , s + 1) ;
map < ll , ll > mp ;
ll cnt = 0 , ans = 0 ;
for(int i = 1 ; i <= n ; i ++){
int res = s[i] - 'a' ;
if(mp[res] == 0){
cnt ++ ;
mp[res] = 1 ;
}
ans += cnt ;
}
cout << ans << endl ;
}
int main(){
t = read() ;
while(t --){
solve() ;
}
return 0 ;
}
C. Watering an Array
题意:给你n个数字a和k个数字b还有一个d,有两个操作,一个是将a[i]的[1,b[i % k]]加上1,第二个操作是统计(a[i] == i)的个数,添加到答案里。每次必须选一个操作操作,问最大的答案是多少。
题解:因为他第二个操作的统计答案一定是递增的才会增加更多的答案,但是第一个操作会使数列变成递减的,所以多次选择一操作再统计答案没有选一次一操作统计一次答案更优。那么问题就转化为原始数列加上n次b[i],如何使答案更优,因为一次操作可以增加[1 , n] ,所以大胆猜测最多操作2 * n 次。那么答案就是每次的(a[i] == i)的数量加上(d - (i + 1)) / 2的数量,求最大值即可。
代码:
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 1e6 + 7 ;
inline ll read(){
ll x = 0 , f = 1 ;
char c = getchar() ;
while(c > '9' || c < '0'){
if(c == '-')
f = -1 ;
c = getchar() ;
}
while(c >= '0' && c <= '9'){
x = x * 10 + c - '0' ;
c = getchar() ;
}
return x * f ;
}
char s[maxn] , S[maxn] ;
ll t , n , k , q , a[maxn] , c[maxn] ;
bool vis[maxn] ;
ll b[maxn] , d[maxn] ;
void solve(){
n = read() ;
k = read() ;
q = read() ;
for(int i = 1 ; i <= n ; i ++){
a[i] = read() ;
}
for(int i = 0 ; i < k ; i ++){
b[i] = read() ;
}
ll ans = 0 ;
for(int i = 0 ; i < 2 * n && (i + 1) <= q ; i ++){
ll cnt = 0 ;
for(int j = 1 ; j <= n ; j ++){
if(a[j] == j){
cnt ++ ;
}
}
ans = max(ans , cnt + (q - (i + 1)) / 2) ;
ll t = b[i % k] ;
for(int j = 1 ; j <= t ; j ++){
a[j] ++ ;
}
}
cout << ans << endl ;
}
int main(){
t = read() ;
while(t --){
solve() ;
}
return 0 ;
}
喜欢作者的记得点赞收藏加关注哦~~~