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

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);
}
}

CodeForces336 A & B的更多相关文章

随机推荐

  1. VB中PictureBox控件使用教程

    PictureBox对象可以说是任何对象的原始型态,它可以加载图片.显示文字.画图外,它还能与Frame对象一样,在自己本身里头加载其它的对象而自成一个小群组,用PictureBox可以仿真出任何对象 ...

  2. win7系统下python安装numpy,matplotlib,scipy和scikit-learn

    1.安装numpy,matplotlib,scipy和scikit-learn win7系统下直接采用pip或者下载源文件进行安装numpy,matplotlib,scipy时会遇到各种问题,这是因为 ...

  3. ubuntu

    mongoChef: http://3t.io/mongochef/download/core/platform/#tab-id-3 背景色改成豆沙绿: /usr/share/themes/Ambia ...

  4. c++虚析构函数

    虚析构函数的作用主要是当通过基类指针删除派生类对象时,调用派生类的析构函数(如果没有将不会调用派生类析构函数) #include <iostream> using namespace st ...

  5. ThinkInside

    #sideBar,#blog_post_info_block { display: none;}#under_post_news { display: none;} /*评论框大小*/ #tbComm ...

  6. Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium

    Xpath in selenium is close to must required. XPath is element locator and you need to provide xpath ...

  7. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  8. AnyImgUpload

    @{ ViewBag.Title = "ImgForAny"; Layout = null; } <h2>ImgForAny</h2> <script ...

  9. TCP/IP协议三次握手与四次握手流程解析

    原文链接地址:http://www.2cto.com/net/201310/251896.html TCP/IP协议三次握手与四次握手流程解析 TCP/IP协议的详细信息参看<TCP/IP协议详 ...

  10. yii 验证问题

    yii 版本2.08 yii 验证码问题 1.模型里加入'verifyCode', 'captcha','message'=>'error','captchaAction' => 'tes ...