本题是很好的双指针练习题。

关于双指针,详见洛谷日报#73

我们可以用两个指针l和r表示题中两人接下来要比较的数字,用fl标记下一个将要取的人,并分别用两个计数器统计双方的答案。

因此,我们有了如下AC代码:

 #include <bits/stdc++.h>//万能头文件

 using namespace std;//使用标准名字空间

 inline int read()//快速读入
{
int f=,x=;
char c=getchar(); while(c<'' || c>'')
{
if(c=='-')f=-;
c=getchar();
} while(c>='' && c<='')
{
x=x*+c-'';
c=getchar();
} return f*x;
} int n,a[]/*输入的数*/,ans[]/*答案*/,l/*左指针*/,r/*右指针*/,fl/*标记*/; int main()
{
n=read();//输入n for(register int i=; i<=n; i++)
{
a[i]=read();//输入每个数
} l=,r=n;//初始化指针 while(n--)//如果还有牌取
{
if(fl==)//如果是第一个人取
{
if(a[l]<a[r])//如果右边的比左边的牌大
{
ans[]=ans[]+a[r];//累加 --r;//移动指针
}
else//如果右边的牌比左边的小(或者等于)
{
ans[]=ans[]+a[l];//累加 ++l;//移动指针
} fl=;//接下来是第二个人取
}
else//如果是第二个人取
{
//代码和第一个人取几乎一样,只是改了累加的计数器
if(a[l]<a[r])
{
ans[]=ans[]+a[r]; --r;
}
else
{
ans[]=ans[]+a[l]; ++l;
} fl=;//接下来是第一个人取
}
} printf("%d %d",ans[],ans[]);//输出,注意顺序! return ;//完美结束
}

题解 【Codeforces381A】 Sereja and Dima的更多相关文章

  1. Codeforces Round #223 (Div. 2)--A. Sereja and Dima

    Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. Codeforces Round #223 (Div. 2) A

    A. Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. Codeforces 380D Sereja and Cinema (看题解)

    Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...

  4. Codeforces Little Dima and Equation 数学题解

    B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. 【题解】CF374C Inna and Dima

    题面传送门 解决思路 本题是找最长路的图上问题,所以先考虑如何建图. 首先把每一个字母转化为数字,然后对于每一个点枚举四个方向,如果有下一个字母,就向那个点建一条边,可以用 \(vector\) 存图 ...

  6. CodeForces - 425E Sereja and Sets 题解

    题目大意: 我们有一个集合 S,其中包含了 m 个不完全相同的区间[l1,r1],[l2,r2]…[lm,rm] (1≤li≤ri≤n,li,ri 都为整数). 定义 f(S)=k,表示集合 S 中能 ...

  7. CF460B Little Dima and Equation (水题?

    Codeforces Round #262 (Div. 2) B B - Little Dima and Equation B. Little Dima and Equation time limit ...

  8. Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合

    题目链接: http://codeforces.com/problemset/problem/272/D D. Dima and Two Sequences time limit per test2 ...

  9. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes map

    B. Sereja and Suffixes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

随机推荐

  1. 产生随机数(rand()函数和srand()函数)

    有时候,我们需要随机产生一个在某范围的数,C/C++提供了一个库函数rand()来产生随机数. 函数原型:int rand(void); 功能:返回一个[0,RAND_MAX]间的随机整数.其中RAN ...

  2. Codeforces Round #617 (Div. 3) 补题记录

    1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需 ...

  3. Wannafly Winter Camp 2020 Day 5H Geometry PTSD - 构造,计算几何

    Solution 考虑如何计算点到平面的距离,我们可以用行列式算出锥形的体积和底面的面积,来得到距离 考虑构造一个等边三角形,这样基本是满足 \(\geq 1.7\) 的条件的 于是我们对它的坐标枚举 ...

  4. PP: Taking the human out of the loop: A review of bayesian optimization

    Problem: Design problem parameters consist of the search space of your model. Scientists design expe ...

  5. IDEA 找不到包或者找不到符号的一些解决办法

    有时使用IDE导入项目后,启动时会发生找不到包或者找不到符号的情况,下面有一些处理方法 1.右键项目Maven→Reimport 2.IDEA窗口左上角File→Invalidate and Rest ...

  6. NSSM 将jar 安装成windows服务

    1.下载 nssm (下载地址: http://www.nssm.cc/release/nssm-2.24.zip ) 2.将打包好的jar包放到一个文件夹中,编写run.bat文件,run.bat内 ...

  7. LaTeX技巧010:LaTtex中如何给每个句子加序号?

    效果图: 代码: \documentclass{article} \newcounter{sentence} \renewcommand\thesentence{\textsuperscript{\a ...

  8. RHEL/CentOS 7中Nginx的systemd service

    源码安装的nginx ,没有systemd service 管理 nginx 下面教程,告诉你如何设置nginx 的systemd service nginx systemd的服务文件是/usr/li ...

  9. mac或windows下Navicat Premium安装

    找了很多地址都乱七八糟的说明一通还没啥用,好不容易找到一个靠谱的,记录一下,也方便一下和我一样苦苦寻找的人,亲测有用 https://www.52pojie.cn/thread-727433-1-1. ...

  10. JDBC没有导入驱动jar包