牛客寒假算法基础集训营3
只会4题,真难受
题目链接:https://ac.nowcoder.com/acm/contest/329/C
来源:牛客网
C:处女座点名
做法:照题意模拟,因为保证严格递增,所以只要遍历for循环一次就行。
AC代码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[] a = new int[n + 5];
for (int i = 1; i <= n - 1; ++i)
a[i] = cin.nextInt();
for (int i = 1; i <= n - 1; ++i)
{
if (i != n - 1)
{
if (a[i] + 1 != a[i])
System.out.println(a[i] + 1);
}
}
}
}
D.处女座的训练
题目链接:https://ac.nowcoder.com/acm/contest/329/D
来源:牛客网
做法:贪心,这题最一开始一直想着用疲倦值排序,手写cmp完成,可是一直WA…,后面听大佬说是用“交换相邻位的贪心”,也就是设两个任务为a,b,即a.x∗b.y和b.x∗a.y对比,值较小的后做。
核心代码就是 return a.time * b.pijuan - b.time * a.pijuan;即可
AC代码:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class train
{
int time;
int pijuan;
}
class cmptr implements Comparator<train>
{
public int compare(train a, train b)
{
return a.time * b.pijuan - b.time * a.pijuan;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
train[] tr = new train[n + 5];
long cur = 0;
for (int i = 0; i < n; ++i)
{
tr[i] = new train();
tr[i].time = cin.nextInt();
tr[i].pijuan = cin.nextInt();
cur += tr[i].pijuan;
}
cin.close();
Arrays.sort(tr, 0, n, new cmptr());
long sum = 0;
for (int i = 0; i < n; ++i)
{
cur -= tr[i].pijuan;
sum += tr[i].time * cur;
}
System.out.println(sum);
}
}
E:处女座和小姐姐
题目链接:https://ac.nowcoder.com/acm/contest/329/E
来源:牛客网
做法:貌似类似蛇形填数,注意行列别搞混就行
AC代码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while (t-- > 0)
{
int cnt = 1;
int n = cin.nextInt();
int m = cin.nextInt();
int[][] a = new int[1005][1005];
for (int i = 0; i < n; ++i)
{
int q = m - 1;
for (int j = 0; j < m; ++j)
{
if (i % 2 == 0)
a[i][j] = cnt++;
else
a[i][q--] = cnt++;
}
}
System.out.print(a[n - 1][m - 1] - 2);
}
}
}
I.处女座的约会
题目链接:https://ac.nowcoder.com/acm/contest/329/I
来源:牛客网
AC代码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for (int i = 0; i < n; ++i)
System.out.println("cnznb");
}
}