USACO Section1.2
section1.1主要包括四道题和两个编程知识介绍。下面将对这6个部分内容进行学习。
Your Ride Is Here
这道题没什么难度,读懂题目意思就行:把两个字符串按照题目要求转换成数字,然后对47取模,比较是否相等。
代码如下
/*
LANG: JAVA
TASK: ride
*/
import java.io.*;
import java.util.*;
public class ride {
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("ride.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ride.out")));
Scanner in = new Scanner(f);
String comet = null;
String group = null;
while (in.hasNextLine()) {
comet = in.nextLine();
group = in.nextLine();
}
if (caculate(group) == caculate(comet)) {
out.println("GO");
}else {
out.println("STAY");
}
out.close(); // close the output file
}
public static int caculate(String str) {
int size = str.length();
int result = 1;
for (int i = 0; i < size; i++) {
result = result * (str.charAt(i) - 'A' + 1);
}
return result % 47;
}
}
Programming Contest Problem Types
这个内容介绍了编程比赛中的题目类型,Hal Burch有研究发现:编程比赛里面只有16种比赛类型。这16种比赛类型的前几个几乎占了IOI比赛题目的80%。
这16种题型分别是:
- Dynamic Programming
- Greedy
- Complete Search
- Flood Fill
- Shortest Path
- Recursive Search Techniques
- Minimum Spanning Tree
- Knapsack
- Computational Geometry
- Network Flow
- Eulerian Path
- Two-Dimensional Convex Hull
- BigNums
- Heuristic Search
- Approximate Search
- Ad Hoc Problems
稍微复杂一点的程序就结合例上述多个类型,所以呀,想把USACO刷完,还是挺不容易的。
Ad Hoc Problems
这种问题也叫“临时问题”,是上面16种类型中最后一种。它不属于标准的,具有精心研究的解决方案,反而是每个问题都没有特定的方法来解决。
这种问题一般需要仔细阅读,也需要一些合理的优化和一些分析。解决方案可能需要一个新颖的数据结构等。
Greedy Gift Givers
这道题就是典型的Ad Hoc Problems问题。题目的大概意思是分礼物。难点在于读懂题目和如何读数据。难度倒是不大,代码如下
/*
LANG: JAVA
TASK: gift1
*/
import java.io.*;
import java.util.*;
public class gift1 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("gift1.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
int size = Integer.parseInt(br.readLine());
String[] names = new String[size];
HashMap<String, Integer> net = new HashMap<String, Integer>();
for (int i = 0; i < size; i++) {
String name = br.readLine();
net.put(name, 0);
names[i] = name;
}
for (int i = 0; i < size; i++) {
String name = br.readLine();
String gift = br.readLine();
String[] temp = gift.split(" ");
int sum = Integer.parseInt(temp[0]);
int div = Integer.parseInt(temp[1]);
if (div != 0) {
for (int j = 0; j < div; j++) {
String target = br.readLine();
net.put(target, net.get(target) + sum / div);
}
net.put(name, net.get(name) - (sum - sum % div));
}
}
for (String nameToPrint : names) {
pw.println(nameToPrint + " " + net.get(nameToPrint));
}
pw.close();
br.close();
}
}
Friday the Thirteenth
这道题也是需要读懂题目。判断闰年和计算天数的函数比较简单,关键是最后输出的时候,数组里面的值分别对应星期几容易出错。
/*
LANG: JAVA
TASK: friday
*/
import java.io.*;
public class friday {
public static void main(String[] args) throws Exception {
int count[] = new int[7];
BufferedReader br = new BufferedReader(new FileReader("friday.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
int years = Integer.parseInt(br.readLine());
int allDays = 0;
for (int i = 1900; i < 1900 + years; i++) {
for (int j = 1; j <= 12; j++) {
count[(allDays + 13) % 7]++;
allDays += countDaysInMonth(j, i);
}
}
pw.println(count[6] + " " + count[0] + " " + count[1] + " " + count[2] + " " + count[3] + " " + count[4] + " " + count[5]);
pw.close();
br.close();
}
public static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
}else {
return false;
}
}
public static int countDaysInMonth(int month, int year) {
switch (month) {
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeapYear(year)) {
return 29;
}else {
return 28;
}
default:
return 31;
}
}
}
Broken Necklace
这个题算的上是secion1.1里最难的题了。题目也不太好读懂。看懂题目后直观感觉是暴力求解。这是我写的代码。
/*
LANG: JAVA
TASK: beads
*/
import java.io.*;
public class beads {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("beads.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("beads.out")));
int numOfBeads = Integer.parseInt(br.readLine());
String necklace = br.readLine();
necklace = necklace + necklace;
int maxTotal = 0;
for (int i = 1; i < necklace.length() - 1; i++) {
int total = 0;
char start = necklace.charAt(i);
char end = necklace.charAt(i - 1);
//找到有意义的起始点
for (int j = i; j < necklace.length() - 1; j++) {
if (necklace.charAt(j) == 'w') {
continue;
} else {
start = necklace.charAt(j);
break;
}
}
for (int j = i; j < necklace.length() - 1; j++) {
if (necklace.charAt(j) == 'w') {
total++;
} else {
if (necklace.charAt(j) == start) {
total++;
}else {
break;
}
}
}
//找到有意义的起始点
for (int j = i - 1; j >= 0; j--) {
if (necklace.charAt(j) == 'w') {
continue;
} else {
end = necklace.charAt(j);
break;
}
}
for (int j = i - 1; j >= 0; j--) {
if (necklace.charAt(j) == 'w') {
total++;
} else {
if (necklace.charAt(j) == end) {
total++;
}else {
break;
}
}
}
maxTotal = maxTotal > total?maxTotal:total;
}
maxTotal = maxTotal < numOfBeads?maxTotal:numOfBeads;
pw.println(maxTotal);
pw.close();
br.close();
}
}
如果要算复杂度的话,估计有4×N^2,可能比一般人写的更慢。提交通过后看了官网给的答案,有一个复杂度为O(n)的方法。
/*
LANG: JAVA
TASK: beads
*/
import java.io.*;
public class beads1 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("beads.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("beads.out")));
int numOfBeads = Integer.parseInt(br.readLine());
String necklace = br.readLine();
necklace = necklace + necklace;
int[][] left = new int[necklace.length() + 1][2];
int[][] right = new int[necklace.length() + 1][2];
left[0][0] = left[0][1] = 0;
for (int i = 1; i <= necklace.length(); i++) {
if (necklace.charAt(i - 1) == 'r') {
left[i][0] = left[i - 1][0] + 1;//第0列存r,假如为r,则加一
left[i][1] = 0;//第1列存b,假如为r,则置为0
} else if (necklace.charAt(i - 1) == 'b') {
left[i][1] = left[i - 1][1] + 1;
left[i][0] = 0;
} else {
left[i][0] = left[i - 1][0] + 1;
left[i][1] = left[i - 1][1] + 1;
}
}
right[necklace.length()][0] = right[necklace.length()][1] = 0;
for (int i=necklace.length() - 1; i >= 0; i--){
if (necklace.charAt(i) == 'r'){
right[i][0] = right[i + 1][0] + 1;//第0列存r,假如为r,则加一
right[i][1] = 0;//第1列存b,假如为r,则置为0
} else if (necklace.charAt(i) == 'b'){
right[i][1] = right[i + 1][1] + 1;
right[i][0] = 0;
} else {
right[i][0] = right[i + 1][0] + 1;
right[i][1] = right[i + 1][1] + 1;
}
}
int m = 0;
for (int i = 0; i < necklace.length(); i++) {
m = Math.max(m, Math.max(left[i][0], left[i][1]) + Math.max(right[i][0], right[i][1]));
}
m = Math.min(m, numOfBeads);
pw.println(m);
pw.close();
br.close();
}
}
USACO Section1.2的更多相关文章
- USACO Section1.3
section1.2主要包括5道题和1个编程知识介绍.下面对这6部分内容进行学习. Complete Search 这个翻译成枚举搜索或者穷举搜索.主要用于当写代码时间不够用而且不用考虑程序的效率问题 ...
- 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 ...
随机推荐
- Linux rpm命令详解
rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM ...
- 铁乐学Python_day06-整数和字符串小数据池
python小数据池(内存地址) 今天来学习认识一下python中的小数据池. 我们都知道 ==是用来作比较,要是两个变量的数值相等, 用==比较返回的bool值会是True: a = 1000 b ...
- Sailing
Sailing 目录 1基本信息 2歌曲简介 3歌词内容 4歌手简介 5专辑介绍 1基本信息编辑 歌曲: Sailing 所属专辑: Atlantic Crossing 艺人:Rod Stewart[ ...
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource【转】
本文转载仅供自己学习收录,不做任何商业用途,如有需要请访问原地址:http://blog.csdn.net/qyf_5445/article/details/8124431 ApplicationCo ...
- 页面中图片以背景图形式展示好还是以img标签形式展示
img和background-image的异同: img是网页结构层面上的标签,页面中多一个img标签就会多一次http请求,且当我们浏览页面时,img标签作为网页结构的一部分,会在浏览器加载结构的过 ...
- JavaScript设计模式—工厂模式
工厂模式介绍 将new操作符单独进行封装,遇到new时,就要考虑是否该使用工厂模式 举一个生活当中的示例: 你要去购买汉堡,直接点餐,取餐,不会自己动手做,商店要“封装” 做汉堡的工作,做好直接给购买 ...
- P4906 小奔关闹钟
题目背景 由于今天是星期一,闹钟准时响了,由于小奔太困了,所以她想关停闹钟. 题目描述 可是,他的闹钟电路太复杂了,有很多个开关,每个开关都连着其他开关,其他开关又连着更多的开关,当且仅当所有开关都关 ...
- virtualbox+vagrant学习-2(command cli)-4-vagrant global-status命令
Global Status 格式: vagrant global-status 这个命令将告诉你当前登录的用户系统上所有活跃的vagrant环境的状态. userdeMacBook-Pro:~ use ...
- 说说Javac
Java语言有Java语言的规范,,这个规范详细描述了Java语言有哪些词法.语法,而Java虚拟机也有其Java虚拟机的规范,同样Java虚拟机的规范和Java语言规范并不一样,它们都有自己的词法和 ...
- SSL证书更换(具体路径可参考iRedMail.tips文件)及邮件服务器架构
由于Google的chrome不认可赛门铁克的证书,因此要更换新证书 0.8.6版本 0.9.4版本 - /etc/pk ...