TopCoder<SRM>上的一道1100分的题目解析附代码
首先我们来简单看一下这道题的statement
Problem Statement |
|||||||||||||
|
Note that in the following problem statement, all quotes and angle brackets are for clarity A certain vending machine delves out its goods from a rotating cylinder, which can rotate around in both clockwise and counter-clockwise directions. The cylinder has a number of shelves on it, and each shelf is divided into a number of columns. On the front of the machine, there is a panel of doors that extends the entire height of the column. There is one door for each shelf, which is the width of one column. When a purchase is made, the user uses two buttons to rotate the cylinder so their purchase is located at a door. They make their purchase by sliding the appropriate door open, and removing the item (there can only be one item per column on a particular shelf). The cylinder can rotate in a complete circle, and so there are always two ways to get from a particular column to another column. Because the vending machine company wants to sell the most expensive items possible, and the machine can only show one column at a time, the machine will always try to put forth the most expensive column available. The price of a column is calculated by adding up all the prices of the remaining items in that column. The most expensive column is defined to be the one with the maximum price. If 5 minutes have elapsed since the last purchase was made, the machine rotates the cylinder to the most expensive column. If, however, another purchase has been made before the 5 minutes are up, the rotation does not occur, and the 5 minute timer is reset. Recently, some machines' rotating motors have been failing early, and the company wants to see if it is because the machines rotate to show their expensive column too often. To determine this, they have hired you to simulate purchases and see how long the motor is running. You will be given the prices of all the items in the vending machine in a vector <string>. Each element of prices will be a single-space separated list of integers, which are the prices (in cents) of the items. The Nth integer in the Mth element of prices represents the price of the Nth column in the Mth shelf in the cylinder. You will also be given a vector <string> purchases. Each element in purchases will be in the format: "<shelf>,<column>:<time>" <shelf> is a 0-based integer which identifies the shelf that the item was purchased from. <column> is a 0-based integer which identifies the column the item was purchased from. <time> is an integer which represents the time, in minutes, since the machine was turned on. In the simulation, the motor needs to run for 1 second in order to rotate to an adjacent column. When the machine is turned on, column 0 is facing out, and it immediately rotates to the most expensive column, even if the first purchase is at time 0. The machine also rotates to the most expensive column at the end of the simulation, after the last purchase. Note that when an item is purchased, its price is no longer used in calculating the price of the column it is in. When the machine rotates to the most expensive column, or when a user rotates the cylinder, the rotation is in the direction which takes the least amount of time. For example, in a 4-column cylinder, if column 0 is displayed, and the cylinder is rotated to column 3, it can be rotated backwards, which takes 1 second, versus rotating forwards which takes 3 seconds. If a user tries to purchase an item that was already purchased, this is an incorrect simulation, and your method should return -1. Otherwise, your method should return how long the motor was running, in seconds. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
| - | When rotating to the most expensive column, if two columns have the same price, rotate to the one with the lowest column number (see example 0). | ||||||||||||
| - | If two purchases are less than 5 minutes apart, the machine does not perform a rotation to the most expensive column between the purchases. If two purchases are 5 or more minutes apart, the machine rotates to the most expensive column between the two purchases. | ||||||||||||
Constraints |
|||||||||||||
| - | prices will have between 1 and 50 elements, inclusive. | ||||||||||||
| - | Each element of prices will have between 5 and 50 characters, is a single-space separated list of integers, and has no leading or trailing spaces. | ||||||||||||
| - | Each element of prices will have the same number of integers in it. | ||||||||||||
| - | Each element of prices will have at least 3 integers in it. | ||||||||||||
| - | Each integer in prices will be between 1 and 10000, inclusive, and will not contain leading 0's. | ||||||||||||
| - | purchases will have between 1 and 50 elements, inclusive. | ||||||||||||
| - | Each element of purchases will be in the format "<shelf>,<column>:<time>" (angle brackets and quotes are for clarity only), where <shelf>, <column>, and <time> are all integers. | ||||||||||||
| - | In each element of purchases, <shelf> will be between 0 and M - 1, inclusive, where M is the number of elements in prices. | ||||||||||||
| - | In each element of purchases, <column> will be between 0 and N - 1, inclusive, where N is the number of integers in each element of prices. | ||||||||||||
| - | In each element of purchases, <time> will be between 0 and 1000, inclusive. | ||||||||||||
| - | In each element of purchases, <shelf>, <column>, and <time> will not contain extra leading 0's. | ||||||||||||
| - | purchases will be sorted in strictly ascending order by <time>. This means that each purchase must occur later than all previous ones. | ||||||||||||
Examples |
|||||||||||||
| 0) | |||||||||||||
|
|||||||||||||
| 1) | |||||||||||||
|
|||||||||||||
| 2) | |||||||||||||
|
|||||||||||||
| 3) | |||||||||||||
|
|||||||||||||
| 4) | |||||||||||||
|
|||||||||||||
TopCoder的题目往往有这样一种规律,就是陈述的长度越长那么这道题的难度往往也会下降
没有考什么独特新颖的算法,这道题的精髓就在于熟练操作字符串和逻辑思路严密与否
下面这张图片是我手写的流程框体(用office体现太麻烦了)

1)整个算法的主要程序步为
1.计算每一列的商品之和并返回最大的一列
2.计算从当前位置到最大一列的最短路径
3.将购买的代价(权)迭加且同时判断边界问题和题目要求的不重叠问题
2)另一个比较重要的问题是在于字符串的操作上,根据SRM的规则灵活运用格式化字符串转换和c++里的stringstream类会极大的提升程序运行以及Coding的速度效率
3)代码如下在3月22日下午AC
#include<iostream>
#include<cstdlib>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<sstream> using namespace std; class VendingMachine{
public: int maxColumn(vector<vector<int>> p){
int maxVal=;
int maxCol=;
//////////////If the maximun sum is the same
//////////////The function will return the minimized colomn index
for(int i=;i<p[].size();i++){
int tmp=;
for(int j=;j<p.size();j++){
tmp+=p[j][i];
}
if(maxVal<tmp){
maxVal=tmp;
maxCol=i;
}
}
return maxCol;
} int minMove(int start,int end,int len){
if(start<len&&end<len){
return abs(end-start)<(len-abs(end-start))?abs(end-start):(len-abs(end-start));
}else return -;
} vector<vector<int>> transform(vector<string> pr){
vector<vector<int>> tmp;
for(int i=;i<pr.size();i++){
vector<int> v;
istringstream iss(pr[i]);
int iX;
while(iss>>iX){
v.push_back(iX);
}
tmp.push_back(v);
}
return tmp;
} int motorUse(vector<string> prices,vector<string> purchases){
int iTotal=;
int iMax=;
int iNow=;
int iClock=;
vector<vector<int>> p;
p=transform(prices);
int iLen=p[].size();
//////Initialize The Cost
iMax=maxColumn(p);
iTotal+=minMove(iNow,iMax,iLen);
iNow=iMax;
//////Loops Begin
for(int i=;i<purchases.size();i++){
int s,c,t;
sscanf(purchases[i].c_str(),"%d,%d:%d",&s,&c,&t);
if(!p[s][c])return -;
if((t-iClock)>=){
////////Adjusting
iMax=maxColumn(p);
iTotal+=minMove(iNow,iMax,iLen);
iNow=iMax;
}
iTotal+=minMove(iNow,c,iLen);
iNow=c;
p[s][c]=;
//////Ending of a loop
iClock+=t;
}
iMax=maxColumn(p);
iTotal+=minMove(iNow,iMax,iLen);
return iTotal;
} };
TopCoder<SRM>上的一道1100分的题目解析附代码的更多相关文章
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- TopCoder SRM 667 Div.2题解
概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 625 Incrementing Sequence 题解
本题就是给出一个数k和一个数组,包含N个元素,通过每次添加�数组中的一个数的操作,最后须要得到1 - N的一个序列,不用排序. 能够从暴力法入手,然后优化. 这里利用hash表进行优化,终于得到时间效 ...
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- 一道简单的动态规划题目——House Robber
一.题目 House Robber(一道Leetcode上的关于动态规划的简单题目)具体描述如下: There is a professional robber planning to rob hou ...
- 手把手教从零开始在GitHub上使用Hexo搭建博客教程(一)-附GitHub注册及配置
前言 有朋友问了我关于博客系统搭建相关的问题,由于是做开发相关的工作,我给他推荐的是使用github的gh-pages服务搭建个人博客. 推荐理由: 免费:github提供gh-pages服务是免费的 ...
- Mac上使用Visual Studio Code开发/调试.NET Core代码
Mac上使用Visual Studio Code开发/调试.NET Core代码 .Net Core 1.0终于发布了,Core的一大卖点就是跨平台.这个跨平台不只是跨平台运行,而且可以跨平台开发.今 ...
随机推荐
- java查看工具jstack-windows
Prints Java thread stack traces for a Java process, core file, or remote debug server. This command ...
- vue2.X props 数据传递 实现组件内数据与组件外的数据的双向绑定
vue2.0 禁止 子组件修改父组件数据 在Vue2中组件的props的数据流动改为了只能单向流动,即只能由组件外(调用组件方)通过组件的DOM属性attribute传递props给组件内,组件内只能 ...
- winform制作自定义控件
一 .概述Windows 窗体控件是可再次使用的组件,它们封装了用户界面功能,并且可以用于客户端 Windows 应用程序.“Windows 窗体”不仅提供了许多现成控件,还提供了自行开发控件的基础结 ...
- tomcat启动出现异常 Error filterStart
tomcat启动中出现 Error filterStart异常, 没有任何堆栈信息,如下: SEVERE: Error filterStart Jul 6, 2012 3:39:05 PM org.a ...
- 【甘道夫】Ubuntu14 server + Hadoop2.2.0环境下Sqoop1.99.3部署记录
第一步.下载.解压.配置环境变量: 官网下载sqoop1.99.3 http://mirrors.cnnic.cn/apache/sqoop/1.99.3/ 将sqoop解压到目标文件夹,我的是 /h ...
- Oracle:创建存储过程
1.无参存储过程 create or replace procedure test_procasv_total number(10);begin select count(*) into v_tot ...
- android 5.0 默认水波纹背景属性,可设置不论什么View
actionBarItemBackground 5.0以上超出边界圆形水波纹 selectableItemBackground 5.0以上边界内圆形水波纹 这两个属性在5.0下面是默认的灰色效果 ...
- 现在有一张半径为r的圆桌,其中心位于(x,y),现在他想把圆桌的中心移到(x1,y1)。每次移动一步,都必须在圆桌边缘固定一个点然后将圆桌绕这个点旋转。问最少需要移动几步。
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- Spark源码分析之一:Job提交运行总流程概述
Spark是一个基于内存的分布式计算框架,运行在其上的应用程序,按照Action被划分为一个个Job,而Job提交运行的总流程,大致分为两个阶段: 1.Stage划分与提交 (1)Job按照RDD之间 ...
- php正则表达式匹配img中任意属性的方法
经常和图片打交道,不得不用到一些提取图片中scr.alt.title.等的属性,这里总结给大家一些常用的,感觉还不错,比较通用! PHP正则表达式匹配img中任意属性PHP 复制代码代码如下: < ...