题意:给出一个数组,一个在此基础上被操作过的数组,找出被操作过的最长的非递减连续序列。
题解:这道题最开始写就被给的样例迷惑了,给出的样例都是被操作过出现非递减序列的片段,因此我就直接去看第二个数组,找它的最长非递减连续序列了。但是题意中还有提到一层意思,如果找出的非递减序列没被操作过,就不能作为答案。所以不能但看第二个数组,还要比对第一个数组找出被操作的地方,再找其中的最长非递减序列。
代码:
import java.util.ArrayList;
import java.util.Scanner;
public class A {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
int tt=0;
while(t>0) {
t--;
tt++;
int n = cin.nextInt();
ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
for(int i=0; ians && flag==1) {
ll = l;
rr = i+2;
ans=cnt;
}
}
}else if(aa > bb){
if(cnt>ans && flag==1) {
ll = l;
rr = i+1;
ans=cnt;
}
// System.out.println(aa+ " " + bb + " " +ll + " " + rr);
cnt=0;
flag=0;
}
}
System.out.println(ll + " " + rr);
}
}
}
题意:对给出的字符串进行k次删除操作,每次操作删掉n个不相邻的字符,使得最后只剩下一种字母。求k的最小次数。
题解:这里我先找出给出字符串中出现过的字符,然后根据出现过的字符来找出它和它下一次出现时中间其他字符的个数,找出最大值就能求出留下这个字符所需操作的最小次数。然后依次将字符串中所有字符遍历。
代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class B {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0) {
t--;
String s = cin.next();
int len = s.length();
Map mp = new HashMap<>();
int []p = new int[10005];
for(char i:s.toCharArray()) {
if(mp.containsKey(i)){ //key存在
Integer value = mp.get(i);
value++;
mp.put(i,value);
}else{ //key不存在
mp.put(i,1);
}
}
int minn=Integer.MAX_VALUE,cnt=0,sum=0;
Character ms = new Character('a');
for(Character key: mp.keySet()) {
Integer val = mp.get(key); //出现次数
// System.out.println(key + ": " + val);
cnt=0;
for(int i=0; i0) {
if(sum%2==1) {
sum = (sum-1)/2;
cnt++;
}else {
sum /= 2;
cnt++;
}
}
minn = Math.min(cnt, minn);
}
System.out.println(minn);
}
cin.close();
}
}
题意:^^或者^_^是笑脸,每次只能插入一个^或者_,求最小插入次数。
题解:先遍历中间连续的_,连续n个则中间插入n-1个^,特判第一个和最后一个,还要特判一下只有一个字符的时候。
代码:
import java.util.Scanner;
public class C {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0) {
t--;
String s = cin.next();
int len = s.length();
int cnt=0,k=0;
if(len==1) {
if(s.charAt(0)=='^') {
cnt=1;
}else if(s.charAt(0)=='_') {
cnt=2;
}
}else if(len==2 && s.charAt(0)=='_' && s.charAt(1)=='_'){
cnt=3;
}else {
for(int i=1; i
题意:
题解:
代码:
import java.util.Scanner;
public class D {
public static void main(String[] args) throws Exception {
Scanner cin=new Scanner(System.in);
int t=cin.nextInt();
cin.nextLine();
while(t-->0) {
String s=cin.nextLine();
int i=0,j=s.length()-1;
if(!s.contains("0")){
long ans= (long) s.length() *s.length();
System.out.println(ans);
}
else{
long cnt=0,cnt1=0;
while(s.charAt(i)=='1'){
i++;
cnt++;
}
while(s.charAt(j)=='1'){
j--;
cnt1++;
}
int l=i+1,r=i+1;
long len=0;
for(;r<=j;){
if(s.charAt(r)=='1') r++;
else{
len=Math.max(len,r-l);
r++;
l=r;
}
}
len=Math.max(len,r-l);
len=Math.max(len,cnt+cnt1);
long ans=0,p=1,q=0;
while(p<=len){
q=p*len;
ans=Math.max(q,ans);
p++;
len--;
}
System.out.println(ans);
}
}
}
}
题意:
题解:
代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class E {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0) {
t--;
int n = cin.nextInt();
ArrayList a = new ArrayList<>();
Map mp = new HashMap<>();
int cnt=0;
for(int i=0; i
题意:划掉最少的点,使得给出的起始位置走不到终点。
题解:特判四个点和除去点以外的边需要的点。
代码:
import java.util.Scanner;
// F - Li Hua and Maze
public class F {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0) {
int n = cin.nextInt();
int m = cin.nextInt();
int x1 = cin.nextInt();
int y1 = cin.nextInt();
int x2 = cin.nextInt();
int y2 = cin.nextInt();
int cnt=0;
if((x1==1 && y1==1) || (x1==n && y1==1) || (x1==1 && y1==m) || (x1==n && y1==m)) {
cnt=2;
}else if((x2==1 && y2==1) || (x2==n && y2==1) || (x2==1 && y2==m) || (x2==n && y2==m)) {
cnt=2;
}else if((x1==1 && y1!=1 && y1!=m) || (x2==1 && y2!=1 && y2!=m)) {
cnt=3;
}else if((y1==1 && x1!=1 && x1!=n) || (y2==1 && x2!=1 && x2!=n)) {
cnt=3;
}else if((x1==n && y1!=1 && y1!=m) || (x2==n && y2!=1 && y2!=m)) {
cnt=3;
}else if((y1==m && x1!=1 && x1!=n) || (y2==m && x2!=1 && x2!=n)) {
cnt=3;
}else if(x1!=1 && x2!=1 && y1!=1 && y2!=1) {
cnt=4;
}
System.out.println(cnt);
t--;
}
cin.close();
return ;
}
}
题意:给出长度为n的数组,可以做出k个操作,将二维数组翻转180°让它和原来长的一样。
题解:先找出旋转以后与原来颜色不一样的个数cnt,每次操作可以换一个颜色k-1。
如果k 如果k>=cnt时,k为奇数则分为两种情况: 1. n为奇数时,则k可以变换最中间的点,那么对称。 2. n为偶数时,则不能满足题意,怎么变都不一样。 k为偶数则怎么变都满足题意。 代码: 题意: 题解: 代码: 题意:给出n个长度为m的数组,让求出排列组合后每组根据给出公式求出结果的和。 题解:先找出所有两两一组的结果,然后根据每组顺序套公式求出结果相加。 代码: 扩展: 输出: 注意:向动态二维数组内添加行,要在第一层循环内定义,保证更新不同的ArrayList,否则就会出现都加到同一行的情况。 错误的: 正确的: 输出: 题意: 题解: 代码: 题意: 题解: 代码: 题意: 题解: 代码:import java.util.Scanner;
public class G {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0) {
t--;
int n = cin.nextInt();
int k = cin.nextInt();
int [][]a = new int[1005][1005];
for(int i=0; i
I. Lucky Numbers
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class I {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
int []g = new int[10];
ArrayList
J.Playing in a Casino
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
public class J {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0) {
t--;
long n = cin.nextInt();
long m = cin.nextInt();
ArrayList
用 ArrayList 创建动态二维数组:
import java.util.ArrayList;
import java.util.List;
public class I {
public static void main(String []args) {
//创建一个二维数组
List
> re = new ArrayList
>();
//增加元素
//添加行
List
求一组数两两组合的所有结果:
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Arrays;
public class J {
public static void main(String []args) {
List
K. Showstopper
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class I {
public static void main(String []args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0) {
t--;
int n = cin.nextInt();
int []a = new int[105];
int []b = new int[105];
for(int i=0; i
L. Three Sevens
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class L {
public static void main(String []args) {
Scanner scanner=new Scanner(System.in);
int t=scanner.nextInt();
while(t-->0) {
int m=scanner.nextInt();
HashMap
M. Candy Store
#include