任务说明:数组,链表,队列,栈,都是线性结构。巧用这些结构可以做出不少方便的事情。

P1996 约瑟夫问题

n个人,排成环形,喊到m的人出列,输出出列顺序。

咳咳,这个题目不好写,尽管简单就是模拟题...但是orz 乱七八糟加上debug的时间一共花了四十多分钟....

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <climits>
  5. #include <stack>
  6. #include <string>
  7. using namespace std;
  8.  
  9. const int INF = ;
  10. int main () {
  11. int n, m;
  12. cin >> n >> m;
  13. vector<int> vec(n+, );
  14. vec[] = INF;
  15. int out = , ptr = , number = ;
  16. while(out < n) {
  17. if (ptr > n) { ptr = ptr % n; }
  18. if (number == m) {
  19. /*
  20. if (vec[ptr] == INF) {
  21. cout << "\nexception \n";
  22. printf("ptr[%d]\n", ptr);
  23. for (int ele = 0; ele <= n; ++ele) {
  24. printf("%5-d", ele);
  25. }
  26. printf("\n");
  27. for (auto ele : vec) {
  28. printf("%5-d", ele);
  29. }
  30. cout << endl;
  31. return 0;
  32. }
  33. */
  34. cout << ptr << " " ;
  35. number = ;
  36. vec[ptr] = INF;
  37. ++out;
  38. } else {
  39. //printf("=======debug========number[%d], ptr[%d]\n", number, ptr);
  40. ++ptr;
  41. if (ptr > n) { ptr = ptr % n; }
  42. while(vec[ptr] == INF) {
  43. ++ptr;
  44. if (ptr > n) { ptr = ptr % n; }
  45. }
  46. ++number;
  47. }
  48. }
  49. if (out) {
  50. cout << endl;
  51. }
  52. return ;
  53. }

有空看看题解优化下代码..

P1115 最大子段和

做了这个题才颠覆了我对最大字段和的认识。

以前默认的解法就是用dp[i] 表示 a[0..i] 的和,然后用二重循环相减,求出最大子段和。

提交了发现才过了两个点,剩下的三个点TLE。

然后看了题解才发现原来可以这么做:就是用一个变量来记录读进来数字的当前子段和,如果发现这个变量(当前子段和)比零小,那么就把它置0。因为后面那一坨不论是啥,前面加个负数都不划算,所以这么解最优。

时间复杂度O(N), 空间复杂度O(1)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <climits>
  5. using namespace std;
  6.  
  7. int main () {
  8. int n;
  9. cin >> n;
  10. int ans = INT_MIN, dp = ;
  11. for (int i = ; i <= n; ++i) {
  12. int x;
  13. cin >> x;
  14. if (i == ) {
  15. dp = ans = x;
  16. } else {
  17. dp = dp + x > ? dp + x : x;
  18. }
  19. ans = max(ans, dp);
  20. }
  21. cout << ans << endl;
  22. return ;
  23. }

P1739 表达式括号匹配

简单题,不用复习,就是给个字符串,判断左右括号是否匹配。字符串没有空格隔开,直接cin就好。

提交一次AC了。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <climits>
  5. #include <stack>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main () {
  10. stack<char> stk;
  11. string str;
  12. cin >> str;
  13. for (auto ele : str) {
  14. if (ele == '@') {
  15. if (stk.empty()) {
  16. cout << "YES" << endl;
  17. } else {
  18. cout << "NO" << endl;
  19. }
  20. return ;
  21. }
  22. if (ele == '(') {
  23. stk.push(ele);
  24. }
  25. if (ele == ')') {
  26. if (stk.empty()) {
  27. cout << "NO" << endl;
  28. return ;
  29. }
  30. stk.pop();
  31. }
  32. }
  33. return ;
  34. }

队列安排

P1449 后缀表达式

后缀表达式求值,用栈做。注意- 和/的时候top出来的两个变量顺序。要是考到,直接用个减法当例子。

提交一次AC了。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <vector>
  5. #include <climits>
  6. #include <stack>
  7. #include <string>
  8. #include <sstream>
  9. using namespace std;
  10.  
  11. int getNextOper(string& str, int& idx) {
  12. string strOper;
  13. for (int i = idx; i < str.size(); ++i) {
  14. if (str[i] == '.') {
  15. idx = i + ;
  16. break;
  17. }
  18. if (!isdigit(str[i])) {
  19. printf("exception\n\n");
  20. exit(-);
  21. }
  22. strOper += str[i];
  23. }
  24. stringstream ss(strOper);
  25. int ans;
  26. ss >> ans;
  27. return ans;
  28.  
  29. }
  30.  
  31. int main () {
  32. stack<int> stk;
  33. string str;
  34. cin >> str;
  35. int startIdx = ;
  36. int iOper = getNextOper(str, startIdx);
  37. stk.push(iOper);
  38.  
  39. int answer = ;
  40. while (str[startIdx] != '@') {
  41. if (isdigit(str[startIdx])) {
  42. int iOper = getNextOper(str, startIdx);
  43. stk.push(iOper);
  44. } else {
  45. if (stk.size() < ) {
  46. printf("exception \n\n");
  47. }
  48. int a = stk.top(); stk.pop();
  49. int b = stk.top(); stk.pop();
  50. if (str[startIdx] == '+') {
  51. answer = a + b;
  52. }
  53. if (str[startIdx] == '-') {
  54. answer = b - a;
  55. }
  56. if (str[startIdx] == '*') {
  57. answer = a * b;
  58. }
  59. if (str[startIdx] == '/') {
  60. answer = b / a;
  61. }
  62. startIdx++;
  63. stk.push(answer);
  64. }
  65. }
  66. cout << stk.top() << endl;
  67. return ;
  68. }

【Luogu】【关卡2-13】线性数据结构(2017年10月)【还差一道题】的更多相关文章

  1. 【Luogu】【关卡2-6】贪心(2017年10月)

    任务说明:贪心就是只考虑眼前的利益.对于我们人生来说太贪是不好的,不过oi中,有时是对的. P1090 合并果子 有N堆果子,只能两两合并,每合并一次消耗的体力是两堆果子的权重和,问最小消耗多少体力. ...

  2. 【Luogu】【关卡2-3】排序(2017年10月) 【AK】

    任务说明:将杂乱无章的数据变得有规律.有各种各样的排序算法,看情况使用. 这里有空还是把各种排序算法总结下吧.qsort需要会写.. P1177 [模板]快速排序 这个题目懒得写了,直接sort了.. ...

  3. 欢迎来怼-Alpha周(2017年10月19)贡献分配规则和分配结果

    .从alpha周(2017年10月19日开始的2周)开始,提高贡献分比重. 贡献分 : 团队分 = 1 : 5 教师会在核算每位同学总分时按比例乘以系数. 每位同学带入团队贡献分10分,如果团队一共7 ...

  4. 2017年10月31日结束Outlook 2007与Office 365的连接

    2017 年10月31日 ,微软即将推出 Office 365中Exchange Online邮箱将需要Outlook for Windows的连接,即通过HTTP Over MAPI方式,传统使用R ...

  5. 江西省移动物联网发展战略新闻发布会举行-2017年10月江西IDC排行榜与发展报告

    编者按:当人们在做技术创新时,我们在做“外包产业“:当人们在做制造产业,我们在做”服务产业“:江人们在做AI智能时,我们在做”物联网“崛起,即使有一个落差,但红色热土从不缺少成长激情. 本期摘自上月初 ...

  6. 【Luogu】【关卡2-16】线性动态规划(2017年10月)【还差三道题】

    任务说明:这也是基础的动态规划.是在线性结构上面的动态规划,一定要掌握. P1020 导弹拦截 导弹拦截 P1091 合唱队形 老师给同学们排合唱队形.N位同学站成一排,音乐老师要请其中的(N-K)位 ...

  7. 【Luogu】【关卡2-14】 树形数据结构(2017年10月)【AK】

    任务说明:由一个根节点分叉,越分越多,就成了树.树可以表示数据之间的从属关系 P1087 FBI树 给一个01字符串,0对应B,1对应I,F对应既有0子节点又有1子节点的根节点,输出这棵树的后序遍历. ...

  8. 【Luogu】【关卡1-8】BOSS战-入门综合练习2(2017年10月)【AK】------都是基础题

    P1426 小鱼会有危险吗 我个人觉得这个题目出的不好,没说明白,就先只粘贴的AC代码吧 #include <bits/stdc++.h> using namespace std; int ...

  9. 【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】

    任务说明:这是最基础的动态规划.不过如果是第一次接触会有些难以理解.加油闯过这个坎. 01背包二维数组优化成滚动数组的时候有坑有坑有坑!!!必须要downto,downto,downto 情景和代码见 ...

随机推荐

  1. 【串线篇】SpringMVC运行流程

    1.所有请求,前端控制器(DispatcherServlet)收到请求,调用doDispatch进行处理 2.根据HandlerMapping中保存的请求映射信息找到,处理当前请求的,处理器执行链(包 ...

  2. mac版AIcc2019旋转扭曲工具在哪?AI cc 2019 for Mac旋转扭曲工具如何使用?

    想要旋转图片?ai mac通过线性的或非线性的算法,能使图像旋转.扭曲变形.今天小编要给大家分享的是如何查找使用mac版AIcc2019旋转扭曲工具,有需要的朋友快来学习学习吧! https://ww ...

  3. SCP:从Linux服务器下载文件夹到本地

    原文链接:https://blog.csdn.net/netlai/article/details/79756279 scp /home/work/source.txt work@192.168.0. ...

  4. Linux文件归档工具——cpio

    一cpio的介绍 功能:复制文件从或到归档 cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件. cpio [选项] > 文件 ...

  5. Temporarily disable Ceph scrubbing to resolve high IO load

    https://blog.dachary.org/2014/08/02/temporarily-disable-ceph-scrubbing-to-resolve-high-io-load/ In a ...

  6. hdu 6092 Rikka with Subset (集合计数,01背包)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  7. 【LeetCode 34】在排序数组中查找元素的第一个和最后一个位置

    题目链接 [题解] 二分某个数的上下界. 其实这个方法并不难. 只要你想清楚了二分最后一次执行的位置在什么地方就不难了. [代码] class Solution { public: vector< ...

  8. c#如何写服务,打包和卸载服务

    Service.cs  每隔一分钟进行一次数据操作 public Service1()        {            InitializeComponent();            Sy ...

  9. [CSP-S模拟测试45]题解

    开局一行$srand$,得分全靠随机化. A.kill 发现两个并不显然的性质: 1.选中的人和怪物一定是按顺序的.第一个人打所有被选中怪物的第一只,第二个人打第二只,$etc$. 2.最优方案打的怪 ...

  10. 92、R语言分析案例

    1.读取数据 > bank=read.table("bank-full.csv",header=TRUE,sep=";") > 2.查看数据结构 & ...