problem1009
JAVA
A. GameShopping
签到题,不多说
public static void main(String[] args) throws InterruptedException {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt(), m = scan.nextInt();
int[] cost = new int[n];
Queue<Integer> q = new LinkedBlockingQueue<>();
for (int i = 0; i < n; i++) {
cost[i] = scan.nextInt();
}
for (int i = 0; i < m; i++) {
((LinkedBlockingQueue<Integer>) q).put(scan.nextInt());
}
int ans = 0;
Integer head = null;
for (int i = 0; i < n; i++) {
head = q.peek();
if (null == head) {
break;
}
if (head >= cost[i]) {
q.poll();
ans++;
}
}
System.out.println(ans);
}
B. Minimum Ternary String
根据题意可知1可以随意移动,2与0无法移动. 所以可以很快推出2与0的相对位置不会变化,1会通通移动到2前面. 那么只要把第一个2前的0全部先输出,然后输出1,最后再按顺序输出的2与0即可
public static void countWay(String s) {
int zeroBeforeTwo = 0;
int oneNum = 0;
boolean twoFlag = false;
Queue<Character> q = new LinkedBlockingQueue<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '1') {
oneNum++;
continue;
} else if (c == '2') {
twoFlag = true;
}
if (twoFlag) {
q.add(c);
} else if (c == '0') {
zeroBeforeTwo++;
}
}
//开始生成新字符
char[] newChar = new char[s.length()];
int p = 0;
for (int i = 0; i < zeroBeforeTwo; i++) {
newChar[p++] = '0';
}
for (int i = 0; i < oneNum; i++) {
newChar[p++] = '1';
}
while (!q.isEmpty()) {
newChar[p++] = q.poll();
}
System.out.println(new String(newChar));
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
// stackSimulation(s);
// twoAsSpliter(s);
countWay(s);
}
C. Annoying Present
题目较为简单,等差数列求和与二次函数求最值稍微推一下就可以出来. 唯一需要注意的就是精度问题,根据输入数据量可以推出longlong先加不会爆,故可先累加最后再除n
/**
* 输入n,m 以及m个 xi,di
* avg(变换后)-avg(原来的) = Xi+Di*( (n*n+n)/2 + i*i - (n+1)*i )/n
* 故后半段括号内最小值为 (n+1)/2 , 最大值为1或n
* 仅需根据Di正负取值即可
* 注意精度问题,n可最后再除
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n, m, xi, di;
n = scan.nextInt();
m = scan.nextInt();
long avg = 0;
for (int i = 0; i < m; i++) {
xi = scan.nextInt();
di = scan.nextInt();
avg += xi * n;
if (0 == di) {
} else if (di > 0) {
avg += afterDiMultiply(n, di, 1);
} else {
avg += afterDiMultiply(n, di, ((n + 1) >> 1));
}
}
System.out.println(String.format("%.15f", (double) avg / n));
}
public static long afterDiMultiply(long n, long d, long i) {
return d * ((n * n + n) / 2 + i * (i - 1 - n));
}
D. Relatively Prime Graph
这道题因为给的m范围太小导致暴力枚举可直接过....
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt(), m = scan.nextInt();
int[][] ansArr = new int[m][2];
int ansCount = 0;
if (m < n - 1) {
System.out.println("Impossible");
return;
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (gcd(i, j) == 1) {
ansArr[ansCount][0] = i;
ansArr[ansCount++][1] = j;
if (ansCount >= m) {
System.out.println("Possible");
for (int k = 0; k < ansCount; k++) {
System.out.println(ansArr[k][0] + " " + ansArr[k][1]);
}
return;
}
}
}
}
System.out.println("Impossible");
}
public static int gcd(int a, int b) {
return (a == 0) ? b : gcd(b % a, a);
}