Posted onEdited onInJavaWord count in article: 4.5kReading time ≈4 mins.
一、完美对
有 n n个物品,每个物品有 k k个属性,第 i i件物品的第 j j个属性用一个正整数表示记为ai,ja**i,j,两个不同的物品 i,j i,j被称为是完美对的当且仅当ai,1+aj,1=ai,2+aj,2=⋯=ai,k+aj,ka**i,1+a**j,1=a**i,2+a**j,2=⋯=a**i,k+a**j,k,求完美对的个数。
// 注意类名必须为 Main, 不要有任何 package xxx 信息 publicclassMain { publicstaticvoidmain(String[] args) { Scannerin=newScanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case inta= in.nextInt();
int[] monster = newint[a]; for (inti=0 ; i < a ; i++) { monster[i] = in.nextInt(); }
intleft=0, res = Integer.MAX_VALUE; for (intright=1 ; right < a ; right++) { if (monster[left] * 2 >= monster[right]) { if(right > 1){ res = Math.min(res, (monster[right]+1)/2+((monster[left]-(monster[right]+1)/2)+1)/2); left++; }else{ res = Math.min(res,monster[left]); } }else{ res = Math.min(res,(monster[right]+1)/2); left++; } }