USACO Section1.3
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的更多相关文章
- USACO Section1.2
section1.1主要包括四道题和两个编程知识介绍.下面将对这6个部分内容进行学习. Your Ride Is Here 这道题没什么难度,读懂题目意思就行:把两个字符串按照题目要求转换成数字,然后 ...
- USACO Section1.1
本系列博客主要学习和记录USACO的相关代码和总结,附上我的github地址. 什么是USACO USACO全称是The USA Computing Olympiad,主要目的是从美国高中生中选出代码 ...
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.4 Mother's Milk 解题报告
milk3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section1.3 Wormholes 解题报告
wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...
- USACO Section1.2 Name That Number 解题报告
namenum解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
- USACO Section1.1 Friday the Thirteenth 解题报告
friday解题报告 —— icedream61 博客园(转载请注明出处) -------------------------------------------------------------- ...
- USACO section1.1 Broken Necklace
/* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...
- USACO section1.2 Miking cows
/* ID: vincent63 LANG: C TASK: milk2 */ #include <stdio.h> #include<stdlib.h> #include&l ...
随机推荐
- ORA-28547:(Navicat Premium连接oracle报错)
1.背景 Navicat Premium连接oracle报 ORA-28547:connection to server failed, probable Oracle Net admin errro ...
- Redis常用指令
1.使用指令存储数据 不同数据类型的使用 1.String > 在以上指令中我们使用set指令向redis存进了一个数据类型为string,名为str1,值为123456.(如果你要问为什么,那 ...
- 汽车学习---汽车知识大全【all】
汽车驱动/发动机/大灯 侧方位停车和倒车入库技巧 手动挡车型 换档8大技巧 常见的掉头情况注意事项 最全的左转,右转,待转,掉头方法 开车技巧大汇总,尤其适合新手司机 汽车常识扫盲,不做“汽车小白”! ...
- Python学习---IO的异步[tornado模块]
tornado是一个异步非阻塞的WEB框架.它的异步非阻塞实际上就是用事件循环写的. 主要体现在2点: 1. 作为webserver可以接收请求,同时支持异步处理请求.Django只能处理完成上一个请 ...
- JAVA入门之基础语言
在上一章文章中,读者已经了解了如何搭建Java 开发环境及Java 程序的开发过程.从本篇文章开始讲解Java 的基本语言.这些基本语言的语法和其他一些编程语言相比有些是类似的,但还有很多不同之处,读 ...
- September 14th 2017 Week 37th Thursday
Don't let the past steal your present. 别让过去悄悄偷走了我们的当下. We take what we can get and make the best of ...
- 【3】python中如何生成随机数的几个例子
#__author:"吉勇佳" #date: 2018/10/14 0014 #function: import math import random # 向上取整 print(m ...
- nodejs11安装教程(升级最新版本)
nodejs需要不断升级,那么电脑如何安装nodejs11呢,下面将通过亲身实践来详细介绍 工具/原料 电脑 nodejs11安装包 方法/步骤 访问node11官网,下载安装包,如下 ...
- python第十二课——for in循环
1.for...in循环: 有两个使用场景: 场景一:for in和range对象配合使用 range对象的引入讲解 格式:range([start,end,step]): 特点:索引满足含头不含尾的 ...
- 清除SQL server 记住的用户名和密码
公司更换电脑,清除SQL server 记住的用户名和密码 请按照上图中的位置找到相应的文件SqlStudio.bin,然后把它删除,请放一百个心,这个文件会自动生成的. 需要注意的是,在删除之前一定 ...