HDU 6917 Shorten the array(构造)

题目地址

原题

Description

Alice has an array a. The array has N positive numbers. She thinks the array is too long, so she wants to shorten the array. She decides to shorten the array via the following operation: every time she will choose an index i (1≤i<n) which ai>0 and ai+1>0. She will delete ai and ai+1 and use (aimodai+1) or (ai+1modai) to replace them.

For example, for array [3,2,4,5], if Alice choose i=2, she can change the array to [3,2,5] or [3,0,5]. Alice wants you to tell her the shortest possible length of the array after several options.

INPUT

The first line contains a single integer T (1≤T≤10), indicating the number of test cases.

For each test cases:

The first line contains one integer N (2≤N≤106), indicating the size of the array.

The next line contains N integers ai (ai≤109), representing the array.

OUTPUT

Output T lines.

The i-th line contains a single integer, representing the answer of i-th test case.

SAMPLE INPUT

2
4
1 1 1 1
4
2 3 4 5

SAMPLE OUTPUT

2
1

HINT

For the first sample, Alice first choose i = 1 i=1 i=1 to change the array to [ 0 , 1 , 1 ] [0, 1, 1] [0,1,1], then choose i = 2 i=2 i=2 to change the array to [ 0 , 0 ] [0, 0] [0,0], which is the best result she can reach.

题目大意

本题又是给了一个正数序列,然后可以选择两个相邻数 a a a b b b(要求这两个数要大于零),把这两个数变成 a % b a\%b a%b或者 b % a b\%a b%a,直到不能操作为止,最少可以留下几个数。

解题思路

首先可以发现,全都是一样的数只能进行两两合并,那最少就要 n + 1 2 \frac{n+1}{2} 2n+1个数字。然后发现一段数 [ a , b ] [a, b] [a,b]都可以用 m i n ( [ a , b ] ) min([a,b]) min([a,b])消除,这样就可以用最小数代表一段区间(用最小的数字往两边消,总能消到只剩下自己,因为 a % b = a , a < b a\%b=a,a<b a%b=a,a<b)。根据这个原理把数字消得只剩下最小数,这是其中一种最优解,统计最小数的次数 c c c,计算 c + 1 2 \frac{c+1}{2} 2c+1即可。最后发现 [ 2 , 2 , 2 , 3 ] [2,2,2,3] [2,2,2,3]的结果是1(按照上面的方法是2),所以得到另一个最优解的构造,只要能构造出一个比当前最小值还要小的模数,直接可以消掉所有变得只剩下一个数;比如可以用 [ 2 , 3 ] [2,3] [2,3]构造出1,就可以用1消掉所有数啦。

算法步骤

  1. 得到序列的最小值 p p p以及最小值出现的次数 c c c
  2. 尝试在序列中寻找一个数 k k k,使得 k % p < p & & k % p > 0 k \% p < p\&\&k\%p>0 k%p<p&&k%p>0
  3. 如果步骤二成功,输出1;否则输出 c + 1 2 \frac{c + 1}{2} 2c+1

时空复杂度

寻找最小值扫描一遍数组 O ( n ) O(n) O(n),记录次数 O ( n ) O(n) O(n),寻找数字 k k k O ( n ) O(n) O(n),整体复杂度 O ( n ) O(n) O(n)
空间复杂度 O ( n ) O(n) O(n)

参考代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <malloc.h>
#include <string>
#include <stack>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<ll> VII;
typedef unsigned long long ULL;
#define x first
#define y second
#define $(x) cout << x;
#define prt(x) printf("%d", x);
#define prtn(x) printf("%d\n", x);
#define read(x) scanf("%d", &x);
#define readd(x) scanf("%lf", &x);
#define $sp printf(" ");
#define lowbit(x) (x & -x)
#define reset(x, y) memset(x, 0, sizeof x);
#define rep(a, b, c) for (int c = a; c <= b ; ++ c)
#define mul_T int TTTTT;read(TTTTT);while (TTTTT--)
#define mul_n(x) while(~scanf("%d", &x))
#define mul_xy(x, y) while(~scanf("%d%d", &x, &y))
#define pb push_back
#define all(x) x.begin(), x.end()
#define max(a, b, c) max(max(a,b), c)
const double pi = acos(-1.0);
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
clock_t START, END;
const int CTIME = 1;
const int FOPEN = 0;


void __INPUT() {
#ifndef ONLINE_JUDGE
	if (FOPEN) freopen("in.txt", "r", stdin);
	START = clock();
#endif
}
void __TIME() {
#ifndef ONLINE_JUDGE
	END = clock();
	if (CTIME) printf("\n\ntime : %d(%.2lfs)ms", END - START, (END - START) / 1000.0);
#endif
}
const int N = 1e6 + 10;

int n;
int a[N];
void solve() {
	read(n);
	rep(1, n, i) read(a[i]);
	int c = 0, p = 0;
	p = *min_element(a + 1, a + 1 + n);
	rep(1, n, i) c += a[i] == p;
	// 找到最小值以及出现的次数
	rep(1, n, i) {
		int k = a[i] % p;
		if (k && k < p) {
			c = 1;
			break;
		}
	}
	prtn((c + 1) / 2);
}

int main() {
	__INPUT();
	mul_T solve();
	__TIME();
	return 0;
}

TIPS

思路仅供参考

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值