B. Mark the Dust Sweeper

Mark is cleaning a row of 𝑛n rooms. The 𝑖i-th room has a nonnegative dust level 𝑎𝑖ai. He has a magical cleaning machine that can do the following three-step operation. 

  • Select two indices 𝑖<𝑗i<j such that the dust levels 𝑎𝑖ai, 𝑎𝑖+1ai+1, ……, 𝑎𝑗−1aj−1 are all strictly greater than 00. 
  • Set 𝑎𝑖ai to 𝑎𝑖−1ai−1. 
  • Set 𝑎𝑗aj to 𝑎𝑗+1aj+1. 

Mark's goal is to make 𝑎1=𝑎2=…=𝑎𝑛−1=0a1=a2=…=an−1=0 so that he can nicely sweep the 𝑛n-th room. Determine the minimum number of operations needed to reach his goal.

Input

The first line contains a single integer 𝑡t (1≤𝑡≤1041≤t≤104) — the number of test cases.

The first line of each test case contains a single integer 𝑛n (2≤𝑛≤2⋅1052≤n≤2⋅105) — the number of rooms.

The second line of each test case contains 𝑛n integers 𝑎1a1, 𝑎2a2, ..., 𝑎𝑛an (0≤𝑎𝑖≤1090≤ai≤109) — the dust level of each room.

It is guaranteed that the sum of 𝑛n across all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print a line containing a single integer — the minimum number of operations. It can be proven that there is a sequence of operations that meets the goal.

Example

input

Copy

 

4

3

2 0 0

5

0 2 0 2 0

6

2 0 3 0 4 6

4

0 0 0 10

output

Copy

3
5
11
0

Note

In the first case, one possible sequence of operations is as follows. 

  • Choose 𝑖=1i=1 and 𝑗=2j=2, yielding the array [1,1,0][1,1,0]. 
  • Choose 𝑖=1i=1 and 𝑗=3j=3, yielding the array [0,1,1][0,1,1]. 
  • Choose 𝑖=2i=2 and 𝑗=3j=3, yielding the array [0,0,2][0,0,2]. 

At this point, 𝑎1=𝑎2=0a1=a2=0, completing the process.

In the second case, one possible sequence of operations is as follows. 

  • Choose 𝑖=4i=4 and 𝑗=5j=5, yielding the array [0,2,0,1,1][0,2,0,1,1]. 
  • Choose 𝑖=2i=2 and 𝑗=3j=3, yielding the array [0,1,1,1,1][0,1,1,1,1]. 
  • Choose 𝑖=2i=2 and 𝑗=5j=5, yielding the array [0,0,1,1,2][0,0,1,1,2]. 
  • Choose 𝑖=3i=3 and 𝑗=5j=5, yielding the array [0,0,0,1,3][0,0,0,1,3]. 
  • Choose 𝑖=4i=4 and 𝑗=5j=5, yielding the array [0,0,0,0,4][0,0,0,0,4]. 

In the last case, the array already satisfies the condition.

题目讲的无比不清楚,研究半天才研究明白🧐

就是选择i和j使得a【i】-1,a【j】+1,但是,在移动过程中要保证,a【j】>0;

那么长的题目就这么点信息

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<map>
#include<cstring>
#include<queue>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
ll gcdd(ll a, ll b)
{
	if (b) while ((a %= b) && (b %= a));
	return a + b;
}

ll t,n,m,a,b,cnt,ans;
ll arr[N];
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		ans=0;
		ll flag=0;
		cnt=0;
		for(int i=1;i<=n;i++)
		{
			cin>>arr[i];
			if(arr[i]!=0)
			{
				cnt=1;
			}
			if(cnt==1 && i<n )
			{
				if(arr[i]==0)
				{
					ans++;
				}else{
					ans+=arr[i];
				}
			}
		}
		cout<<ans<<endl;
	}
}

 

import pygame import math from pygame.sprite import Sprite class Robot(Sprite): def __init__(self, screen): # initialize robot and its location 初始化机器人及其位置 self.screen = screen # load image and get rectangle 加载图像并获取矩形 self.image = pygame.image.load('images/robot.png').convert_alpha() self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() # put sweeper on the center of window 把扫地机器人放在界面中央 self.rect.center = self.screen_rect.center # 初始角度 self.angle = 0 self.moving_speed = [1, 1] self.moving_pos = [self.rect.centerx, self.rect.centery] self.moving_right = False self.moving_left = False def blitme(self): # buld the sweeper at the specific location 把扫地机器人放在特定的位置 self.screen.blit(self.image, self.rect) def update(self, new_robot): # 旋转图片(注意:这里要搞一个新变量,存储旋转后的图片) new_robot.image = pygame.transform.rotate(self.image, self.angle) # 校正旋转图片的中心点 new_robot.rect = new_robot.image.get_rect(center=self.rect.center) self.moving_pos[0] -= math.sin(self.angle / 180 * math.pi) * self.moving_speed[0] self.moving_pos[1] -= math.cos(self.angle / 180 * math.pi) * self.moving_speed[1] self.rect.centerx = self.moving_pos[0] self.rect.centery = self.moving_pos[1] # 右转的处理 if self.moving_right: self.angle -= 1 if self.angle < -180: self.angle = 360 + self.angle # 左转的处理 if self.moving_left: self.angle += 1 if self.angle > 180: self.angle = self.angle - 360 # 上下边界反弹的处理 if (self.rect.top <= 0 and -90 < self.angle < 90) or ( self.rect.bottom >= self.screen_rect.height and (self.angle > 90 or self.angle < -90)): self.angle = 180 - self.angle # 左右边界反弹的处理 if (self.rect.left <= 0 and 0 < self.angle < 180) or ( self.rect.right >= self.screen_rect.width and (self.angle > 180 or self.angle < 0)): self.angle = - self.angle
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值