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 ...
随机推荐
- C++项目规范
https://segmentfault.com/a/1190000007659754
- NoSQL数据库---NoSQL数据库的分类
NoSQL数据库的分类 个人理解一下文档型数据库: 就是键值对数据库的升级,允许键值之间嵌套键值,比如JSON格式的数据.效率比key_value的数据库更高. [更多参考]http://www.cn ...
- Windows 7 添加快速启动栏
1.右击任务栏空白处,选择 “工具栏” ,单击 “新建工具栏” 2.输入 以下路径: %userprofile%\AppData\Roaming\Microsoft\Internet Explorer ...
- LVS 原理(调度算法、四种模式、四层负载均衡和七层 的区别)
参考文档:http://blog.csdn.net/ioy84737634/article/details/44916241 目录 lvs的调度算法 lvs的四种模式 四层均衡负载和七层的区别 1.l ...
- django 错误之 OSError: mysql_config not found
pip 导入包时出现如下错误 Complete output from command python setup.py egg_info: /bin/: mysql_config: not found ...
- CentOS7 防火墙(firewall)的操作命令
CentOS7 防火墙(firewall)的操作命令 安装:yum install firewalld 1.firewalld的基本使用 启动: systemctl start firewalld 查 ...
- Weblogic web应用中获取文件的绝对路径
注意点: 1. file必须在/下,或者/WEB-INF/,不能在classes下 2. weblogic中进行如下配置,以获取绝对路径: <wls:container-descriptor&g ...
- 【原创】Python 对象创建过程中元类, __new__, __call__, __init__ 的处理
原始type: type是最原始的元类,其__call__方法是在你使用" t_class = type(classname_string, base_classes_tuple, attr ...
- HNOI2018退役记
不想记流水账了,总结一下考炸的原因吧.. $day1$: $12$点才知道$t3$怎么做. 可以用容斥+动态$dp$来搞,但是没时间写了. 事实上这个方法也比较复杂,标算比这优美多了. 所以还是想得太 ...
- 线性dp
线性dp应该是dp中比较简单的一类,不过也有难的.(矩乘优化递推请出门右转) 线性dp一般是用前面的状态去推后面的,也有用后面往前面推的,这时候把循环顺序倒一倒就行了.如果有的题又要从前往后推又要从后 ...