o0w0o / ARTS

ARTS 鸽友打卡🐦
2 stars 0 forks source link

CF582 #101

Open hitolz opened 5 years ago

hitolz commented 5 years ago

https://codeforces.com/contest/1213/problem/A A题 题目内容: 说一条直线上,有n个碎片,你有两种方式移动碎片 1、往左或者往右移动2个单位,免费 2、往左或者往右移动1个单位,需要一个硬币 问,把这些碎片移动到相同的位置最少需要多少硬币。

思路: 移动偶数个单位不需要硬币,移动单数个碎片需要硬币, 就碎片的位置而言,偶数到偶数不花钱,奇数到奇数不花钱,奇数到偶数花钱 * 1。 那就是看看数组中有多少个奇数、多少个偶数,取最小的。


import java.util.Scanner;

public class Cf582A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] x = new int[n];

        int countOdd = 0;
        int countEven = 0;

        int min = 0;
        for (int i = 0; i < n; i++) {
            x[i] = scanner.nextInt();
            if (x[i] % 2 == 0) {
                countEven++;
            } else {
                countOdd++;
            }
        }

        System.out.println(countOdd < countEven ? countOdd : countEven);

    }
}

https://codeforces.com/contest/1213/problem/B 题目内容: 说一部手机的价格随天数是不断变化的,当后面的价格有比今天的价格低的时候,认为今天的价格是bad。 问,n天中,价格是bad的天数。 思路: 1、TLE的思路:数组中后面的元素有比当前的元素小的情况,天数加1,双重for判断 2、从后往前,找最小的数,如果当前元素比最小数大,加1


import java.util.Scanner;

public class Cf582B {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt();
            int[] a = new int[n];
            int count = 0;
            for (int j = 0; j < n; j++) {
                a[j] = scanner.nextInt();
            }
// TLE
//          for (int j = 0; j < n - 1; j++) {
//              for (int k = j + 1; k < n; k++) {
//                  if (a[k] < a[j]) {
//                      count++;
//                      break;
//                  }
//              }

//          int max = a[0];
//          for (int j = 0; j < n; j++) {
//              max = Math.max(max, a[j]);
//              if (a[j] < max) {
//                  count++;
//              }
//          }

            int min = a[n - 1];
            for (int j = n - 1; j >= 0; j--) {
                min = Math.min(min, a[j]);
                if (a[j] > min) {
                    count++;
                }
            }

            System.out.println(count);
        }
    }
}