codeforces Sereja and Dima 题解
来源:互联网 发布:mysql日期格式化 编辑:程序博客网 时间:2024/06/04 18:50
Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.
Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.
Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.
The first line contains integer n (1 ≤ n ≤ 1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to1000.
On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.
44 1 2 10
12 5
这里使用一下deque数据结构吧。当然这里使用一般数列,用two points的思想解决也是可以的。
deque是可以两头取数都很快的容器。很适合本题这样的情况
#include <deque>#include <string>#include <iostream>using namespace std;void SerejaandDima(){int n, a;cin>>n;deque<int> deq;while (n--){cin>>a;deq.push_back(a);}int Sereja = 0, Dima = 0;bool turn = true;while (deq.size()){if (deq.front() > deq.back()){if (turn) Sereja += deq.front();else Dima += deq.front();deq.pop_front();}else{if (turn) Sereja += deq.back();else Dima += deq.back();deq.pop_back();}turn = !turn;}cout<<Sereja<<' '<<Dima;}
- codeforces Sereja and Dima 题解
- A. Sereja and Dima
- Sereja and Dima
- Sereja and Dima
- codeforces--2014/1/13--A. Sereja and Dima
- Codeforces 381A Sereja and Dima(模拟)
- Codeforces Round #223 (Div. 2)--A. Sereja and Dima
- Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs
- Codeforces Little Dima and Equation 数学题解
- CF223A题Sereja and Dima
- Codeforces Round #223 (Div. 2)——A. Sereja and Dima
- Codeforces 367D Sereja and Sets 题解&代码
- cf-381A-Sereja and Dima
- Codeforces Sereja and Mirroring
- codeforces Sereja and Mugs
- Codeforces Dima and Guards
- 【codeforces】Dima and Guards
- codeforces Dima and Salad
- python学习笔记(1)--《python基础教程》第1章内容总结
- HDU 1002.A + B Problem II
- HashMap实现缓存
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
- 常用库函数摘要
- codeforces Sereja and Dima 题解
- ACM-字符串处理之Substrings——hdu1238
- nodejs详解
- 拆解报告:谷歌眼镜硬件成本约80美元
- mongodb使用笔记
- 设计模式(笔记)概述
- hdu 1045
- 美政府挑选三家私企开发商业月球着陆器
- 网站加载优化