Good Bye 2014 C.(栈模拟)
来源:互联网 发布:网络互助平台怎么赚钱 编辑:程序博客网 时间:2024/05/03 15:25
题目链接:http://codeforces.com/contest/500/problem/C
解题思路:
本题过程完全可以用栈模拟来做,难点在于初始栈的构造。题目要求求出移动的总的最小重量,我们首先就构造初始栈,构造方法就是观察b数组里的值,把前n个不同元素入栈,注意是不同的元素!!!如果发现b数组扫完了,但是栈内元素不满n个,那么也不用再考虑其他元素了。具体证明略,此处想想就出来了,我们关心的只是入栈的这些元素,没入栈的元素不会对最后结果产生影响。
构造完初始栈,就开始模拟过程,每次找相应元素,出栈。找到后再移动位置,重新入栈。
最后每次做完后记得要把栈清空,一定要清空!!!
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 100001;int a[maxn] , b[maxn] , c[maxn] , d[maxn];stack<int> s;bool vis[maxn];int main(){ #ifdef DoubleQ freopen("in.txt","r",stdin); #endif std::ios::sync_with_stdio(false); std::cin.tie(0); int n , m; while(cin >> n >> m) { for(int i = 1 ; i <= n ; i++) cin >> a[i]; for(int i = 0 ; i < m ; i ++) cin >> b[i]; int k = 0; memset(vis , true , sizeof(vis)); for(int i = 0 ; i < m ; i ++) { if(b[i] != b[i+1] && vis[b[i]] ) { c[k++] = b[i]; vis[b[i]] = false; } if(k == n) break; } for(int i = k - 1 ; i >= 0 ; i --) { // cout << c[i] << endl; s.push(c[i]); } int sum = 0; int i = 0; while(i < m) { memset(d , 0 , sizeof(d)); int ss = 0 ; while(s.top() != b[i]) { sum += a[s.top()]; d[ss++] = s.top(); s.pop(); } int temp = b[i]; s.pop(); for(int j = ss - 1; j >=0 ; j --) { s.push(d[j]); } s.push(temp); i ++; } cout << sum <<endl; while(!s.empty()) s.pop(); memset(a , 0 , sizeof(a)); memset(b , 0 , sizeof(b)); memset(c , 0 , sizeof(c)); }}
0 0
- Good Bye 2014 C.(栈模拟)
- Good Bye 2014 A.(模拟)
- Good Bye 2016 C. New Year and Rating(模拟)
- Good Bye 2014(codeforces)
- Good Bye 2014 A B C
- Codefores Good Bye 2014 - A,B,C
- Codeforces Good Bye 2016 C. New Year and Rating(模拟)
- Good Bye 2014 B.(Floyd+暴力)
- A Good Bye 2014(CF)
- Good Bye 2014
- Good Bye 2014
- Codeforces Good Bye 2014
- codeforces good bye 2014
- codeforces Good Bye 2014
- Codeforces Good Bye 2014
- Good Bye 2014
- Good Bye 2014 题解
- Codeforces Good Bye 2014 解题报告 (A B C D)
- DOM(Document Object Model)解析 入门
- javascript字符串限制输出 或 struts2标签限制
- 玻璃纤维短切丝简介以及优势
- linux网络管理命令-网络配置
- 32Spannable的使用(Android显示html带图片 (表情开发))
- Good Bye 2014 C.(栈模拟)
- linux网络管理命令-网络测试
- eoj1817 dijkstra单元最短路径 普通方法+二叉堆优化
- Python学习之抽象
- linux网络管理命令-网络应用
- Java安全层与OSGi
- SpringMVC与freemarker整合
- linux网络管理命令-高级网络指令
- 网页抓取与处理的一些方法