【HDU - 5914 】Triangle (打表 或 set维护)

题干:

Mr. Frog has n sticks, whose lengths are 1,2, 3⋯⋯n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides to steal some sticks! Output the minimal number of sticks he should steal so that Mr. Frog cannot form a triangle with 
any three of the remaining sticks.

Input

The first line contains only one integer T (T≤20T≤20), which indicates the number of test cases. 

For each test case, there is only one line describing the given integer n (1≤n≤201≤n≤20).

Output

For each test case, output one line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of sticks Wallice should steal.

Sample Input

3
4
5
6

Sample Output

Case #1: 1
Case #2: 1
Case #3: 2

解题报告:

    看网上的题解都说是打表,那怎么打的表啊?手推?万一不正确呢?有点侥幸啊。。其实这题也可以搜索来找答案,但是还有更好的方法,比如用set,也是0ms过,我看好多打表的都是15ms,不知道是什么情况。

AC代码:

#include<bits/stdc++.h>using namespace std;int main()
{int t,n;int iCase = 0;scanf("%d",&t);while(t--) {scanf("%d",&n);printf("Case #%d: ",++iCase);if(n <= 3) {printf("0\n");continue;}int ans = 0;set<int> st;for(int i = 1; i<=n; i++) st.insert(i);set<int>::iterator it,itt,ittt,qq;int flag = 1;while(flag) {qq=st.end();qq--;qq--;for(it = st.begin(); it!=qq; ++it) {itt=++it;--it;ittt=++itt;--itt;if(*it + *itt > *ittt) {st.erase(ittt);ans++;break;}++it;if(it == qq) flag=0;--it;}}printf("%d\n",ans);}return 0 ;
}

总结:

    刚开始写的时候错误还是比较多的,比如for遍历的时候it != st.end(),但是这题不行,因为itt和ittt就越界了。。所以这题qq迭代器就是记录一下st.end()往前两个是多少。还有一个坑点就是需要实时计算qq的值,因为你有erase操作,所以st.end()就在变,所以qq也要跟着变才对。再就是n<=3的时候要特判,输出格式的空格要注意。


还有一个类似的题目在别的题解上看到了这里粘贴一下:

切金条

一根金条长31厘米,如果把它切成数段,使其中的一段或几段能够接成长为1到31中任何整数的金条,请问要切几次?

题解:第一反应是斐波那契数列1 2 3 5 8 12,切5刀,结果错了,这道题应该用二进制做。123是不需要3的,因为12可以构成3,所以124是更好的选择。

这两道题好像,但是这是两道题,一定要仔细区分开。

1 2 4 8 16,和正好31,切4刀,答案是4。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441760.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

harmonyos消息服务器,第三方纯HarmonyOS应用太少,你还愿意升级吗?

部分纯鸿蒙 HarmonyOS 应用已上线&#xff1a;图标多了“HMOS”角标标识&#xff0c;而只有真正的鸿蒙系统应用才能真正体验到万能卡片等系列的功能&#xff0c;但是很明显目前真正属于鸿蒙系统的第三方应用太少……基本上都是华为自身的应用&#xff0c;那么如果应用太少你还愿…

如何释放mysql连接资源_CI框架出现mysql数据库连接资源无法释放的解决方法

本文实例分析了CI框架出现mysql数据库连接资源无法释放的解决方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;使用ci框架提供的类查询数据&#xff1a; $this->load->database();$query $this->db->query($sql);程序运行一段时间之后&#xff0c;报错…

【nyoj - 860】 又见0-1背包 (dp,反向0-1背包,好题好思路)

题干&#xff1a; 又见01背包 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;3 输入 多组测试数据。 每组测试数据第一行输入&#xff0c;n 和 W &#xff0c;接下来有n行&#xff0c;每行输入两个数&#xff0c;代表第i个物品的wi 和 v…

300英雄服务器维护多久,《300英雄》2021年5月20日6:00-9:00更新维护公告

尊敬的《300英雄》玩家:《300英雄》将于2021年5月20日6:00-9:00(星期四)&#xff0c;对所有大区进行停机更新&#xff0c;更新期间&#xff0c;您将无法登录游戏。如果在预定时间内无法完成维护内容&#xff0c;开服时间也将继续顺延。具体更新内容如下&#xff1a;一、活动相关…

c 连接mysql示例 源码_MySQL 连接

MySQL 连接使用mysql二进制方式连接您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库。实例以下是从命令行中连接mysql服务器的简单实例&#xff1a;[[email protected]]# mysql -u root -pEnter password:******在登录成功后会出现 mysql> 命令提示窗口…

【 FZU - 2214 】Knapsack problem(逆向0-1背包)

题干&#xff1a; Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the …

服务器2008系统如何设置休眠时间,Win7休眠和睡眠怎么开启(Win2008)

如果把 Win7休眠和睡眠关闭了&#xff0c;需要的时候可以用命令重新开启&#xff0c;毕竟这两个功能不但可以节约电&#xff0c;还可以迅速恢复工作状态&#xff0c;节约开机开软件的时间。Win2008 R2 跟 Win7 同一内核&#xff0c;开启休眠和睡眠的命令也一样。在开启休眠和睡…

【HDU - 1220】Cube (组合数学,简单)

题干&#xff1a; Cowl is good at solving math problems. One day a friend asked him such a question: You are given a cube whose edge length is N, it is cut by the planes that was paralleled to its side planes into N * N * N unit cubes. Two unit cubes may h…

java poi读取word中附件_Java POI导入word, 带图片

1.导入文件示例&#xff0c;word中简历表格模板2.代码示例分两部分&#xff0c;一部分读取图片/*** 导入word(基本信息&#xff0c;word格式)* param staffId* param baseInfoFile*/void importStaffInfo(Integer staffId, MultipartFile file);-- 读取图片InputStream inputSt…

如何将文件拷贝服务器上,如何将文件复制到云服务器上

如何将文件复制到云服务器上 内容精选换一换将文件上传至Windows云服务器一般会采用MSTSC远程桌面连接的方式。本节为您介绍本地Windows计算机通过远程桌面连接&#xff0c;上传文件至Windows云服务器的操作方法。Windows云服务器可以访问公网。在本地Windows计算机上&#xff…

mysql5.7解压版错误_mysql 5.7 解压版 安装net start mysql 发生系统错误 2

1.配置环境变量 用户变量path 添加 mysql 安装目录2.新建my.ini文件 放到E:\mysql-5.7.24-winx64安装目录下[mysqld]port 3306basedirC:/software/mysql-5.7.21-winx64datadirC:/software/mysql-5.7.21-winx64/datamax_connections200character-set-serverutf8default-storage…

【HDU - 2553】N皇后问题 (dfs经典问题,回溯,搜索)

题干&#xff1a; 在N*N的方格棋盘放置了N个皇后&#xff0c;使得它们不相互攻击&#xff08;即任意2个皇后不允许处在同一排&#xff0c;同一列&#xff0c;也不允许处在与棋盘边框成45角的斜线上。 你的任务是&#xff0c;对于给定的N&#xff0c;求出有多少种合法的放置方…

浪潮服务器建立虚拟驱动器,像《十二时辰》一样去建立标准! 浪潮这款服务器做到了...

原标题&#xff1a;像《十二时辰》一样去建立标准&#xff01; 浪潮这款服务器做到了这个夏天&#xff0c;《长安十二时辰》制霸屏幕开画至今豆瓣评分达到8.8分现已成功“出海”在Amazon、Youtube、Viki付费上线成为唐风古韵的又一风向标现如今&#xff0c;越洋的标准可不止悠悠…

【HDU - 5918 】Sequence I (数组(字符串)匹配问题,可选KMP)

题干&#xff1a; Mr. Frog has two sequences a1,a2,⋯,ana1,a2,⋯,an and b1,b2,⋯,bmb1,b2,⋯,bm and a number p. He wants to know the number of positions q such that sequence b1,b2,⋯,bmb1,b2,⋯,bm is exactly the sequence aq,aqp,aq2p,⋯,aq(m−1)paq,aqp,aq2p,…

mysql_fetch_bit_mysql_fetch_array()

mysql_fetch_array()(PHP 4, PHP 5)从结果集中取得一行作为关联数组&#xff0c;或数字数组&#xff0c;或二者兼有说明mysql_fetch_array(resource$result[,int$ result_type]):array返回根据从结果集取得的行生成的数组&#xff0c;如果没有更多行则返回FALSE。mysql_fetch_a…

【HDU - 5916】Harmonic Value Description (构造,思维,SJ题)

题干&#xff1a; The harmonic value of the permutation p1,p2,⋯pnp1,p2,⋯pn is ∑i1n−1gcd(pi.pi1)∑i1n−1gcd(pi.pi1) Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n]. Inpu…

在python中、对于函数定义代码的理解_python中如何理解装饰器代码?

长文预警&#xff0c;【最浅显易懂的装饰器讲解】能不能专业地复制题目&#xff1f;配上代码&#xff0c;问题分段。我来给提主配上问题的代码。正式回答&#xff1a;1&#xff1a;如何理解return一个函数&#xff0c;它与return一个值得用法区别在哪&#xff1f;敲黑板&#x…

【AtCoder - 2554】Choose Integers (找规律,或枚举)

题干&#xff1a; Problem Statement We ask you to select some number of positive integers, and calculate the sum of them. It is allowed to select as many integers as you like, and as large integers as you wish. You have to follow these, however: each sele…

1m带宽可以做mysql数据库吗_服务器的1M带宽够用吗?1M网速是多少?

1M是什么&#xff1f;通常&#xff0c;在计算机中1M表示的是计算机的内存容量&#xff0c;即1M1024KB。但有不同的含义&#xff0c;云服务器中1M表示服务器的带宽&#xff0c;即1M带宽。在服务器中1M带宽够用吗&#xff1f;云服务器的1兆网速是多少&#xff1f;首先把云服务器带…

【POJ - 2318】TOYS(计算几何,叉积判断点与直线位置关系,二分)

题干&#xff1a; Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys i…