TOI 2021/10 潛力組(一)-美味漢堡(Hamburger) Java解題心得( 題目 )
心得:因為TOI使用的是C++,所以沒有參加,是使用ZJ上面的題目,我覺得前面兩題算簡單,第三題很麻煩,還會遇到StackOverflow問題,目前還沒解決。
解題概念:Greedy(還是其實不是?)
解題方法:這題只要不斷讀測資,如果遇到重複的就選價值比較高的那個,如果不一樣,就加進來。
Java參考程式碼(有更好意見可留言於下方,謝謝!)
import java.io.*;
public class zjg544 {
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static int r,ret;
public static void main(String[] args) throws Exception {
final int n=readint();
final int k=readint();
int property=-1,greedy=0,out=0,w;
int[] s=new int[n];
for(int i=0;i<n;i++) {
s[i]=readint();
}
for(int i=0;i<n;i++) {
w=readint();
if(w!=property) {
//不一樣就加上去
property=w;//屬性
out+=greedy;
greedy=s[i];
}
else {
greedy=Math.max(s[i], greedy)//選擇比較好的那個
}
}
System.out.println(out+greedy);//輸出
}
public static int readint() throws Exception {
ret=0;
r=br.read();
while(r>47&&r<58) {
ret*=10;
ret+=(r&15);
r=br.read();
}
return ret;
}
}
文章標籤
全站熱搜
