codeforces New Year Book Reading
2015-01-20 21:14
411 查看
题意是 堆放N本书, 每本书有个重量,然后他有M次要看的书,一本书可能看多次,每次他在从上到下找到要读的书,然后把这本书上面的书都移开,花费为总重量,然后把这本拿出来,放在最上面。 让你找个序列 使得看完M次过后 花费最小。
我直接贪心,我是在纸上假设 只有3本书,然后 重量分别是X,Y,Z,然后用不同的顺序堆放书,最后发现,按照读书的顺序堆放书是最优的。
数据范围不大,这样就可以直接模拟做了。
我直接贪心,我是在纸上假设 只有3本书,然后 重量分别是X,Y,Z,然后用不同的顺序堆放书,最后发现,按照读书的顺序堆放书是最优的。
数据范围不大,这样就可以直接模拟做了。
///AC 15MS #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; int n,m; int w[510]; int b[1010]; int s[510],t[510]; int main() { while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&w[i]); memset(s,-1,sizeof(s)); int ix=0; for(int i=1;i<=m;i++) { scanf("%d",&b[i]); if(s[b[i]]==-1) { t[++ix]=b[i]; s[b[i]]=1; } } int ans=0; for(int i=1;i<=m;i++) { int tmp=0; int id; for(int j=1;j<=n;j++) { if(t[j]==b[i]) { ans+=tmp; id=j; break; } else { tmp+=w[t[j]]; } } t[0]=t[id]; for(int j=id;j>1;j--) { t[j]=t[j-1]; } t[1]=t[0]; } printf("%d\n",ans); } return 0; }
相关文章推荐
- C - New Year and Hurry CodeForces - 750A(动态规划)
- CodeForces 500C New Year Book Reading
- New Year and Old Property CodeForces - 611B
- Codeforces GoodBye2015 New Year and Three Musketeers Codeforces 611E(贪心)
- I - New Year and Rating CodeForces - 750C 模拟
- E - New Year and North Pole CodeForces - 750B(状态处理下就好了)
- 【转】Codeforces GoodBye2015 New Year and Three Musketeers Codeforces 611E(贪心)
- 【Goodbye2014】Codeforces 500C New Year Book Reading【贪心+模拟】
- New Year Book Reading
- New Year Tree CodeForces - 620E(dfs序+线段树)
- CF 500 C. New Year Book Reading 贪心 简单题
- New Year Tree CodeForces - 620E
- codeforces goodbye 2016 C. New Year and Rating
- Codeforces 500C New Year Book Reading
- New Year Book Reading(贪心)
- 【Good Bye 2014C】【脑洞】New Year Book Reading 书放一摞依次看 最佳顺序使费用最小
- New Year and Curling CodeForces - 908C
- New Year and Rating CodeForces - 750C
- cf (dfs,floyd,并查集). New Year Book Reading
- Codeforces 500C:New Year Book Reading(思维+贪心?)