题目
具体题目不是很记得,大概如下:
给出n个人,然后分别给出他们的成绩
n个人成环状(数组首尾相连),每个人都至少有一个奖牌
如果一个人的成绩比他左右两人高,则他的奖牌要比其余两人多
求最少需要的奖牌
思路
1.扩展数组,首位相连
2. 双向遍历,如果存在 分数比他后面的小,但是奖牌数比后面的大,调整后面的奖牌数
代码
/**
*
*/
/***
* @author 18071
* @Date 2019年3月18日
* 功能: "3" 2 3 1 5 3 "2" 扩展数组
* 从1开始往后 1 1 1 1 1
* 1 2 1 2 1
* 1 2 1 2 2
* ***/
public class test2 {
public static void main(String args[]) {
int n=4;
person [] p =new person[n+1];
p[0]=new person(1);
p[1]=new person(2);
p[2]=new person(3);
p[3]=new person(3);
//p[4]=new person(1);
//收尾相连
p[n]=new person(p[0].present);
solution22 s =new solution22();
s.so(p);
}
}
class solution22 {
public void so(person [] p) {
while(true) {
boolean flag =false;
for(int i=0;i<p.length-1;i++) {
if(p[i].score<p[i+1].score&& p[i].present>=p[i+1].present) {
p[i+1].present=p[i].present+1;
flag=true;
}
}
p[0].present=p[p.length-1].present;
for(int i= p.length-1;i>0;i--) {
System.out.println(i);
if(p[i-1].score>p[i].score&& p[i-1].present<=p[i].present) {
p[i-1].present=p[i].present+1;
flag=true;
}
}
if(!flag) {
break;
}
}
int count =0;
for(int i=0;i<p.length-1;i++) {
count=count+p[i].present;
}
System.out.println(count);
}
}
class person{
int score;
int present;
person(int score){
this.score=score;
this.present=1;
}
}
结果