TopCoder SRM285 -- My Solution

My solution to cs224n assignment1(3-4) My solution3 阅读详情

这次比赛是北京时间夜里12点开始的,所以没有参加。下面的代码是比赛后一天写的,都通过了系统测试:

Problems :

BasketsWithApples:

We have some baskets containing apples, and we would like to perform the following procedure in a way that maximizes the number of remaining apples. First, we discard some (or none) of the baskets completely. Then, if the remaining baskets do not all contain the same number of apples, we remove excess apples from the baskets until they do.

You will be given a vector <int> apples where the i-th element of apples is the number of apples in the i-th basket. Return the number of apples remaining after the procedure described above is performed.

Definition

    
Class: BasketsWithApples
Method: removeExcess
Parameters: vector <int>
Returns: int
Method signature: int removeExcess(vector <int> apples)
(be sure your method is public)

Constraints

- apples will contain between 1 and 50 elements, inclusive.
- Each element in apples will be between 0 and 1000, inclusive.

这一问题时Div2 Level 1 ,所以很简单,只需要对apples排序,然后假设最少的篮子中的水果数依次为apple[0],apple[1]...,计算出每种情况下的剩余水果数,选出其中最大的返回。

Code :

int BasketsWithApples::removeExcess(vector <int> apples)
{
 int size = apples.size();
 vector<int> Apples = apples;
 std::sort(Apples.begin(),Apples.end());
 int ret = Apples[0]*size;
 int left;
 int i;
 for(i=1;i<size;i++){
  left = Apples[i]*(size-i);
  if(left>ret) ret = left;
 }
 return ret;
}

SentenceSplitting

You have a sentence written entirely in a single row. You would like to split it into several rows by replacing some of the spaces with "new row" indicators. Your goal is to minimize the width of the longest row in the resulting text ("new row" indicators do not count towards the width of a row). You may replace at most K spaces.

You will be given a string sentence and an int K. Split the sentence using the procedure described above and return the width of the longest row.

Definition

    
Class: SentenceSplitting
Method: split
Parameters: string, int
Returns: int
Method signature: int split(string sentence, int K)
(be sure your method is public)

Constraints

- sentence will contain between 1 and 50 characters, inclusive.
- sentence will consist of only letters ('a'-'z', 'A'-'Z') and spaces (' ').
- Each space character in sentence will be between two letters.
- K will be between 1 and 50, inclusive.

这个问题第一次看的时候感觉很简单,用递归就可以了。但是在时间复杂度上却总是不能通过,经过反复优化,终于通过系统测试了。我的算法如下:

  1. if K == 0 then return sentence.size()
  2. 以' '为分隔符将sentence分割成string[],以vector<int> size 记录每一段的长度,vector<int>pos记录空格的位置
  3. if size.length()<= K then return max(size)
  4. if all element in size are same then calculate result directly
  5. 用length记录每种分法的结果,result记录最终结果
  6. for i = 1 to pos.size()
  7.        strone = sentence.substr(0,pos[i]);
  8.        strtwo = sentence.substr(pos[i]+1,end)
  9.        if strone.length >= strtwo.length then length = strone.length
  10.        else length = MAX(strone.length,split(sentence,K-1)
  11.        result = MIN(result,length)
  12. End loop
  13. return result

Code:

int SentenceSplitting::split(string sentence, int K)
{
 if(K==0) return sentence.size();
 string one,two;
 int i,j;
 int ret = 100;
 int length;
 bool find_space = false;
 vector<int> pos;
 pos.push_back(-1);
 vector<int> size;
 int size_min = 100;
 int size_max = 0;
 for(i=0;i<sentence.size();i++){
  if(sentence[i]==' '){
   //cout<<i<<endl;
   find_space = true;
   size.push_back(i-pos.back()-1);
   if(size_min>size.back()) size_min = size.back();
   if(size_max<size.back()) size_max = size.back();
   pos.push_back(i);
  }
 }
 size.push_back(sentence.size()-pos.back()-1);
 if(size_min>size.back()) size_min = size.back();
 if(size_max<size.back()) size_max = size.back();
 if(!find_space) return sentence.size();

 if(K>=(size.size()-1)){
  return *(std::max_element(size.begin(),size.end()));
 }
 if(size_min == size_max && K>3){
  int half = (int)(sentence.size()/2)+1;
  cout<<half<<endl;
  if(half%(K+1)==0){
   length = (int)(half/(K+1));
   return length*2-1;
  }else{
   length = (int)(half/(K+1))+1;
   return length*2-1;
  }
 }

 for(i=1;i<pos.size()-K+1;i++){
  one = sentence.substr(0,pos[i]);
  two = sentence.substr(pos[i]+1,sentence.size()-pos[i]);
  if(one.size()>=two.size()){
   length = one.size();
   ret = MIN(ret,length);
   break;
  }else{
   length = split(two,K-1);
   length = MAX(one.size(),length);
   ret = MIN(ret,length);
  }
 }
 return ret;
}

OperationsArrangement

You are given a sequence of digits. You must insert either a '+' (addition) operator or a '*' (multiplication) operator between each pair of adjacent digits in such a way that minimizes the value of the resulting expression. The expression should be evaluated using the standard order of operations (multiplication has a higher precedence than addition).

You will be given a string sequence. Perform the procedure described above on the sequence and return the resulting expression. If there are several possible answers, return the one that comes first lexicographically. Note that '*' comes before '+' lexicographically.

Definition

    
Class: OperationsArrangement
Method: arrange
Parameters: string
Returns: string
Method signature: string arrange(string sequence)
(be sure your method is public)
    
 

Constraints

- sequence will contain between 2 and 50 characters, inclusive.
- Each character in sequence will be a digit ('0'-'9').

这道题目并不难,就是要把所有的可能性考虑全不太容易,注意以下情形:

  1. 1*num < 1 + num
  2. 2*2 = 2+2
  3. '*' <'+'
  4. 如果sequence含有0,则运算符都赋 '*'

注意以上4点,就可以编出正确的代码:

string OperationsArrangement::arrange(string sequence)
{
 char* buf = new char[100];
 memset(buf,0,100);
 int i;
 int k = 0;
 bool find1,find2;
 if(sequence.find("0")!=string::npos){
  for(i=0;i<sequence.size()-1;i++){
   buf[k] = sequence[i];
   buf[k+1] = '*';
   k+=2;
  }
  buf[k] = sequence[sequence.length()-1];
 }else{
  find2 = false;
  find1 = false;
  int num = sequence[0]-'0';
  for(i=0;i<sequence.size()-1;i++){
   cout<<num<<" ";
   if(find1 == false && sequence[i] == '1'){
    find2 = false;
    buf[k] = sequence[i];
    buf[k+1] = '*';
    num = num*(sequence[i+1]-'0');
    k+=2;
    continue;
   }
   if(sequence[i] != '1') find1 = true;
   if(sequence[i+1] == '1'){
    find2 = false;
    buf[k] = sequence[i];
    buf[k+1] = '*';
    k+=2;
   }
   else if(sequence[i+1] == '2'){
    if((num<=2) && find2 == false){
     find2 = true;
     buf[k] = sequence[i];
     buf[k+1] = '*';
     num = num*2;
     k+=2;
    }else{
     find2 = false;
     buf[k] = sequence[i];
     buf[k+1] = '+';
     find1 = true;
     num = sequence[i+1]-'0';
     k+=2;
    }
   }
   else{
    find2 = false;
    buf[k] = sequence[i];
    buf[k+1] = '+';
    find1 = true;
    num = sequence[i+1]-'0';
    k+=2;
   }
  }
  buf[k]=sequence[sequence.length()-1];
 }
 string ret = buf;
 return ret;
}

给出一些结果,可供参考

"12121212121212121212121212121212121212121212121212"

1*2*1*2*1+2*1*2*1+2*1*2*1+2*1*2*1+2*1*2*1+2*1*2*1+2*1*2*1+2*1*2*1+2*1*2*1+2*1*2*
1+2*1*2*1+2*1*2*1+2

"11111111112111111111211121111112"

1*1*1*1*1*1*1*1*1*1*2*1*1*1*1*1*1*1*1*1*2*1*1*1+2*1*1*1*1*1*1*2

【Unity】YooAsset与HybridCLR:高效资源管理与代码热更的黄金组合 本文深入探讨了Unity开发中YooAsset与HybridCLR的组合应用,为需要长期运营的项目提供了完整的资源与代码热更新解决方案。YooAsset作为高效的资源管理框架,简化了AssetBundle的打包、加载与热更流程;而HybridCLR则攻克了IL2CPP模式下C#代码无法热更的难题。两者结合,实现了从资源到逻辑的平滑在线更新,极大提升了开发迭代与线上维护的效率。 阅读详情

相关推荐

LRTimelapse Pro v7.0.0 激活版下载安装教程 (延迟摄影后期渲染)

LRTimelapse Pro 是一款优秀的高级延时摄影解决方案,使用将为用户提供最全面的工具和功能,从导入和组织您的延时摄影序列、视觉工作流程、在 LRTimelapse 中直接编辑和渲染、视觉去闪烁等等,功能齐全,可以使用 LRTimelapse Pro 编辑多个序列时显著提高您的效率,轻松在 LRTimelapse 中配置和使用色彩管理,进行长期延时编辑,在 LRTimelapse Pro 中使用长期工作流程。为延时编辑、关键帧、分级和渲染提供最全面的解决方案。

wyqshusan的博客 3464

eclipse常见语句释义

安装spring常见语句 choose one of the following alternate solutions 选择下列替代解决方案之一 keep my installaion the same and modeify the items being installe to be compatible 让我的设备一样,modeify项目正在安装要比赛 show

pgh_1947的博客 1394

小米CyberGear微电机驱动代码(CAN总线驱动,含开源地址)

小米CyberGear微电机驱动代码(ROS下的CAN总线驱动,含开源地址)

2302_80751833的博客 4431

topcoder SRM500 DIV1 LEVEL1 250分题

Problem Statement   N friends (numbered from 0 to N-1) play a game called Mafia. The exact rules of the game are not important for this problem. What's important is that at some point in the

AChunter的专栏 795

MBD-从一个示例看代码生成过程(rtw文件、TLC语言)

大概介绍了MBD代码生成流程,rtw文件、TLC语言

小胡同学的工程师学习之路 4214

深入解析Xilinx IDELAYE2在FPGA时序优化中的关键作用与实战配置

本文深入解析了Xilinx FPGA中IDELAYE2模块在时序优化中的关键作用。作为精密的信号延时微调器,IDELAYE2通过可编程的抽头延迟,有效解决高速接口中数据与时钟的相位偏差问题。文章详细阐述了其必须与IDELAYCTRL配对使用的核心机制,并对比分析了FIXED、VARIABLE和VAR_LOAD三种工作模式的实战配置方法与代码示例,为FPGA工程师提供了解决时序收敛难题的关键硬件配置指南。

pear55的博客 1183

maven构建项目(多模块项目)

在eclipse下构建maven项目,该项目由多个子模块组成。 1.创建一个父项目 NEW -->project-->maven-->maven Project,点击下一步,进入new maven Project的Select project name and location界面 ,什么也不做,直接下一步到Select an Archetype界面。 在这个界面中选择ma

subuser的专栏 8万+

CMOS图像传感器系列--(四)HDR之LOFIC技术

CMOS图像传感器HDR技术之LOFIC解析 LOFIC(横向溢出积分电容器)技术是当前高动态范围成像的重要方案,通过在像素光电二极管旁设置高密度电容器收集溢出电荷,有效保留高光细节。该技术采用特殊像素结构,通过skim level触发电荷转移,实现单帧HDR成像。然而LOFIC存在显著局限性:高光区域噪声明显且难以消除,动态范围拓展不均衡(主要优化亮部),需复杂MIM工艺导致成本升高。为应对挑战,行业提出混合方案(如结合DCG技术)和工艺改进方向。OmniVision最新车载传感器OX08D10就采用了L

AIoT深度探索者 | 嵌入式系统架构师 深耕物联网(IoT)与人工智能(AI)融合技术,聚焦​​边缘计算、图像开发、低功耗通信协议​​,致力于智能硬件,关注我获取最新技术文章信息与海量资源~ 5788

eclipse 离线安装插件报cannot perform operation.Computing alternate solutions...解决办法

问题如下: 当不能连接外网,离线安装Spring插件时,可能会发现以下问题:eclipse长时间停留在下图所示状态,提示“cannot perform operation.Computing alternate solutions,may take a while: ...”。在网上查找了资料,问题可能是eclipse一直在尝试从自己的更新源下载插件,却因为局域网环境,连接不上导致 解决方...

晓风岚月 1970

FANUC机器人_CRX协作机器人的开机和示教器的初始化设置基本步骤(二)

FANUC机器人_CRX协作机器人的开机和示教器的初始化设置基本步骤

Robot_PLC_HMI_Automation 2732

jira的详细使用教程

​jira的详细使用教程,1,Jira- 是一款缺陷管理软件工具,用其进行缺陷的管理和跟踪。(是B/S架构的软件) 2,主要涉及的角色: 3,具体工作流 添加工作流 1.点击项目设置

计算机毕业论文源码,学生个人网页制作html源码。贴近用户做网络推广和互联网优化。 3295

Cannot choose between the following configurations of project

1.现象描述Android studio 2.3升级3.0之后报如下错误:2.解决办法在module的build.gradle里面的dependencies加上一句:compileproject(path:':rxlife',configuration:'default') 转载于:https://blog.51cto.com/9963492/1973671...

weixin_34417200的博客 288

9篇2章2节:GBD 数据库的数据申请详解

无论您是初入公共卫生领域的学生、资深的科研工作者,还是政策决策者,GBD数据库都将成为您数据分析与决策支持中不可或缺的重要工具。

MD Code 2599

JAVA中常见的JVM配置以及参数释义

垃圾回收相关的JVM参数: -Xms / -Xmx — 堆的初始大小 / 堆的最大大小 -Xmn — 堆中年轻代的大小 -XX:-DisableExplicitGC — 让System.gc()不产生任何作用 -XX:+PrintGCDetails — 打印GC的细节 -XX:+PrintGCDateStamps — 打印GC操作的时间戳 -XX:NewSize / XX:MaxNewSize —...

周氏“武功秘籍”之天马行空 961

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Error occurred in deployment step 'Add Solution': Failed to extract the cab file in the solution

今天在部署solution的时候突然出现了一个奇怪的问题“Error occurred in deployment step 'Add Solution': Failed to extract the cab file in the solution”, 在添加解决

shrenk的专栏 2498

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Eclipse软件常见设置

myEclipse 默认workspace的设置问题eclipse/configuration/.settings目录下,有一个“org.eclipse.ui.ide.prefs”文件,用记事本打开,里面有一个“SHOW_WORKSPACE_SELECTION_DIALOG”,如果你之前有打勾的话后面的值是false,你把它改成true就好了,下次再开就出现那个“Workspace Launc

jianxue的专栏 693

eclipse软件使用

1.行号显示: Window -- Prefences -- General -- Editors -- Text Editors -- show line numbers 2.eclipse怎么打开项目管理视图 Window--&gt;Show View--&gt;Project Explorer

移动开发记录 119

Eclipse/myEclipse常见操作

文章目录 1、我开发常用的快捷键 2、怎么查看eclipse的版本? 3、查看配置sdk路径? 4、编码修改? 5、类的注释 6、android eclipse截屏 1、我开发常用的快捷键 注:mac的Command对应windows的ctrl 自动排版:Ctrl+shift+F 自动引导类包 :Ctrl+Shift+O 定位在某行:Ctrl+L 删除当前行:Ctrl+...

鲁迷那的专栏—坚持实践后再写出来! 400

solutions

Yesterday, we conducted a technical exchange with Mirapoint CTO, There are several issues ,please provide solutions. 1、Support multi-domain LDAP query, there are two options, next month testing ...

laikunhhhh的专栏 344

Myeclipse2014 安装插件提示:cannot perform operation.Computing alternate solutions,may take a while:解决方法

  (这第一部分废话有点多,可以忽略直接看解决方案部分)今天在Myeclipse2014安装Jrebel插件的时候,遇到了一个问题:cannot perform operation.Computing alternate solutions,may take a while: ...,页面一直卡在这里,一开始以为是文件资源的不好,下载的比较慢,后来发现下载了好久一直下载不下来。   后来终于找到问...

h_025的博客 5759
上一篇: TopCoder SRM 281
下一篇: 弗雷德里克·特曼:硅谷之父、斯坦福大学前副校长——(转自新浪网)
xlvector
博客等级 码龄21年 64粉丝 71原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值