Codeforces 500C New Year Book Reading
2014-12-31 05:21
459 查看
C. New Year Book Reading
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
He lifts all the books above book x.
He pushes book x out of the stack.
He puts down the lifted books without changing their order.
After reading book x, he puts book x on the top of the stack.
View Code
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
He lifts all the books above book x.
He pushes book x out of the stack.
He puts down the lifted books without changing their order.
After reading book x, he puts book x on the top of the stack.
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1010; int n , m , w , d ; bool vis ; int main() { while( cin >> n >> m ) { for( int i = 1 ; i <= n ; ++i ) cin >> w[i] ; for( int i = 1 ; i <= m ; ++i ) cin >> d[i] ; LL ans = 0 ; for( int i = 1 ; i <= m ; ++i ) { memset( vis , false , sizeof vis ); for( int j = i - 1 ; j > 0 ; --j ){ if( d[i] == d[j] ) break ; if( !vis[ d[j] ] ) { ans += w[ d[j] ] ; vis[ d[j] ] = true ; } } } cout << ans << endl ; } }
View Code
相关文章推荐
- 【CodeForces】500C - New Year Book Reading(贪心)
- codeforces 500C New Year Book Reading (贪心+思维)
- codeforces 500c New Year Book Reading 【思维】
- Codeforces 500C:New Year Book Reading(思维+贪心?)
- 【Goodbye2014】Codeforces 500C New Year Book Reading【贪心+模拟】
- Codeforces 500C New Year Book Reading
- CodeForces 500C New Year Book Reading
- codeforces 500C New Year Book Reading(取书)
- codeforces 500C New Year Book Reading
- CodeForces 500C New Year Book Reading(贪心+思维)
- codeforces 500C New Year Book Reading (贪心,很好的思维题)
- Codeforces 500C New Year Book Reading
- 【codeforces】New Year Book Reading
- Codeforce 500c New Year Book Reading
- CF 500C New Year Book Reading
- CodeForces 500 C. New Year Book Reading(贪心)
- CODE FORCE 500C (New Year Book Reading) (贪心or 模拟)
- cf (dfs,floyd,并查集). New Year Book Reading
- New Year Book Reading
- CF 500 C. New Year Book Reading 贪心 简单题