hdu4302贪吃蛇

author author     2022-09-09     222

关键词:

贪吃蛇

Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is.  

INPUT

The input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events.
The next n lines, each line describes an event. 0 x(0<=x<=L, x is a integer) represents a piece of cake appears in the x position; 1 represent Holedox wants to eat a cake.
In each case, Holedox always starts off at the position 0. 

OUTPUT

Output the total distance Holedox will move. Holedox don’t need to return to the position 0.

Sample Input

3
10 8
0 1
0 5
1
0 2
0 0
1
1
1

10 7
0 1
0 5
1
0 2
0 0
1
1

10 8
0 1
0 1
0 5
1
0 2
0 0
1
1

Sample Output

Case 1: 9
Case 2: 4
Case 3: 2

 

思路:开始尝试使用数组,超时,然后考虑各种数据结构,没错,就是使用优先队列,一下子就AC了。。

//听说网上还有2种做法,一定要去了解一下

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

int main()
{
    int t;
    cin >> t;
    for(int T = 1; T <= t; T++)
    {
        priority_queue<int> s;
        priority_queue<int, vector<int>, greater<int> > ss;
        int she = 0;
        long long sum = 0;
        int l, n;
        int fangxiang = 1;//1为右,0为左
        cin >> l >> n;
        while(n--)
        {
            int c;
            cin >> c;
            if(c == 0)
            {
                int x;
                cin >> x;
                if(x > she)ss.push(x);
                else s.push(x);
            }
            else
            {


                if(!s.empty() && !ss.empty() && ss.top() - she > she - s.top()){sum += she - s.top();she = s.top();fangxiang = 0;s.pop();continue;}
                if(s.empty() && !ss.empty()){sum += ss.top() - she;she = ss.top();fangxiang = 1;ss.pop();continue;}
                if(!s.empty() && !ss.empty() &&ss.top() - she < she - s.top()){sum += ss.top() - she;she = ss.top();fangxiang = 1;ss.pop();continue;}
                if(!s.empty() && ss.empty()){sum += she - s.top();she = s.top();fangxiang = 0;s.pop();continue;}
                if(!s.empty() && !ss.empty() && ss.top() - she == she - s.top() )
                {
                    if(fangxiang == 1)
                    {
                        sum += ss.top() - she;
                        she = ss.top();
                        ss.pop();
                    }
                    else
                    {
                        sum += she - s.top();
                        she = s.top();
                        s.pop();

                    }
                }
            }
        }
        cout << "Case " << T << ": " << sum << endl;
    }
    return 0;
}

 

bzoj4302hdu5301buildings

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4302【题解】出自2015多校-学军题意大概是给出一个n*m的格子有一个格子(x,y)是坏的,用一些矩形覆盖没有坏的格子,使得每个矩形都有一面靠着边界。求最大的矩形的面积最小。稍微... 查看详情

使用vb创建贪吃蛇

    贪吃蛇作为一个经典游戏,在其开发成功后,有很长一段时间令很多人为之振奋,但随着时间的流逝,贪吃蛇也逐渐淡出人们的视野。本次,我利用VB重现贪吃蛇的创建。主要使用到的控件:Label标签,Command按... 查看详情

一条贪吃蛇的使命——零基础入门贪吃蛇游戏(代码片段)

零基础入门贪吃蛇游戏贪吃蛇是一款最常见、最经典、最受欢迎的小游戏之一。本篇文章带你零基础实现贪吃蛇游戏,一条蛇的使命从这里开始。1、游戏描述????贪吃蛇是一款非常经典的休闲类游戏。在一块固定大小的区域内,... 查看详情

一条贪吃蛇的使命——零基础入门贪吃蛇游戏(附演示地址)(代码片段)

零基础入门贪吃蛇游戏贪吃蛇是一款最常见、最经典、最受欢迎的小游戏之一。本篇文章带你零基础实现贪吃蛇游戏,一条蛇的使命从这里开始。演示地址:贪吃蛇演示,可能会提示危险操作,请忽略,放心访问。1、游戏描述??... 查看详情

“贪吃蛇”

“贪吃蛇”小游戏HTML5贪吃蛇游戏页面结构以保存对javascript的些许理解/****************//*****game.js****//****************/Array.prototype.remove = function (obj) {    for (var i 查看详情

贪吃蛇

  无聊发现自己没写过贪吃蛇,于是做了一个简单的贪吃蛇,并不打算做完整版的(只能看到贪吃蛇不断的从上往下移动)。。  效果(截图软件的问题,会有前一帧的印记):  代码:#include<iostream>#include<windows.h... 查看详情

我也来写一个贪吃蛇

...作量好大,好忙,趁周末练练手,花了近3小时写了一个贪吃蛇。  实现贪吃蛇的功能很简单。  我就分享一下我实现贪吃蛇看起来在界面上移动并且吃食物长大的原理。  我建了一个数组list_arr[]来保存贪吃蛇所在的每个... 查看详情

结对-贪吃蛇游戏-测试过程

贪吃蛇游戏托管平台地址:https://gitee.com/a540816440/codes/kuizlv3wfc5eyx8gopmhd70功能测试:移动功能,测试方法:使用键盘上的,方向键,是否贪吃蛇可以移动。  功能测试:分数功能测试方法:当贪吃蛇吃掉食物是否分数会有增加 ... 查看详情

结对-贪吃蛇游戏-需求分析

结对编程--贪吃蛇需要安装pythonpygame需求分析:玩家用键盘“WASD”来控制贪吃蛇的方向,          贪吃蛇碰壁结束游戏,主要完成游戏的开始按钮,暂停按钮,退出按钮等功能 查看详情

结对-贪吃蛇游戏-需求分析

结对编程--贪吃蛇需要安装pythonpygame需求分析:玩家用键盘“WASD”来控制贪吃蛇的方向,          贪吃蛇碰壁结束游戏,主要完成游戏的开始按钮,暂停按钮,退出按钮等功能 查看详情

经典游戏还原之:贪吃蛇

...nitymaker)您可以自由转载,但必须加入完整的版权声明!贪吃蛇与方块主要玩法:贪吃蛇吃食物,吃到食物后根据相应数值增加身体长度,如果贪吃蛇碰到方块后,根据方块的数值逐渐减少贪吃蛇 查看详情

《结对-贪吃蛇-设计文档》

项目:贪吃蛇游戏,所用软件,eclipse成员:孙晨旭,高云鹏贪吃蛇游戏设计文档:搭建环境:AndroidStudio,eclipse“贪吃蛇”游戏是一个经典的游戏,它操作简单、界面美观、功能较齐全的“贪吃蛇”游戏。整个游戏程序分为二个... 查看详情

自制贪吃蛇游戏中的几个“大坑”(代码片段)

  贪吃蛇游戏已经告一段落了,在完成这个游戏的过程中,我遭遇了许多“坎坷”和“挫折”,下面就几个让我印象深刻的“挫折”做一个具体的讲解,以此来为这个贪吃蛇项目画上一个完整句号。(包括打包这个游戏时遇到... 查看详情

javascrip写的贪吃蛇

原生JavaScript写的贪吃蛇这个游戏好像就如同helloworld一样,简单又有代表性,以前我学习c语言的时候,第一个做的趣味小游戏也是贪吃蛇---------------------------------1//贪吃蛇的食物模块2(function(){3varelements=[]4//创建一个食物的构造... 查看详情

贪吃蛇需求分析

Partone项目题目 贪吃蛇游戏(单词版)Parttwo选题背景和意义 作为一个经典的游戏,贪吃蛇设计简单,实用和娱乐性高,是90后的我们童年的美好回忆。对于贪吃蛇传统的玩法,大家众所周知,即:玩家通过控制游戏手柄... 查看详情

gui简单实战——贪吃蛇(代码片段)

将前面学到的GUI基础知识完成实战,完成一个简单的贪吃蛇项目项目功能用键盘上下左右实现贪吃蛇的自动移动贪吃蛇吃到食物后,长度加一,分数加一贪吃蛇吃到自己的身体,则游戏结束按空格键实现游戏的暂停和继续效果截... 查看详情

结对-结对编项目贪吃蛇-设计文档

项目名称:贪吃蛇成员:张立新、李根使用工具:python目标:1.使用pygame模块,开发制作贪吃蛇游戏。   蛇头碰到食物时加长蛇身,蛇头碰到蛇身或者边界GOMEOVER(GG)。   2.每吃到一个食物加一分。游戏控制:1.暂停。... 查看详情

贪吃蛇版本1.0

学号(2017197);姓名:王浩铭我的码云贪吃蛇项目仓库:https://gitee.com/wojiubufu/projects今天尝试了给贪吃蛇添加背景音乐,预计时间十五分钟,结果花费的时间比预计多了好多,感受到了自己的不足,还有很长的路要走 查看详情