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 ...
随机推荐
- Python学习---IO的异步[twisted模块]
安装twisted模块 Linux: pip3 install twisted Window: a. http://www.lfd.uci.edu/~gohlke/pythonlibs/#twiste ...
- Linux which/whereis/locate命令详解
which 查看可执行文件的位置,从全局环境变量PATH里面查找对应的路径,默认是找 bash内所规范的目录 whereis 查看文件的位置,配合参数-b,用于程序名的搜索,从linux数据库查找. ...
- (转)Matlab增加块注释
1)方法一选中你要加注释的内容,然后选择工具菜单“text|comment”就可以了,如果要把注释变为语句,同样选中要转变的语句,然后用鼠标选择“text|uncomment”就可以了.用键盘的快捷键 ...
- tomcat7换端口号调试
1.C:\tomcat\conf\server.xml中修改端口号 2.C:\tomcat\bin\startup.bat批处理文件启动tomcat 3.用ctrl+c结束批处理文件 4.调试结束
- zabbix日常监控Apache2.4
Apache的安装请参考https://www.cnblogs.com/huangyanqi/p/9168637.html 1.修改配置 [root@apache ~]# httpd -v Serve ...
- 一、Linux概述 二、Linux的安装 三、Linux的常用命令(重点)
一.Linux概述###<1>操作系统 OS,管理和控制 计算机的 硬件和软件资源的 计算机程序. 最基本的系统软件. 是用户和计算机交互的桥梁,是硬件和软件交互的桥梁. 操作系统:she ...
- 个人作业2:APP案例分析--腾讯动漫
第一部分 调研,评测 个人第一次上手体验 以往看漫画就是在浏览器直接搜索在网页上看,直到用了腾讯动漫APP,我才摒弃这个很low的方法.腾讯动漫直接用qq就可以登陆,有更齐全的漫画分类,更清晰的画质, ...
- Echarts 多曲线“断点”问题解决方法
Echarts 用来做可视化曲线是非常优秀的一个库.建议使用 Echarts 作为项目的可视化图标库时,仔细研究 官方实例,根据需求来选择类似的示例,下载实例模板来开发,节省时间,减少出错,提高效率. ...
- JWinner:一个私人定制的快速开发框架,为理想而生
关于JWinner JWinner是一个JAVA项目的快速开发框架,他已经实现了大多数项目开发之前需要进行的一些必备工作,还有很多在开发过程中可能会用到的工具集. JWinner的诞生并不是一蹴而就的 ...
- WebForm下的$.ajax中contentType: “application/json” 的用法
不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...