• CodeForces336 A & B


    第一题就是排序然后计算一下时间。没什么

    package codeforces336;
    
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class MainA {
        public static void sortArr(int[][] arr, int n) {
            int[] tmp = new int[2];
            for(int i = 0; i < n; ++i) {
                for(int j = i+1; j < n; ++j) {
                    if(arr[i][0] < arr[j][0]) {
                        tmp[0] = arr[i][0];
                        tmp[1] = arr[i][1];
                        arr[i][0] = arr[j][0];
                        arr[i][1] = arr[j][1];
                        arr[j][0] = tmp[0];
                        arr[j][1] = tmp[1];
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            int n, s;
            int[][] arr = new int[105][2];
            Scanner sc = new Scanner(new InputStreamReader(System.in));
            n = sc.nextInt();
            s = sc.nextInt();
            for(int i = 0; i < n; ++i) {
                arr[i][0] = sc.nextInt();
                arr[i][1] = sc.nextInt();
            }
    
            sortArr(arr, n);
            int t = 0;
            int[] tmp = new int[2];
            for(int i = 0; i < n; ++i) {
                tmp[0] = s - arr[i][0];
                tmp[1] = arr[i][1];
                if(tmp[1] < (t + tmp[0])){
                    t += tmp[0];
                } else {
                    t = tmp[1];
                }
                s = arr[i][0];
            }
            if(s != 0) {
                t += s;
            }
            System.out.println(t);
        }
    }
    

     第二题,暴力肯定TLE,用前缀和算可以。看a的每一位,是0,统计在 lenb - lena + i  ~ i - 1 范围内 1的 个数;是 1,统计在 lenb - lena + i  ~ i - 1 范围内 0 的 个数

    package codeforces336;
    
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    /**
     * Created by lenovo on 2016-01-28.
     */
    public class MainB {
        public static void main(String[] args) {
            String a;
            String b;
            Scanner sc = new Scanner(new InputStreamReader(System.in));
            a = sc.nextLine();
            b = sc.nextLine();
            //System.out.println(a + " " + b);
            long[][] pre = new long[200005][2];
            int lena = a.length();
            int lenb = b.length();
    
            for(int i = 1; i <= lenb; ++i) {
                if(b.charAt(i-1) == '1'){
                    pre[i][1] = pre[i-1][1] + 1;
                    pre[i][0] = pre[i-1][0];
                }else {
                    pre[i][0] = pre[i-1][0] + 1;
                    pre[i][1] = pre[i-1][1];
                }
            }
            long  ans = 0;
            for(int i = 1; i <= lena; ++i) {
                if(a.charAt(i-1) == '0') {
                    ans += pre[lenb - lena + i][1] - pre[i-1][1];
                } else {
                    ans += pre[lenb - lena + i][0] - pre[i-1][0];
                }
            }
            System.out.println(ans);
        }
    }
    
  • 相关阅读:
    CSU 1505: 酷酷的单词【字符串】
    HDU 2036 改革春风吹满地【计算几何/叉乘求多边形面积】
    HDU 2034 人见人爱A-B【STL/set】
    HDU 2031 进制转换
    HDU 1020 Encoding【连续的计数器重置】
    HDU 1999 不可摸数【类似筛法求真因子和】
    动态规划总结
    CSU 1785: 又一道简单题
    CSU 1779: 错误的算法【矩阵/模拟】
    CSU 1777: 大还是小?【模拟/后导0】
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/5165721.html
Copyright © 2020-2023  润新知