codeforces 498 div3(a-e java)
【摘要】 题目链接 A:水题
package codeforces498;
import java.util.Scanner;
public class testA {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
...
题目链接
A:水题
package codeforces498;
import java.util.Scanner;
public class testA {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{ a[i]=sc.nextInt(); if(a[i]%2==0)a[i]--;
}
for(int i=0;i<n;i++)
{ if(i==n-1)System.out.println(a[i]); else System.out.print(a[i]+" ");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
B
package codeforces498;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class testB {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
List<String> list=new ArrayList();
for(int i=0;i<n;i++)
{ a[i]=sc.nextInt();
} int b[]=a.clone(); Arrays.sort(b); int value=0; for(int i=b.length-1;i>b.length-1-k;i--) { list.add(b[i]+""); value+=b[i]; } System.out.println(value); int index=-1; for(int i=0;i<n;i++) { if(list.contains(a[i]+"")) { if(list.size()==1) {System.out.print(n-index-1);break;} else System.out.print((i-index)+" "); index=i; list.remove(a[i]+""); } }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
C:
package codeforces498;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class testC {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; long first[]=new long[n]; Map<Long,Integer>map=new HashMap(); map.put((long) 0, 0); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(i==0)first[i]=a[i]; else first[i]=first[i-1]+a[i]; map.put(first[i],i ); } long last[]=new long[n+1]; for(int i=n-1;i>0;i--) { last[i]=last[i+1]+a[i]; } for(int i=1;i<n+1;i++) { if(map.containsKey(last[i])) {if(map.get(last[i])<i) {System.out.println(last[i]);break;}} } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
D:
package codeforces498;
import java.util.Scanner;
public class testD {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String a=sc.next();
String b=sc.next();
char a1[]=a.toCharArray();
char b1[]=b.toCharArray();
boolean bool[]=new boolean[a.length()];
for(int i=0;i<n;i++)//标记相同的不动。
{ if(a1[i]==b1[i]) {bool[i]=true;}
}
int time=0;
for(int i=0;i<n;i++)
{ if(!bool[i]) { if(!bool[n-i-1]) { if(b1[i]==b1[n-i-1]) { char t=a1[i]; a1[i]=b1[i]; b1[i]=t; //time++; } if(a1[i]==b1[n-i-1]) { char t=a1[i]; a1[i]=a1[n-1-i]; a1[n-1-i]=t; bool[n-1-i]=true; } if(b1[i]==a1[n-1-i]) { char t=a1[i]; a1[i]=a1[n-1-i]; a1[n-1-i]=t; bool[i]=true; } } }
} for(int i=0;i<n;i++)
{ if(!bool[i]&&a1[i]!=b1[i]) {time++;}
}
System.out.println(time);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
E:
package codeforces498;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class testE {
static boolean bool=false; static int value,number=1; static int a[];//打表的值,通过位置反应值 static int b[];//
// static Map<Integer,Integer>map;//通过值反应对应位置 static int end[];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int q=sc.nextInt();
// map=new HashMap(); a=new int[n+1]; b=new int[n+1]; end=new int[n+1];
tree t[]=new tree[n+1];
for(int i=0;i<n+1;i++)
{ t[i]=new tree(i);
} for(int i=1;i<n;i++)
{ int team=sc.nextInt(); t[team].list.add(i+1); }
dfs(1,t);
for(int i=0;i<q;i++)
{ bool=false; int first1=sc.nextInt(); value=sc.nextInt();//数量 int fi=b[first1];//位置编号 if(fi+value-1>end[first1]) {System.out.println(-1);} else {System.out.println(a[fi+value-1]);} }
}
private static void dfs(int first, tree[] t) { a[number]=first; //map.put(first, number++); b[first]=number++; for(int i=0;i<t[first].list.size();i++) { dfs(t[first].list.get(i),t); } end[first]=number-1;
}
static class tree
{
int index; List<Integer> list=new ArrayList();
public tree() {}
public tree(int index)
{ this.index=index;
}
public tree(int index,ArrayList list)
{ this.index=index; this.list=list;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
文章来源: bigsai.blog.csdn.net,作者:Big sai,版权归原作者所有,如需转载,请联系作者。
原文链接:bigsai.blog.csdn.net/article/details/81667932
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)