当前位置:网站首页>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;
}边栏推荐
- Using JMeter for web side automated testing
- Starting from the classification of database, I understand the graph database
- How to select the appropriate version of neo4j (version 2022)
- Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga
- Unity3d post process volume profile
- Graphacademy course explanation: introduction to neo4j graph data science
- Conference chat room - development documents
- Graphconnect 2022 at a glance
- [7. high precision division]
- Is the link of Hengtai securities VIP low commission account opening safe?
猜你喜欢

table标签的不规则布局

【Percona-Toolkit】系列之pt-table-checksum和pt-table-sync 数据校验修复神器

图数据平台解决方案:集群部署
![[go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network](/img/91/324d04dee0a191725a0b8751375005.jpg)
[go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network

GraphConnect 2022 大会的产品发布一览

Figure base de données ongdb version V - 1.0.2

自动化工具-监测文件的变化

【9. 子矩阵和】

xpm_ memory_ A complete example of using the tdpram primitive
![[2. merge sort]](/img/60/5e87cffabd91af0155ae681f5bf0ba.png)
[2. merge sort]
随机推荐
Game jam development cycle
GraphConnect 2022 大会的产品发布一览
【4. 高精度加法】
微软 IE 浏览器于 6 月 15 日被永久关闭
【5. 高精度减法】
Figure data platform solution: cluster deployment
Graphacademy course explanation: introduction to neo4j graph data science
Brief analysis of application source code of neo4j intelligent supply chain
[Shangshui Shuo series] day two
Li Kou today's question 1108 IP address invalidation
Architecture and practice of vivo container cluster monitoring system
图数据库ONgDB Release v-1.0.2
Sword finger offer 57 Next node of binary tree
Adaptive batch job scheduler: automatically derive parallelism for Flink batch jobs
[9. submatrix sum]
JS操作节点经典案例(三级联动)
GetEmptyBlcoksPre Info
Memory hierarchy introduction
[1. quick sort]
Latest release: neo4j figure data science GDS 2.0 and aurads GA