美文网首页
USACO Combination Lock

USACO Combination Lock

作者: langlang_moqi | 来源:发表于2017-11-25 23:25 被阅读0次

偶尔重抄旧业嘛

大意是给一个范围N,然后A和B会从1<=x<=N 中分别选出A1,A2,A3,B1,B2,B3;求有多少组3位数(C1,C2,C3),满足A或者B中三位数一一对应位相差 <=2,注意数排列是一个环,像钟表走时一样。

可以直接遍历剪枝:

Version:1.0StartHTML:0000000128EndHTML:0000015312StartFragment:0000000128EndFragment:0000015312SourceURL:about:blank

/*

ID: wanglan1

LANG: JAVA

TASK: combo

*/

importjava.io.BufferedReader;

importjava.io.BufferedWriter;

importjava.io.FileReader;

importjava.io.FileWriter;

importjava.io.IOException;

importjava.io.PrintWriter;

public classcombo {

public static voidmain(String args[])throwsIOException {

BufferedReader f =newBufferedReader(newFileReader("combo.in"));

PrintWriter out =newPrintWriter(newBufferedWriter(newFileWriter("combo.out")));

intlen = Integer.valueOf(f.readLine());

String[] ssA = f.readLine().trim().split(" ");

String[] ssB = f.readLine().trim().split(" ");

f.close();

int[] a = {Integer.valueOf(ssA[0]),Integer.valueOf(ssA[1]),Integer.valueOf(ssA[2])};

int[] b = {Integer.valueOf(ssB[0]),Integer.valueOf(ssB[1]),Integer.valueOf(ssB[2])};

intcount =0,maxA,maxB,minA,minB;

for(inti =1;i <= len;i++) {

booleanAA00 =false,BB00 =false;

BB00 =false;

maxA =max(i,a[0]);

minA =min(i,a[0]);

maxB =max(i,b[0]);

minB =min(i,b[0]);

if(isCan(maxA,minA,len))

AA00 =true;

if(isCan(maxB,minB,len))

BB00 =true;

if(AA00 || BB00)

for(intj =1;j <= len;j++) {

booleanAA11 =false,BB11 =false;

maxA =max(j,a[1]);

minA =min(j,a[1]);

maxB =max(j,b[1]);

minB =min(j,b[1]);

if(AA00 &&isCan(maxA,minA,len))

AA11 =true;

if(BB00 &&isCan(maxB,minB,len))

BB11 =true;

if(AA11 || BB11)

for(intk =1;k <= len;k++) {

maxA =max(k,a[2]);

minA =min(k,a[2]);

maxB =max(k,b[2]);

minB =min(k,b[2]);

if(AA00 && AA11 &&isCan(maxA,minA,len) || BB00 && BB11 &&isCan(maxB,minB,len)) {

count++;

}

}

}

}

out.println(count);

out.close();

}

private static intmax(inta, intb) {

returna > b ? a : b;

}

private static intmin(inta, intb) {

returna > b ? b : a;

}

private static booleanisCan(inta, intb, intlen) {

if(a -2<= b || (a +2- len >= b))

return true;

return false;

}

}

相关文章

网友评论

      本文标题:USACO Combination Lock

      本文链接:https://www.haomeiwen.com/subject/rhyhbxtx.html