# 洛谷提单：【数据结构1-1】线性表

（1）【数据结构1-1】线性表

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws IOException {
int n=sc.nextInt(),m=sc.nextInt();
int[] arr=new int[n+1];
for(int i=1;i<=n;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<m;i++) {
System.out.println(arr[sc.nextInt()]);
}
}
}
StreamTokenizer tokenizer;
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}

public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}



（2）P3613 【深基15.例2】寄包柜

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws IOException {
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt(),q=sc.nextInt();
node[] desk=new node[100005];
for(int i=0;i<n;i++) {
desk[i]=new node();
}
while(q-->0)
{
int x,a,b,c;
x=sc.nextInt();
if(x == 1)
{
a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();
desk[a].s++;
}
else
{
a=sc.nextInt();b=sc.nextInt();
for(int i = desk[a].s - 1;i >= 0;i--)
{
if(desk[a].num.get(i) == b)
{
out.println(desk[a].w.get(i));
break;
}
}
}
}
out.close();
}
}
class node{
ArrayList<Integer> num=new ArrayList<Integer>(),w=new ArrayList<Integer>();
int s = 0;
}
StreamTokenizer tokenizer;
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}

public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}



（3）P1449 后缀表达式

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Stack;

public class Main {
public static void main(String[] args) throws IOException {
Stack<Integer> stack=new Stack<Integer>();
int num=0;
for(int i=0;i<s.length();i++) {
char x=s.charAt(i);
if(Character.isDigit(x)) {
num=num*10+(int)(x-'0');
}
if(x=='.') {
stack.push(num);
num=0;
}
if(x=='+') {
int a=stack.pop();
int b=stack.pop();
stack.push(a+b);
}
if(x=='-') {
int a=stack.pop();
int b=stack.pop();
stack.push(b-a);
}
if(x=='*') {
int a=stack.pop();
int b=stack.pop();
stack.push(a*b);
}
if(x=='/') {
int a=stack.pop();
int b=stack.pop();
stack.push(b/a);
}
}
System.out.println(stack.pop());
}
}



（4）P1996 约瑟夫问题

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
boolean[] arr=new boolean[n+1];
int count=0;
int k=0;
while(count!=n) {
int mm=m;
while(mm>0) {
k++;
if(k>n) {
k-=n;
}
if(!arr[k]) {
mm--;
}
}
System.out.print(k+" ");
count++;
arr[k]=true;
}
}
}



（5）P1160 队列安排

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) throws IOException {
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
for(int i=2;i<=n;i++) {
}
int m=sc.nextInt();
for(int i=0;i<m;i++) {
int x=sc.nextInt();
if(al.indexOf(x)!=-1)
al.remove(al.indexOf(x));
}
for(int x:al) {
out.print(x+" ");
}
out.close();

}
}
StreamTokenizer tokenizer;
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}

public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}



（6）P1540 机器翻译

import java.util.Arrays;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int M=sc.nextInt();
int N=sc.nextInt();
int[] help=new int[1001];
int[] arr=new int[M];
Arrays.fill(arr, -1);
int k=0;
int count=0;
for(int i=0;i<N;i++) {
int x=sc.nextInt();
if(help[x]==1) {
continue;
}
count++;
if(arr[k]!=-1) {
help[arr[k]]=0;
}
help[x]=1;
if(k==M-1) {
arr[k]=x;
k=0;
}
else {
arr[k++]=x;
}
}
System.out.println(count);
}
}


（7）P2058 海港

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StreamTokenizer;
import java.math.BigInteger;

public class Main {
public static void main(String[] args) throws IOException {
int n=sc.nextInt();
int[] time=new int[300005];    //记录第i个人他到达的时间
int[] peo=new int[300005];    //记录第i个人他的国籍
int[] arr=new int[100005];   //记录每个国籍的人数
int z=0,num=0,r=0;
while(n-->0) {
int t=sc.nextInt(),k=sc.nextInt();
while(k-->0) {
peo[r]=sc.nextInt();
time[r]=t;
if(arr[peo[r]]==0) {
num++;
}
arr[peo[r]]++;
r++;
}
while(t-time[z]>=86400) {
arr[peo[z]]--;
if(arr[peo[z++]]==0) {
num--;
}
}
System.out.println(num);
}
}
}
StreamTokenizer tokenizer;
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}

public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}



（8）P1241 括号序列

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Stack;

public class Main {
public static void main(String[] args) throws IOException {
int  n=s.length();
Stack<Character> st=new Stack<Character>();
Stack<Integer> st1=new Stack<Integer>();
char[] arr=new char[n];
boolean[] vis=new boolean[n];
for(int i=0;i<n;i++) {
arr[i]=s.charAt(i);
if(arr[i]=='('||arr[i]=='[') {
}
else {
if(!st.isEmpty()) {
if(arr[i]==')'&&st.peek()=='(') {
st.pop();
vis[st1.pop()]=true;
vis[i]=true;
}
if(arr[i]==']'&&st.peek()=='[') {
st.pop();
vis[st1.pop()]=true;
vis[i]=true;
}
}
}
}
for(int i=0;i<n;i++) {
if((arr[i]=='('||arr[i]=='[')&&vis[i]==true) {
System.out.print(arr[i]);
}
if((arr[i]=='('||arr[i]==')')&&vis[i]==false){
System.out.print("()");
}
if((arr[i]=='['||arr[i]==']')&&vis[i]==false){
System.out.print("[]");
}
if((arr[i]==')'||arr[i]==']')&&vis[i]==true) {
System.out.print(arr[i]);
}

}
}
}


（9）P4387 【深基15.习9】验证栈序列

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.Stack;

public class Main {
public static void main(String[] args) throws IOException {
PrintWriter out=new PrintWriter(System.out);
int q=sc.nextInt();
int[] a=new int[100000];
int[] b=new int[100000];
while(q-->0) {
Stack<Integer> st=new Stack<Integer>();
int n=sc.nextInt();
boolean flag=true;
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
b[i]=sc.nextInt();
}
int k=0;
for(int i=0;i<n;i++) {
st.push(a[i]);
while(!st.isEmpty()&&st.peek()==b[k]) {
k++;
st.pop();
}
}
if(st.isEmpty()) {
out.println("Yes");
}
else {
out.println("No");
}
}
out.close();
}
}
StreamTokenizer tokenizer;
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}

public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}



（10）P2234 [HNOI2002]营业额统计

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.Arrays;

public class Main {
public static void main(String[] args) throws IOException {
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
no[] arr=new no[n];
for(int i=0;i<n;i++) {
arr[i]=new no(i,sc.nextInt());
}
Arrays.sort(arr);
long sum=0;
for(int i=0;i<n;i++) {
int temp=Integer.MAX_VALUE;
for(int j=i-1;j>=0;j--) {
if(arr[i].day>arr[j].day) {
temp=Math.min(temp, Math.abs(arr[i].num-arr[j].num));
break;
}
}
for(int j=i+1;j<n;j++) {
if(arr[i].day>arr[j].day) {
temp=Math.min(temp, Math.abs(arr[i].num-arr[j].num));
break;
}
}
if(arr[i].day==0) {
sum+=arr[i].num;
}
else {
sum+=temp;
}
}
out.println(sum);
out.close();
}
}
class no implements Comparable<no>{
int day,num;
no(int day,int num){
this.day=day;this.num=num;
}
@Override
public int compareTo(no o) {
return num-o.num;
}

}
StreamTokenizer tokenizer;
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}

public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}



©️2019 CSDN 皮肤主题: 猿与汪的秘密 设计师: 上身试试