201809-1 卖菜
简单模拟,不解释
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] price = new int[n];
for(int i=0; i<n; i++){
price[i] = in.nextInt();
}
in.close();
for(int i=0; i<n; i++){
if(i==0){
System.out.printf("%d ",(price[i] + price[i+1])/2);
}else if(i == (n-1)){
System.out.printf("%d\n", (price[i] + price[i-1])/2);
}else{
System.out.printf("%d ",(price[i-1] + price[i] + price[i+1])/3);
}
}
}
}
201809-2 买菜
寻找相交的子区间,使用pair数组存储区间(由于Java的Pair不在标准类库中,直接自行创建Pair类即可)
使用两个指针对
h
h
h curhh
以及
w
w
w curww
进行扫描,直到其中一个到达
n
n
n 即可计结束
根据两个区间的位置
-
无交的情况
a i ≥ d j a_i \geq d_j ai≥dj : h h h 的开始时间大于 w w w 结束时间,故curww++
b i ≤ c j b_i \leq c_j bi≤cj : h h h 的结束时间小于 w w w 开始时间,故curhh++
-
相交的情况
a i ≤ d j a_i \leq d_j ai≤dj && b i ≥ c j b_i \geq c_j bi≥cj
这时相交的开始时间是 h h h与 w w w中开始时间较大的,开始时间是 h h h与 w w w中结束时间较小的。而需要递增的区间为结束时间较小的,即作为相交结束的时间的那个区间。
import java.util.*;
class Pair{
public int start=-1, end=-1;
}
public class Main {
public static void main(String[] args) {
//数据读入
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Pair[] hh = new Pair[n];
Pair[] ww = new Pair[n];
for(int i=0; i<n; i++){
hh[i] = new Pair();
hh[i].start = in.nextInt();
hh[i].end = in.nextInt();
}
for(int i=0; i<n; i++){
ww[i] = new Pair();
ww[i].start = in.nextInt();
ww[i].end = in.nextInt();
}
//求相交区间
int ans = 0;
int cur_hh=0, cur_ww=0;
while(cur_hh!=n && cur_ww!=n){
if(hh[cur_hh].start >= ww[cur_ww].end){
cur_ww++;
continue;
}
if(hh[cur_hh].end <= ww[cur_ww].start){
cur_hh++;
continue;
}
if(hh[cur_hh].start <= ww[cur_ww].end || hh[cur_hh].end >= ww[cur_ww].start){
int startTime = Math.max(hh[cur_hh].start, ww[cur_ww].start);
int endTime = -1;
if(hh[cur_hh].end < ww[cur_ww].end){
endTime = hh[cur_hh].end;
cur_hh++;
}else{
endTime = ww[cur_ww].end;
cur_ww++;
}
ans += (endTime - startTime);
}
}
System.out.printf("%d\n", ans);
}
}
201809-2
尚未完成,有时间再写
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
class Node{
public String name;
public String id;
public int row_num;
public LinkedList<Node> next;
public Node(){
this.name = null;
this.id = null;
this.row_num = -1;
this.next = new LinkedList<>();
}
public Node(String name, int row_num){
this.name = name;
this.row_num = row_num;
this.next = new LinkedList<>();
}
}
public class Main {
public static Node root;
public static int[] result = new int[200];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
in.nextLine();
//constructor the tree
LinkedList<Node> parents = new LinkedList<>();
for(int i=0; i<n; i++){
String line = in.nextLine();
//first line
if(i == 0){
root = new Node(line,1);
parents.addLast(root);
continue;
}
//other line
//accumulate the number of '.'
int num = 0;
int index = 0;
while(line.charAt(index) == '.') {
num++;
index++;
}
//make sure the right element in the parents
while(parents.size() >= (num/2 + 1)){
parents.pollLast();
}
boolean have_id = line.contains("#");
String node_name = null;
String node_id = null;
int index_end = index;
if(have_id){
while(line.charAt(index_end)!= ' '){
index_end++;
}
node_name = line.substring(index,index_end);
node_id = line.substring(index_end + 1);
}else{
node_name = line.substring(index_end);
}
//the new node
Node cur = new Node(node_name, i+1);
cur.id = node_id;
//add to node tree
Node parent = parents.peekLast();
parent.next.addLast(cur);
parents.addLast(cur);
}
for(int j=0; j<m; j++){
//search answer
String que = in.nextLine();
//id
if(que.charAt(0) == '#'){
int re_row = search_id(que);
if(re_row!=-1){
System.out.printf("1 %d\n",re_row);
}else{
System.out.printf("0\n");
}
continue;
}
//tag
if(!que.contains(" ")){
int res = search_name(que);
System.out.printf("%d ",res);
for(int k=0; k<res; k++){
System.out.printf("%d ", result[k]);
}
System.out.printf("\n");
continue;
}
//gen
String[] qque = que.split(" ");
int res = 0;
if(qque[0].charAt(0) == '#'){
res = search_gen_id(qque);
}else{
res = search_gen_name(qque);
}
System.out.printf("%d", res);
for(int k=0; k<res; k++){
System.out.printf("%d ", result[k]);
}
System.out.printf("\n");
}
}
public static int search_id(String ser){
LinkedList<Node> stack = new LinkedList<>();
stack.addLast(root);
while(!stack.isEmpty()){
Node cur = stack.pollLast();
if(ser.equals(cur.id)){
return cur.row_num;
}
//search for next
for(Node ele : cur.next){
stack.addLast(ele);
}
}
return -1;
}
public static int search_name(String ser){
LinkedList<Node> stack = new LinkedList<>();
stack.addLast(root);
for(int i=0; i<200; i++){
result[i] = -1;
}
int index = 0;
while(!stack.isEmpty()){
Node cur = stack.pollLast();
if(ser.equals(cur.name)){
result[index++] = cur.row_num;
}
for(Node ele : cur.next){
stack.addLast(ele);
}
}
Arrays.sort(result,0,index);
return index;
}
public static int search_gen_name(String[] ser){
int result_ans = 0;
int len = ser.length;
String[] name_a = new String[120];
for(int i=0; i<120; i++){
name_a[i] = "";
}
for(int i=0; i<200; i++){
result[i] = -1;
}
LinkedList<Node> stack = new LinkedList<>();
stack.addLast(root);
int depth = 0;
while (!stack.isEmpty()){
Node cur = stack.pollLast();
name_a[depth] = cur.name;
if(cur.name.equals(ser[len-1])){
boolean same = true;
int index = 1;
while(len - index -1 >= 0){
if( (depth - index) < 0){
same = false;
break;
}
if(!name_a[depth - index].equals(ser[len - index -1])){
same = false;
break;
}
index++;
}
if(same){
result[result_ans++] = cur.row_num;
}
}
}
}
public static int search_gen_id(String[] ser){
int len = ser.length;
return len;
}
}