USACO section 1.1 C++题解
USACO section1.1:
DONE 2017.03.03 TEXT Submitting Solutions 
DONE 2017.03.04 PROB Your Ride Is Here [ANALYSIS] 
DONE 2017.03.04 TEXT Contest Problem Types 
DONE 2017.03.04 TEXT Ad Hoc Problems 
DONE 2017.03.04 PROB Greedy Gift Givers [ANALYSIS] 
DONE 2017.03.04 PROB Friday the Thirteenth [ANALYSIS] 
DONE 2017.03.04 PROB Broken Necklace [ANALYSIS]
由上可见section1.1中共有四道题,没有考察算法,只是单纯看思考是否到位以及细心程度。
Prob1:Your Ride Is Here(水题) 
题意:input给出两个全部由大写字母组成的单词, 每个字母对应一个数字(A-1,B-2……),
两个单词中的字母分别相乘,得到两个数字, 判断这两个数字%47的余数是否相等。
NOTE:由于数据很小,无须担心溢出,但是保险起见,还是在每次运算的过程中取余。(a%b)*(c%b) = (a*c)%b;
源代码:
/*
ID:kongse_1
PROG:ride
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MOD = ;
string a, b;
int num1= ,num2=;
int main()
{
freopen("ride.in", "r", stdin);
freopen("ride.out", "w", stdout);
cin >> a >> b;
for(int i = ; i != a.size(); ++i)
num1 = (num1*(a[i]-'A'+))%MOD;
for(int i = ; i != b.size(); ++i)
num2 = (num2*(b[i]-'A'+))%MOD;
if(num1 == num2) cout << "GO" << endl;
else cout << "STAY" << endl;
fclose(stdin);
fclose(stdout);
return ;
}
Prob2:Greedy Gift Givers (水题)
题意:有n个人,没人开始身上有0块钱,每个人选择掏出x的钱给他指定的n个人买礼物,他指定的人每个人得到x/n块钱,若除不尽(x%n)则多出来的零钱归他自己。输出一轮之后每个人对应的钱数(可以是负的...)。
P.S:本来开始想要用map做一一映射,结果map自带排序,把原来输入的顺序搞没了,于是乎只能拿俩数组维护。
源代码:
/*
ID:kongse_1
PROG:gift1
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
string x[maxn], y[maxn], z;
int num, sum, n, money[maxn];
int find_(string curr)
{
for(int i = ; i != num; ++i)
{
if(x[i] == curr) return i;
}
}
void calculate_(int a, int b)
{
money[a] -= b;
return ;
}
int main(){
freopen("gift1.in", "r", stdin);
freopen("gift1.out", "w", stdout);
cin >> num;
for(int i = ; i != num; ++i)
{
cin >> x[i];
}
for(int i = ; i != num; ++i)
{
cin >> z >> sum >> n;
if(n)
{
calculate_(find_(z), sum-sum%n);
for(int j = ; j != n; ++j)
{
cin >> y[j];
calculate_(find_(y[j]), -sum/n);
}
}
}
for(int i = ; i != num; ++i)
{
cout << x[i] << " " << money[i] << endl;
}
fclose(stdin);
fclose(stdout);
return ;
}
Prob3:Friday the Thirteenth
题意:已知1900年1月1日是周一,统计包括1900年在内的N年间,每月13号是周一——周日的次数。
NOTE1:得到1900年1月13日是周六,根据每个月的天数算出下一个月13日是星期几,注意判断闰年即可。
NOTE2:如果按照每年作为单位一步步求,会出现一个问题:每一年算的第12次实际上算的是下一年的1月13日(+31即是度过了12月份到了一月),所以算的最后一年的循环要特判。
源代码:
/*
ID:kongse_1
PROG:friday
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
int month[maxn]={,,,,,,,,,,,,}, N, times[maxn], curr = ;
int whether(int year)
{
if(year% == || (year% == && year% != )) return ;
return ;
}
void caculate_(int year)
{
if(year == +N) return ;
if(whether(year)) month[] = ;
else month[] = ;
for(int i = ; i != ; ++i)
{
curr = (curr+month[i])%;
if(i == && year == +N-) break;
++times[curr];
}
caculate_(year+);
}
int main()
{
freopen("friday.in", "r", stdin);
freopen("friday.out", "w", stdout);
cin >> N;
++times[];
caculate_();
cout << times[] << " ";
for(int i = ; i != ; ++i)
{
cout << times[i];
if(i != ) cout << " ";
}
cout << endl;
fclose(stdin);
fclose(stdout);
return ;
}
Prob4:Broken Necklace(有点意思)
题意:一串项链有三种颜色:红(r),蓝(b),白(w)。从中间某一处断开,从断开的两边分别去从头取颜色相同的珠子(两边右的可摘取数,然后从两个序列中找到a[i]+b[i+1]最大值(因为是从中间断的两川,所以一定是相邻的且向左的是第i个,向右的是第(i+1)%n)个。(注意,这是一串项链,首尾相连,是个循环队列)。
而算出没一个的向左区和向右取的值也很简单,不需要每个都算,例如算出第i个可以向左取m个,那么第(i-1)个,即他的左边一定只能取(m-1)个(m!=1),m = 1即只有自己。当出现左边的颜色与当前颜色不同时再重新计算。但是有一个例外。
我们观察这样的一串:bbwbwrrwrwr 第一个b是5,第五个w则应该是1,但是事实上第五个应该是7,因为它跟右边的红色也可以相连。所以我们得到特判:当上一个是2并且当前是白色(w)时应重新计算(即w是某队队尾)。
源代码:
/*
ID:kongse_1
PROG:beads
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn = ;
string x;
int n, num[][maxn], max_;
int whether(char a, char b)
{
if(a+b == 'r'+'b') return ;
return ;
}
void calculate_(int curr, string y)
{
char last = x[curr];
if(y == "left")
{
int wh = ;
num[wh][curr] = ;
for(int i = (curr-+n)%n;i != curr ; i = (i-+n)%n)
{
if(x[i] == last || x[i] == 'w' || last == 'w')
{
if(last == 'w') last = x[i];
++num[wh][curr];
}
else break;
}
}
else
{
int wh = ;
num[wh][curr] = ;
for(int i = (curr+)%n; i != curr ; i = (i++n)%n)
{
if(x[i] == last || x[i] == 'w' || last == 'w')
{
if(last == 'w') last = x[i];
++num[wh][curr];
}
else break;
}
}
return ;
}
int main()
{
freopen("beads.in", "r", stdin);
freopen("beads.out", "w", stdout);
cin >> n >> x;
for(int i = ; i != n; ++i)
{
if( !i || (num[][(i-+n)%n] == && x[i] == 'w') || whether(x[(i-+n)%n],x[i]) )
calculate_(i, "right");
else num[][i] = num[][i-]-;
}
for(int i = n-; i != -; --i)
{
if( i == n- || (num[][(i+)%n] == && x[i] == 'w') || whether(x[(i+)%n],x[i]) )
calculate_(i, "left");
else num[][i] = num[][i+]-;
}
for(int i = ; i != n; ++i){
max_ = max(max_, num[][i]+num[][(i-+n)%n] );
}
cout <<((max_<n)?max_:n) << endl;
fclose(stdin);
fclose(stdout);
return ;
}
自此,USACO section1.1便全部完成了。
箜瑟_qi 2016.03.04
USACO section 1.1 C++题解的更多相关文章
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
		
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
 - [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)
		
nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...
 - bzoj usaco 金组水题题解(1)
		
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
 - USACO Section 3.3: Riding the Fences
		
典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...
 - USACO Section 3.3 Camlot(BFS)
		
BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...
 - USACO Section 5.3 Big Barn(dp)
		
USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1 (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...
 - USACO Section 1.3 Wormholes 解题报告
		
题目 题目描述 在一个二维平面上有N个点,这N个点是(N/2)个虫洞的端点,虫洞的特点就是,你以什么状态从某个端点进去,就一定会以什么状态从另一端的端点出来.现在有一头牛总是沿着与X轴正方向平行的直线 ...
 - USACO Section 1.3 Prime Cryptarithm 解题报告
		
题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...
 - USACO Section 1.1 Your Ride Is Here 解题报告
		
题目 问题描述 将字符串转变为数字,字母A对应的值为1,依次对应,字母Z对应的值为26.现在有一个字符串,将其中的每个字符转变为数字之后进行累乘,最终的结果对47求余数. 题目给你两个字符串,其中的字 ...
 
随机推荐
- 用js控制css属性
			
在用js控制css属性时,行内css属性可以任意控制,但若是在<style></style>中写的css属性,均不能用alert读取,但是赋值却有几种现象, 第一种:无法读取, ...
 - TensorFlow安装-windows系统
			
官方各版本的安装说明:https://www.tensorflow.org/install/ 本文介绍如何在windows环境下安装tensorflow, 跑起来简单的demo. 1.安装python ...
 - centos下美团sql优化工具SQLAdvisor的安装
			
1.克隆代码 cd /usr/local/src/git clone https://github.com/Meituan-Dianping/SQLAdvisor.git 2.安装依赖(ubuntu下 ...
 - jQuery插件之validation插件
			
前面的话 最常使用javascript的场合就是表单的验证,而jQuery作为一个优秀的javascript库,也提供了一个优秀的表单验证插件——Validation.Validation是历史最悠久 ...
 - 日期格式化,moment.js
			
官方文档:http://momentjs.com/; 使用方法:moment(data).format("YYYY-MM-DD");//data为日期的字符串形式 moment() ...
 - Struts2之Validator
			
Struts2中提供了数据校验验证数据例如验证邮件.数字等.验证方式有3种:一是通过validate()方法,二是通过Xml,三是使用注解方式. 一.初始化 首先定义一个User类 package c ...
 - RabbitMQ集群搭建
			
准备三个节点,系统为CentOS7 Node IP rabbitmq01 172.50.0.64 rabbitmq02 172.50.0.65 rabbitmq03 172.50.0.66 这里把no ...
 - 【Egret】3D 使用中的一些疑难解决技巧!
			
1.问题:目前Egret3D中,发布到手机后无法响应鼠标事件 解决方法:①打开发布后的libs/module/egret/egret.web.min.js,查找e.stopPropagation(), ...
 - poptest老李谈分布式与集群
			
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...
 - Android kernel LOGO的更换方法
			
[从制作logo到LCD显示或者VGA显示logo] 1.制作logo的方法: 首先选择一个自己喜欢的图片,然后通过GIMP软件将该图片保存为.png格式, 变换方式这个就不说了(very easy) ...