Codeforces Round 917 (Div. 2) (1917)A-C JAVA题解

  A - Least Product

题目

You are given an array of integers a1,a2,…,an. You can perform the following operation any number of times (possibly zero):

  • Choose any element ai from the array and change its value to any integer between 00 and ai (inclusive). More formally, if ai<0, replace a with any integer in [ai,0], otherwise replace aiwith any integer in [0,ai]

Let be the minimum possible product of all the ai after performing the operation any number of times.

Find the minimum number of operations required to make the product equal to . Also, print one such shortest sequence of operations. If there are multiple answers, you can print any of them.

翻译

给你一个整数数组 。您可以执行以下操作任意次数(可能为零):

- 从数组中任选一个元素 ai ,将其值改为0和 ai(含)之间的任意整数。更正式地说,如果是 ai  >0 ,则将 ai 替换为 [ai, 0] 中的任意整数,否则将 ai替换为 [0, ai] 中的任意整数。

假设r是执行任意次数运算后所有 a_i 的最小乘积。

求使乘积等于 r所需的最小运算次数。同时,请打印这样一个最短的运算序列。如果有多个答案,可以打印任意一个。

题解

思维题,如果数组里有0出现不用操作,负数有奇数个不修改,其他情况只用把一个数改为0

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {

static int []a=new int [200005];
	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
	while((t--)>0) {
	int n=sc.nextInt();
	int op=0;int flag =0 ;
	int f=0;
	for(int i=0;i<n;i++) {
		a[i]=sc.nextInt();
		if(a[i]<0)op++;
		if(a[i]==0)flag=2;
		
	}
	if(flag==2)System.out.println("0");
	else if(op%2==1)System.out.println("0");
	else {
		System.out.println("1"+'\n'+"1 0");
	}
	}
	}
	
}

B - Erase First or Second Letter

题目

You are given a string s of length n. Let's define two operations you can apply on the string:

  • remove the first character of the string;
  • remove the second character of the string.

Your task is to find the number of distinct non-empty strings that can be generated by applying the given operations on the initial string any number of times (possibly zero), in any order.

翻译

给你一个长度为 n 的字符串 s 。让我们定义一下可以对字符串进行的两种操作:

- 删除字符串的第一个字符;
- 删除字符串的第二个字符。

你的任务是找出对初始字符串进行任意次数(可能为零)、任意顺序的操作后,可以生成的**非空**字符串的个数。

题解

通过set来记录不同的字符数量,然后将更新后的种类加到答案中。

PS:不会用JAVA的set,所以先用c++来写,后面会了再改

#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
int a[N*3];
int sum[N],hav[N],num[N];
int main() {
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    string s;
    cin>>s;
    set<char>st;
    int op=0;
    for(int i=0;i<n;i++){
        st.insert(s[i]);
        op=op+st.size();
    }
    cout<<op<<endl;
}
    }

C - Watering an Array

题目

翻译

题解

枚举第一次操作2前会有多少次操作1,遍历数组统计,然后按照最优操作进行统计

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
static int []b=new int [200005];
static int []a=new int [200005];
	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
	while((t--)>0) {
	int n=sc.nextInt();
	int op=0;int k;int d;
	int up=0;
	k=sc.nextInt();
	d=sc.nextInt();
	
for(int i=1;i<=n;i++) {
	a[i]=sc.nextInt();
	if(a[i]==i)up++;
}
for(int i=1;i<=k;i++) {
	b[i]=sc.nextInt();
}
b[0]=b[k];
for(int i=1;i<=Math.min(2*n+1,d);i++){
    op=Math.max(op,(d-i)/2+up);
	int x=b[i%k];
	for(int j=1;j<=x;j++){
		if(a[j]==j){
			up--;
		}
		a[j]++;
		if(a[j]==j){
			up++;
		}
	}
}
System.out.println(op);
	}
	}
	
}

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值