标签:
题目描述:
大致是这样的,给你长度为n的序列,每个序列上的数代表半径为2的范围内有多少个地雷,问你至多能确定多少个位置的放置,并输出一定有雷的个数及位置序列、一定没有雷的个数及位置序列
思路:
题目数据范围不是很大,O(n)吧
第一个位置确定后,按照arr[1]来说,第二个位置也就确定了
同理,对于arr[2]来说,第三个位置也就确定了.....
但是还是WA了两次,因为他问的是一定确定的!
这里就有一种情况,第一个位置放雷和不放雷都可行,那么虽然满足,但是有些位置是不确定的!
所以flag1,flag2都要判一下
顺便vector真好用
附上代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#pragma warning(disable:4996)
#define Zero(a) memset(a, 0, sizeof(a))
#define Neg(a) memset(a, -1, sizeof(a))
#define All(a) a.begin(), a.end()
#define PB push_back
#define repf(i,a,b) for(i = a;i <= b; i++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,n,1
#define ll long long
#define MAXN 100005
#define mod 1000000007
using namespace std;
int n, num[MAXN];
int ray1[MAXN],ray2[MAXN];
vector<int>have, Nhave;
bool rraay(int temp, int a[])
{
a[0] = 0;
a[1] = temp;
for (int i = 2; i <= n; i++)
{
a[i] = num[i - 1] - a[i - 1] - a[i - 2];
if (a[i]>1 || a[i]<0)
return false;
}
if (num[n] == a[n] + a[n - 1])
return true;
else
return false;
}
void init(){
scanf("%d", &n);
have.clear();
Nhave.clear();
for (int i = 1; i <= n; ++i){
scanf("%d", &num[i]);
}
}
void PT(){
int len = have.size();
printf("%d", len);
for (int i = 0; i < len; ++i){
printf(" %d", have[i]);
}
printf("\n");
len = Nhave.size();
printf("%d", len);
for (int i = 0; i < len; ++i){
printf(" %d", Nhave[i]);
}
printf("\n");
}
void solve(){
bool flag1 = rraay(0, ray1);
bool flag2 = rraay(1, ray2);
if (flag1 && !flag2){
for (int i = 1; i <= n; ++i){
if (ray1[i] == 1) have.push_back(i);
else Nhave.push_back(i);
}
}
if (!flag1 && flag2){
for (int i = 1; i <= n; ++i){
if (ray2[i] == 1) have.push_back(i);
else Nhave.push_back(i);
}
}
if (flag1 && flag2){
for (int i = 1; i <= n; ++i){
if (ray1[i] == ray2[i]){
if (ray1[i] == 1) have.push_back(i);
else Nhave.push_back(i);
}
}
}
PT();
}
int main(){
int T;
scanf("%d", &T);
while (T--){
init();
solve();
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/mashiroG/p/4662870.html