ACM Amman Collegiate Programming Contest C. Street Lamps
2017-06-04 11:12
381 查看
C. Street Lamps
Bahosain is walking in a street of Nblocks. Each block is either empty or has one lamp. If there is a lamp in a block, it will light it’s block and the direct adjacent blocks. For example, if there is a lamp at block
3, it will light the blocks 2, 3, and 4.
Given the state of the street, determine the minimum number of lamps to be installed such that each block is lit.
Input
The first line of input contains an integer T(1 ≤ T ≤ 1025)that represents the number of test cases.
The first line of each test case contains one integer N (1 ≤ N ≤ 100)that represents the number of blocks in the street.
The next line contains Ncharacters, each is either a dot
’.’or an asterisk ’*’.
A dot represents an empty block, while an asterisk represents a block with a lamp installed in it.
Output
For each test case, print a single line with the minimum number of lamps that have to be installed so that all blocks are lit.
题意:给出一个字符串,每个'*'相当于灯,它能照亮相邻两格,每个'.'代表没有灯的格子。问:至少还需要多少个灯才能照亮整条路。
解题思想:这个题是个模拟题。我的思路是先对字符串进行一次扫描,将所有带'*'的左右两格都点亮,然后再扫描一次,每三个'.'需要一个灯来点亮。于是,我创了一个flag数组来存整条路,0代表没照亮,1代表灯,2代表被灯照亮的地方。然后再对这个flag数组进行处理,遇到0区间段,小于三个连续的0,记一个,大于三个连续的0,即用0的个数除以3即可(取整)。以下是我的代码#include <stdio.h>
int main()
{
//freopen("in.txt","r",stdin);
int t,n;
char light[105];
int arr[105];
scanf("%d",&t);
while(t--)
{
for(int i=0;i<105;i++)
arr[i]=0;
scanf("%d",&n);
scanf("%s",light);
for(int i=0; i<n; i++)
{
if(light[i]=='*')
{
if(i>=1) arr[i-1]=2;
arr[i]=1;
arr[i+1]=2;
}
}
int sum=0,temp=0;
for(int i=0; i<n; i++)
{
if(!arr[i]) temp++;
else
{
if(temp>0)
{
sum=sum+temp/3;
if(temp%3) sum++;
temp=0;
}
}
}
if(temp>0)
{
sum=sum+temp/3;
if(temp%3) sum++;
temp=0;
}
printf("%d\n",sum);
}
return 0;
}
Bahosain is walking in a street of Nblocks. Each block is either empty or has one lamp. If there is a lamp in a block, it will light it’s block and the direct adjacent blocks. For example, if there is a lamp at block
3, it will light the blocks 2, 3, and 4.
Given the state of the street, determine the minimum number of lamps to be installed such that each block is lit.
Input
The first line of input contains an integer T(1 ≤ T ≤ 1025)that represents the number of test cases.
The first line of each test case contains one integer N (1 ≤ N ≤ 100)that represents the number of blocks in the street.
The next line contains Ncharacters, each is either a dot
’.’or an asterisk ’*’.
A dot represents an empty block, while an asterisk represents a block with a lamp installed in it.
Output
For each test case, print a single line with the minimum number of lamps that have to be installed so that all blocks are lit.
Sample Input | Sample Output |
3 | 2 |
6 | 0 |
...... | 1 |
3 | |
*.* | |
8 | |
.*.....* | |
解题思想:这个题是个模拟题。我的思路是先对字符串进行一次扫描,将所有带'*'的左右两格都点亮,然后再扫描一次,每三个'.'需要一个灯来点亮。于是,我创了一个flag数组来存整条路,0代表没照亮,1代表灯,2代表被灯照亮的地方。然后再对这个flag数组进行处理,遇到0区间段,小于三个连续的0,记一个,大于三个连续的0,即用0的个数除以3即可(取整)。以下是我的代码#include <stdio.h>
int main()
{
//freopen("in.txt","r",stdin);
int t,n;
char light[105];
int arr[105];
scanf("%d",&t);
while(t--)
{
for(int i=0;i<105;i++)
arr[i]=0;
scanf("%d",&n);
scanf("%s",light);
for(int i=0; i<n; i++)
{
if(light[i]=='*')
{
if(i>=1) arr[i-1]=2;
arr[i]=1;
arr[i+1]=2;
}
}
int sum=0,temp=0;
for(int i=0; i<n; i++)
{
if(!arr[i]) temp++;
else
{
if(temp>0)
{
sum=sum+temp/3;
if(temp%3) sum++;
temp=0;
}
}
}
if(temp>0)
{
sum=sum+temp/3;
if(temp%3) sum++;
temp=0;
}
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- 2015 ACM Amman Collegiate Programming Contest D.Alternating Strings【Dp】
- 2017 ACM Amman Collegiate Programming Contest
- 2015 ACM Amman Collegiate Programming Contest H.Bridges【边双联通+求树上最长链(树的直径)】
- 2015 ACM Amman Collegiate Programming Contest
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
- 2015 ACM Amman Collegiate Programming Contest I.Bahosain and Digits【思维+暴力枚举】
- 2015 ACM Amman Collegiate Programming Contest L.Alternating StringsII
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
- 2015 ACM Amman Collegiate Programming Contest训练总结【12/12】
- Codeforces Gym 2015 ACM Amman Collegiate Programming Contest
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
- GYM 2017 ACM Amman Collegiate Programming Contest
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
- CodeFroces GYM 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16) B.street(最短路)
- 2017 ACM Amman Collegiate Programming Contest 题解
- 2016 ACM Amman Collegiate Programming Contest 训练赛部分题解
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
- 2015 ACM Syrian Collegiate Programming Contest
- 2017 ACM Arabella Collegiate Programming Contest Monkeying Around
- 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16) 题解