section1.2主要包括5道题和1个编程知识介绍。下面对这6部分内容进行学习。

Complete Search

这个翻译成枚举搜索或者穷举搜索。主要用于当写代码时间不够用而且不用考虑程序的效率问题的时候。

这个方法简单易行,一般是做题目的首选,如果满足时间和空间的要求,那就这么做,把时间多出来去解决更难的问题。

需要注意的是,很多人经常不知不觉用了这个方法

Milking Cows

本题难度并不大,对输入数据进行排序是关键。后面的需要细心了。多测几次基本上能过。

/*
LANG: JAVA
TASK: milk2
*/ import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; public class milk2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("milk2.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("milk2.out")));
ArrayList<Schedule> schedules = new ArrayList<Schedule>();
int length = Integer.parseInt(br.readLine());
for (int i = 0; i < length; i++) {
String[] timeInfo = br.readLine().split(" ");
Schedule oneSchedule = new Schedule(Integer.parseInt(timeInfo[0]), Integer.parseInt(timeInfo[1]));
schedules.add(oneSchedule);
}
Collections.sort(schedules, new MyCompare());
int start = schedules.get(0).start;
int end = schedules.get(0).end;
int intervalMilked = end - start;
int intervalNotMilked = 0; for (int i = 1; i < schedules.size(); i++) {
if (schedules.get(i).start > end) {
int newIntervalNotMilked = schedules.get(i).start - end;
intervalNotMilked = Math.max(intervalNotMilked, newIntervalNotMilked);
start = schedules.get(i).start;
end = schedules.get(i).end; } else if (schedules.get(i).start <= end) {
int newIntervalMilked = schedules.get(i).end - start;
intervalMilked = Math.max(newIntervalMilked, intervalMilked);
end = Math.max(end, schedules.get(i).end);
}
} pw.println(intervalMilked + " " + intervalNotMilked);
pw.close();
br.close();
} } class Schedule {
public int start;
public int end;
public Schedule (int start, int end) {
this.start = start;
this.end = end;
}
} class MyCompare implements Comparator<Schedule> {
public int compare(Schedule a, Schedule b) {
if (a.start > b.start) return 1;
else if (a.start < b.start) return -1;
else return 0;
}
}

Transformations

这小节果然是穷举法啊。这题也没什么好说的,把所有情况都检查一下,考察代码能力多于算法能力。

/*
LANG: JAVA
TASK: transform
*/
import java.io.*;
public class transform {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("transform.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("transform.out")));
//initialization
int n = Integer.parseInt(br.readLine());
int[][] before = new int[n][n];
int[][] after = new int[n][n];
for (int i = 0; i < n; i++) {
String line = br.readLine();
for (int j = 0; j < line.length(); j++) {
before[i][j] = line.charAt(j);
}
}
for (int i = 0; i < n; i++) {
String line = br.readLine();
for (int j = 0; j < line.length(); j++) {
after[i][j] = line.charAt(j);
}
} if (clockwise90(before, after)) pw.println("1");
else if (clockwise180(before, after)) pw.println("2");
else if (clockwise270(before, after)) pw.println("3");
else if (reflection(before, after)) pw.println("4");
else if (combination(before, after)) pw.println("5");
else if (noChange(before, after)) pw.println("6");
else pw.println("7"); pw.close();
br.close();
} public static boolean clockwise90(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[j][n - 1 - i]) return false;
}
}
return flag;
} public static boolean clockwise180(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[n - 1 - i][n - 1 - j]) return false;
}
}
return flag;
} public static boolean clockwise270(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[n- 1- j][i]) return false;
}
}
return flag;
} public static boolean reflection(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[i][n - 1 - j]) return false;
}
}
return flag;
} public static boolean combination(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
int[][] newBefore = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newBefore[i][j] = before[i][n - 1 - j];
}
}
if (clockwise90(newBefore, after) || clockwise180(newBefore, after) || clockwise270(newBefore, after)) {
return true;
}
else {
flag = false;
}
return flag;
} public static boolean noChange (int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[i][j]) return false;
}
}
return flag;
}
}

Name That Number

这个题目要是直接枚举空间复杂度就大了,一个小技巧就是先对dict.txt处理成哈希表,用一个数字映射多个字符串,通过目标数字,得到这一列字符串。

有一个需要注意的问题是,哈希表里面的key要设为Long类型,因为有些字符串太长了无法解析成Integer。

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects; /*
ID: jameszh1
LANG: JAVA
TASK: namenum
*/
public class namenum {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("namenum.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("namenum.out")));
BufferedReader brDict = new BufferedReader(new FileReader("dict.txt")); //initialization
int[] letterValue = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
long num = Long.parseLong(br.readLine());
String line = null;
HashMap<Long, ArrayList<String>> dict = new HashMap<>();
while ((line = brDict.readLine()) != null) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < line.length(); i++) {
sb.append(String.valueOf(letterValue[(line.charAt(i) - 'A')]));
}
if (dict.containsKey(Long.parseLong(sb.toString()))) {
dict.get(Long.parseLong(sb.toString())).add(line);
} else {
ArrayList al = new ArrayList();
al.add(line);
dict.put(Long.parseLong(sb.toString()), al);
}
} if (dict.containsKey(num)) {
ArrayList al = dict.get(num);
for (Object s: al) {
pw.println(s);
}
}
else {
pw.println("NONE");
} pw.close();
br.close(); }
}

Palindromic Squares

看上去是一到很简单的题目,难度其实在于进制的任意转换。不过也可以用java内置的函数BigInteger.toString()。

import java.io.*;
import java.math.BigInteger; /*
LANG: JAVA
TASK: palsquare
*/
public class palsquare {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("palsquare.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("palsquare.out"))); int base = Integer.parseInt(br.readLine());
for (int i = 1; i <= 300; i++) {
BigInteger b = new BigInteger(String.valueOf(i), 10);
BigInteger bSquare = new BigInteger(String.valueOf(i*i), 10);
if (isPalindromic(bSquare.toString(base))) {
pw.println(b.toString(base).toUpperCase() + " " + bSquare.toString(base).toUpperCase());
}
} pw.close();
br.close();
} public static boolean isPalindromic(String s) {
StringBuffer sb = new StringBuffer(s);
if (sb.toString().equals(sb.reverse().toString())) return true;
else return false;
}
}

Dual Palindromes

这个题做的郁闷。题目要求里有S的范围,看了测试用例才知道是起始点的范围,并不是所有数的搜寻范围。本来以为时间复杂度不会通过的,结果直接穷举也通过了。

import java.io.*;
import java.math.BigInteger; /*
LANG: JAVA
TASK: dualpal
*/
public class dualpal {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("dualpal.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dualpal.out"))); String[] input = br.readLine().split(" ");
int number = Integer.parseInt(input[0]);
int start = Integer.parseInt(input[1]);
int i = start + 1;
while (number > 0) {
BigInteger b = new BigInteger(String.valueOf(i), 10);
int count = 0;
for (int base = 2; base <= 10; base++) {
String s = b.toString(base);
if (isPalindromic(s) && number > 0) {
count++;
if (count >= 2) {
pw.println(b);
number--;
break;
}
}
}
i++;
} pw.close();
br.close();
} public static boolean isPalindromic(String s) {
StringBuffer sb = new StringBuffer(s);
if (sb.toString().equals(sb.reverse().toString())) return true;
else return false;
}
}

USACO Section1.3的更多相关文章

  1. USACO Section1.2

    section1.1主要包括四道题和两个编程知识介绍.下面将对这6个部分内容进行学习. Your Ride Is Here 这道题没什么难度,读懂题目意思就行:把两个字符串按照题目要求转换成数字,然后 ...

  2. USACO Section1.1

    本系列博客主要学习和记录USACO的相关代码和总结,附上我的github地址. 什么是USACO USACO全称是The USA Computing Olympiad,主要目的是从美国高中生中选出代码 ...

  3. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  4. USACO Section1.4 Mother's Milk 解题报告

    milk3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  5. USACO Section1.3 Wormholes 解题报告

    wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...

  6. USACO Section1.2 Name That Number 解题报告

    namenum解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

  7. USACO Section1.1 Friday the Thirteenth 解题报告

    friday解题报告 —— icedream61 博客园(转载请注明出处) -------------------------------------------------------------- ...

  8. USACO section1.1 Broken Necklace

    /* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...

  9. USACO section1.2 Miking cows

    /* ID: vincent63 LANG: C TASK: milk2 */ #include <stdio.h> #include<stdlib.h> #include&l ...

随机推荐

  1. [控件] LiveChangedImageView

    LiveChangedImageView 效果 说明 切换图片的时候自动根据图片的尺寸进行渐变式切换,效果很不错,使用非常容易. 源码 https://github.com/YouXianMing/U ...

  2. Linux架构之简述企业网站简述

    简述企业网站 用户  --> 负载均衡服务器(Nginx)  ->根据扩展名访问不同的服务区 ->访问数据库 ->返回用户          静态服务器&&动态 ...

  3. Git使用本地仓库之基本操作

    1.Git是什么? 一个分布式版本控制系统,和SVN类似,但远比SVN强大的一个版本控制系统 ①Git可以方便的在本地进行版本管理,如同你本地有一个版本管理服务器一样我们可以选择在合适的时间将本地版本 ...

  4. Nginx 泛解析配置请求映射到多端口实现二级域名访问

    由于想实现一个域名放置多个应用运行的目的,而不想通过域名后加端口号方式处理,这种方式处理记起来太麻烦,偷懒党简直不能忍,故而考虑了使用二级域名来处理多个应用同时运行.Google了一番资料并进行了尝试 ...

  5. IIS 7 反向代理 URL重写 转发动态请求

    一.反向代理是什么 有一篇文章说的挺好的 Nginx 反向代理.负载均衡.页面缓存.URL重写及读写分离详解 http://www.server110.com/nginx/201402/5534.ht ...

  6. 【C#】#102 发送邮件

    项目需求:定时的发送邮件,于是学习了如何发送邮件 下面有一个简单的例子.能够实现简单的发送邮件,加上附件可以添加一个属性[Attachment],然后配置上附件的路径 Demo下载 代码总共只有一下这 ...

  7. python第十三课——嵌套循环

    2.嵌套循环: 概念:循环中再定义循环,称为嵌套循环: [注意]嵌套循环可能有多层,但是一般我们实际开发最多两层就可以搞定了(99%的情况) 格式: 1).while中套while常用 2).whil ...

  8. TensorFlow函数(八)tf.control_dependencies()

    tf.control_dependencies(control_inputs) 此函数指定某些操作执行的依赖关系 返回一个控制依赖的上下文管理器,使用 with 关键字可以让在这个上下文环境中的操作都 ...

  9. HBase学习之路 (五)MapReduce操作Hbase

    MapReduce从HDFS读取数据存储到HBase中 现有HDFS中有一个student.txt文件,格式如下 95002,刘晨,女,19,IS 95017,王风娟,女,18,IS 95018,王一 ...

  10. SQL Server 断开某个数据库所有连接(还原的时候需要)

    问题描述: SQL Server数据库备份还原后,在数据库名称后会出现“受限制访问”字样 解决办法: 右键点击数据库 -> 属性 -> 选项 -> 状态 -> 限制访问 -&g ...