地址:点击打开链接
A. Water The Garden
It is winter now, and Max decided it's about time he watered the garden.
The garden can be represented as n consecutive garden beds, numbered from 1 to n. k beds contain water taps (i-th tap is located in the bed xi), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed xi is turned on, then after one second has passed, the bed xi will be watered; after two seconds have passed, the beds from the segment [xi - 1, xi + 1] will be watered (if they exist); after j seconds have passed (j is an integer number), the beds from the segment [xi - (j - 1), xi + (j - 1)] will be watered (if they exist). Nothing changes during the seconds, so, for example, we can't say that the segment [xi - 2.5, xi + 2.5] will be watered after 2.5seconds have passed; only the segment [xi - 2, xi + 2] will be watered at that moment.
Max wants to turn on all the water taps at the same moment, and now he wonders, what is the minimum number of seconds that have to pass after he turns on some taps until the whole garden is watered. Help him to find the answer!
The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 200).
Then t test cases follow. The first line of each test case contains two integers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n) — the number of garden beds and water taps, respectively.
Next line contains k integers xi (1 ≤ xi ≤ n) — the location of i-th water tap. It is guaranteed that for each condition xi - 1 < xiholds.
It is guaranteed that the sum of n over all test cases doesn't exceed 200.
Note that in hacks you have to set t = 1.
For each test case print one integer — the minimum number of seconds that have to pass after Max turns on some of the water taps, until the whole garden is watered.
3
5 1
3
3 3
1 2 3
4 1
1
3
1
4
The first example consists of 3 tests:
题意:
有一排田地,有k个田地具有水管,且有水管的地的水向边两边进行蔓延,问最少多长时间能够浇完所有地?
思路:
模拟一遍,一遍一遍的从头开始走
代码:
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x7f7f7f7f
#define mod 1000000009
#define maxn 200 +100
using namespace std;
int a[maxn];
int b[maxn];//注意体会b
int l;
int main(){
int n,k;
int t;
cin>>t;
while(t--){
cin>>n>>k;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=1;i<=k;i++){
cin>>l;
a[l]=1;
b[l]=1;
}
bool flag=false;
int ans=1;
while(true){
flag=false;
for(int i=1;i<=n;i++){
if(b[i]==0) flag=true;
if(b[i]==1){
a[i-1]=1;
//b[i-1]=1;
a[i+1]=1;
//b[i+1]=1;
}
}
for(int i=1;i<=n;i++) {
b[i]=a[i];
}
if(!flag) break;
ans++;
}
cout<
B. Tea Queue
题意:有n个人想要喝茶,每个人都有离开的时间和到达的时间,要求输出每个人喝到茶的时间,若没有喝茶,则输出0。
思路:注意记录一下上一个人喝茶的时间,然后再和上一个人的时间进行比较即可,大致分为三种情况,详见代码:
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x7f7f7f7f
#define mod 1000000009
#define maxn 2000+100
using namespace std;
int a[maxn];
int l[maxn],r[maxn];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
memset(a,0,sizeof(a));
scanf("%d",&n);
int cur=0;
for(int i=1;i<=n;i++){ scanf("%d%d",&l[i],&r[i]);}
for(int i=1;i<=n;i++){
if(l[i]>=cur){
a[i]=l[i];
cur=l[i]+1;
}
else if(r[i]
C. Swap Adjacent Elements
题意:
有n个数1-n,前n-1个数被0或则1标记,若为1那么可以与前1个数字进行交换,问能否序列上升?
思路:
只有当某个数到他的位置之间数全部为1才可以,否则就会中断
代码:
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x7f7f7f7f
#define mod 1000000009
#define maxn 200000+100
using namespace std;
int n,m,tt,b[200005],vis[200005],x,y,sum[200005];
struct Node
{
int x,id;
}a[200005];
bool cmp(Node a,Node b)
{
return a.x>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i].x);
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
a[i].x=i;
vis[i]=a[i].id;
}
sort(a+1,a+n+1,cmp2);
cin>>s;
sum[0]=s[0]-'0';
for(int i=1;i