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

P1996 约瑟夫问题

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

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

 #include <iostream>
#include <cstdio>
#include <vector>
#include <climits>
#include <stack>
#include <string>
using namespace std; const int INF = ;
int main () {
int n, m;
cin >> n >> m;
vector<int> vec(n+, );
vec[] = INF;
int out = , ptr = , number = ;
while(out < n) {
if (ptr > n) { ptr = ptr % n; }
if (number == m) {
/*
if (vec[ptr] == INF) {
cout << "\nexception \n";
printf("ptr[%d]\n", ptr);
for (int ele = 0; ele <= n; ++ele) {
printf("%5-d", ele);
}
printf("\n");
for (auto ele : vec) {
printf("%5-d", ele);
}
cout << endl;
return 0;
}
*/
cout << ptr << " " ;
number = ;
vec[ptr] = INF;
++out;
} else {
//printf("=======debug========number[%d], ptr[%d]\n", number, ptr);
++ptr;
if (ptr > n) { ptr = ptr % n; }
while(vec[ptr] == INF) {
++ptr;
if (ptr > n) { ptr = ptr % n; }
}
++number;
}
}
if (out) {
cout << endl;
}
return ;
}

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

P1115 最大子段和

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

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

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

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

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

 #include <iostream>
#include <cstdio>
#include <vector>
#include <climits>
using namespace std; int main () {
int n;
cin >> n;
int ans = INT_MIN, dp = ;
for (int i = ; i <= n; ++i) {
int x;
cin >> x;
if (i == ) {
dp = ans = x;
} else {
dp = dp + x > ? dp + x : x;
}
ans = max(ans, dp);
}
cout << ans << endl;
return ;
}

P1739 表达式括号匹配

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

提交一次AC了。

 #include <iostream>
#include <cstdio>
#include <vector>
#include <climits>
#include <stack>
#include <string>
using namespace std; int main () {
stack<char> stk;
string str;
cin >> str;
for (auto ele : str) {
if (ele == '@') {
if (stk.empty()) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return ;
}
if (ele == '(') {
stk.push(ele);
}
if (ele == ')') {
if (stk.empty()) {
cout << "NO" << endl;
return ;
}
stk.pop();
}
}
return ;
}

队列安排

P1449 后缀表达式

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

提交一次AC了。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <sstream>
using namespace std; int getNextOper(string& str, int& idx) {
string strOper;
for (int i = idx; i < str.size(); ++i) {
if (str[i] == '.') {
idx = i + ;
break;
}
if (!isdigit(str[i])) {
printf("exception\n\n");
exit(-);
}
strOper += str[i];
}
stringstream ss(strOper);
int ans;
ss >> ans;
return ans; } int main () {
stack<int> stk;
string str;
cin >> str;
int startIdx = ;
int iOper = getNextOper(str, startIdx);
stk.push(iOper); int answer = ;
while (str[startIdx] != '@') {
if (isdigit(str[startIdx])) {
int iOper = getNextOper(str, startIdx);
stk.push(iOper);
} else {
if (stk.size() < ) {
printf("exception \n\n");
}
int a = stk.top(); stk.pop();
int b = stk.top(); stk.pop();
if (str[startIdx] == '+') {
answer = a + b;
}
if (str[startIdx] == '-') {
answer = b - a;
}
if (str[startIdx] == '*') {
answer = a * b;
}
if (str[startIdx] == '/') {
answer = b / a;
}
startIdx++;
stk.push(answer);
}
}
cout << stk.top() << endl;
return ;
}

【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. PHP- 搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5输出 ...

  2. BZOJ5205 [CodePlus 2018 3 月赛]白金元首与莫斯科

    传送门 emm在雅礼集训的时候听到的一道题 上来就觉得是插头dp 最后果然是轮廓线状压233 我们简化一下题意. 有一个n*m的网格,每个格子是空地或障碍物,询问把每一个空地看成障碍物的情况下,用1* ...

  3. js console对象

    js调试 根据信息的不同性质,console对象显示信息的方法,分别是一般信息console.log(),console.info().除错信息console.debug().警告提示console. ...

  4. 数据结构---Java---HashMap---JDK1.7

    源码解读 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Clone ...

  5. Windows下Maven安装 + eclipse集成

    一.什么是maven? Maven是一个项目管理工具,能方便的帮我们下载jar包,告别传统手动导包的方式. 二.maven仓库 maven中有中央仓库,本地仓库,私服三个概念 1.中央仓库是maven ...

  6. Python中的try-finally

    >>> try: ... raise KeyboardInterrupt ... finally: ... print('Goodbye, world!') ... Goodbye, ...

  7. js插件-简单拖拽

    前端开发的时候,有好多地方用到拖拽效果,当然 http://jqueryui.com/draggable/  是个不错的选择,but 我是个打破砂锅问到底的人,抽点时间用js小小的实现了类似的插件,话 ...

  8. leetcode-165周赛-1277-统计全为1的正方形子矩阵

    题目描述: 自己的提交: class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matri ...

  9. 存储过程中的in out in out 三种类型的参数

    in 是参数的默认模式,这种模式就是在程序运行的时候已经具有值,在程序体中值不会改变. out模式定义的参数只能在过程体内部赋值,表示该参数可以将某个值传递回调用他的过程 in out 表示高参数可以 ...

  10. BZOJ 4421: [Cerc2015] Digit Division(思路)

    传送门 解题思路 差点写树套树...可以发现如果几个数都能被\(m\)整除,那么这几个数拼起来也能被\(m\)整除.同理,如果一个数不能被\(m\)整除,那么它无论如何拆,都无法拆成若干个可以被\(m ...