当前位置:网站首页>Zcmu--1052: holedox eating (C language)
Zcmu--1052: holedox eating (C language)
2022-06-22 02:54:00 【Little why】
Description
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
Sample Output
#include <stdio.h>
int a[100005]; // Used to record the amount of food at each coordinate point
int main()
{
int t,l,q,s,x,f,z,i,s1,s2,y1,y2,p,c=0;
scanf("%d",&t);
while(t--){
c++; // Test case number
scanf("%d%d",&l,&q);
for(i=0;i<=l;i++) a[i]=0; // initialization 0
x=0,f=1,s=0; //x Represents the current location ,f Represents the last direction (1 For the right ,-1 To the left ),s Represents the total moving distance
while(q--){
scanf("%d",&z);
if(z==0){
scanf("%d",&p);
a[p]++; // Number of food in corresponding position +1
}else{
s1=0,s2=0; //s1,s2 On behalf of the protagonist On the right On the left Is there any food
for(i=x;i<=l;i++){ // Look for the right side first
if(a[i]>=1){ // If there is
y1=i; //y1 Record the current food location
s1=1; //s1 There is food on the right
break;
}
}
for(i=x-1;i>=0;i--){ // Look for the left
if(a[i]>=1){
y2=i;
s2=1;
break;
}
}
if(s1*s2==0){ // Multiply by 0 It means there is no food on either side , Or on the left , Or on the right
if(s1==1){ // On the right
s+=y1-x; // Cumulative distance
a[y1]--; // Quantity of food at this location -1
x=y1; // Update the current lead position
f=1; // The direction is right
}
else{ // On the left
s+=x-y2;
x=y2;
a[y2]--;
f=-1; // The direction is left
}
}else{ // This is the case for both sides , To compare distances
if(y1-x<x-y2){ // Right near
s+=y1-x;
x=y1;
a[y1]--;
f=1;
}else if(y1-x>x-y2){ // Left near
s+=x-y2;
x=y2;
a[y2]--;
f=-1;
}else{ // As close as , according to f To determine whether to go to the left or the right
if(f==1) s+=y1-x,x=y1,a[y1]--;
else s+=x-y2,x=y2,a[y2]--;
}
}
}
}
printf("Case %d: %d\n",c,s);
}
return 0;
}边栏推荐
- Relative references must start with either “/“, “./“, or “../“.
- Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga
- linux下安装mysql8及使用(转载)
- [go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network
- ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment
- 【6. 高精度乘法】
- Penetration testing - logic vulnerability topic
- web框架概述与程序开发
- Moorish voting
- 记一则服务器内存泄漏解决过程
猜你喜欢

JS special effects in the construction of animated web pages

ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment

Graphacademy course explanation: Fundamentals of neo4j graph data science

The latest official product of domestic brand oppo! This ppt report! It really refreshes my understanding of it

微软 IE 浏览器于 6 月 15 日被永久关闭

Use of day19qpushbutton 2021-10-30

圖數據庫ONgDB Release v-1.0.2

360EDR刨析

C3-qt realize Gobang games (I) November 7, 2021

Unity3d post process volume profile
随机推荐
圖數據庫ONgDB Release v-1.0.2
C2-qt serial port debugging assistant 2021.10.21
Adaptive batch job scheduler: automatically derive parallelism for Flink batch jobs
Day13QMainWindow2021-09-28
Architecture and practice of vivo container cluster monitoring system
银联支付 返回商户 Nignx post请求405
Use of day19qpushbutton 2021-10-30
Neo4j 技能树正式发布,助你轻松掌握Neo4j图数据库
GraphAcademy 课程讲解:《Neo4j 图数据科学简介》
Mysql 字段类型以及对应的长度 & 字节
All the knowledge you want to know about the PMP Exam is here
Is the link of Hengtai securities VIP low commission account opening safe?
[9. submatrix sum]
Latest release: neo4j figure data science GDS 2.0 and aurads GA
The brand, products and services are working together. What will Dongfeng Nissan do next?
Li Kou today's question 1108 IP address invalidation
Figure data platform solution: single node deployment
[pit encountered in docekr learning]
Moorish voting
JS操作节点经典案例(三级联动)