第一道: 搬石头排序 题目:
沙滩摆放着一排大小不一的球形石头,已知第i个石头的半径为ri,不存在两个石头半径相等。现要求通过移动石头使摆放的石头从左往右半径递增。每次可选择一块石头,并把它放在剩下n-1块石头的最左边或最右边。求最少操作次数。
输入:
第一行一个整数n,表示石头个数。(1 <= n <= 100000).第二行n个整数,表示从左往右石头的半径r1,r2,…,rn( 1<= ri <= n)。保证不存在两个不同的石头半径相等。
输出:
最少操作次数。
样例输入
6
3 2 1 4 6 5
样例输出
3
package com.langchao;
import java.util.Scanner;
public class Solution1 {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int num=1,max=1;
while(in.hasNext()){
int tree=in.nextInt();
int[] c=new int[tree];
int[] cc=new int[tree];
for(int i=0;i<tree;i++){
c[i]=in.nextInt();
}
for(int i=0;i<tree;i++){
cc[i]=c[i];
for(int j=i+1;j<tree;j++){
cc[j]=c[j];
if(cc[i]+1==cc[j]){
num+=1;
cc[i]=cc[j];
}
}
if(num>max){
max=num;
}
num=1;
}
System.out.println(tree-max);
}
}
}
第二道 题目描述
【问题描述】
兴中道是中山最美丽的道路,路中间的绿化带上种了两列漂亮的大树,这些大树分成了50行,每行两棵大树,一共100棵大树,这些大树被编上了号,编号方式如下:
1 3 5 7 ………… 95 97 99
2 4 6 8 ………… 96 98 100
再过几天奥运火炬就要在中山传递了,美丽的兴中道当然是最重要的必经之路,但是某天晚上却发生了一件令人震惊的大事–可恶的破坏分子为了破坏奥运,让中山人民丢丑,竟然偷去了这100棵大树中的一部分!
公安部门马上出动,列出了被偷去了大树的编号。现在摆在我们面前的情况是,如果火炬的旁边是空空的树坑,那是令人无法接受的,因此我们只能压缩火炬在兴中道上的传递距离,务必使火炬在连续的大树边传递,当时,我们就得找出一列最长的连续的大树供传递火炬时展现在全世界的人面前。请你编写程序解决这一难题。
输入
【输入格式】
N (表示有N棵大树被盗)
N1 N2 N3……NN (被盗大树的编号)
输出
【输出格式】
M X (表示从第M棵大树开始,共有连续的X棵大树,如果有多个解,只输出一个解即可)
样例输入
59 15 27 35 6
样例输出
8 47
package com.oracle.aspect;
import java.util.Scanner;
public class Solution {
private static void abc(int a[], int tree[], int N) {
for (int i = 0; i < 50; i++) {
for (int j = 0; j < N; j++) {
if (a[i] == tree[j]) {
a[i] = 0;
}
}
}
}
private static void def(int a[], int y[], int x[], int f[]) {
for (int i = 0; i < 51; i++) {
if (a[i] != 0) {
y[0]++;
} else {
if (y[0] > x[0]) {
x[0] = y[0];
f[0] = a[i - x[0]];
}
y[0] = 0;
}
}
}
public static void main(String[] args) {
int N, j = 0, k = 0;
int[] tree = new int[100];
int[] a = new int[51];
int[] b = new int[51];
int[] x = {0};
int[] y = {0};
int[] f = {0};
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
N = in.nextInt();
for (int i = 0; i < N; i++) {
tree[i] = in.nextInt();
}
for (int i = 1; i < 100; i++) {
if (i % 2 != 0) {
a[j] = i;
j++;
} else {
b[k] = i;
k++;
}
}
abc(a, tree, N);
abc(b, tree, N);
def(a, y, x, f);
def(b, y, x, f);
System.out.println(f[0] + " " + x[0]);
}
}
}