codeforce B. Two Arrays

题目链接

题目大概是这样说的
在这里插入图片描述
这道题如果使用位运算爆搜的方法一定会超时。实际上,组成unluck数的只有两个数,使用键值对的方式就能够记录当前的数组中是否出现了与这个数可以组成unlucky数的数字。

#include<iostream>
#include<vector>

#include<algorithm>
#include<numeric>
#include<map>
using namespace std;


int main()
{
#ifdef ON
	freopen("sb.txt", "r", stdin);
#endif // !ON
	int casenum;
	cin >> casenum;
	for (int i = 0; i < casenum; i++)
	{
		int n, T;
		cin >> n >> T;
		vector<int>nums;
		map<int, int>p0, p1;
		vector<int>a(n);
		for (int k = 0; k < n; k++)
		{
			int t;
			cin >> t;
			nums.push_back(t);
		}
		for (int i = 0; i < nums.size(); i++)
		{// 
			int c = nums[i];
			if (p0[T - c] > 0)
			{
				if (p0[T - c] > p1[T - c])
				{
					p1[c]++;
					a[i] = 1;
				}
				else
				{
					p0[c]++;
					a[i] = 0;
				}
			}
			else
			{
				p0[c]++;
				a[i] = 0;
			}
		}
		for (int f = 0; f < a.size()-1; f++)
		{

			cout << a[f] << " ";
		}
		cout << a[a.size() - 1] << endl;
		
    }

	return 0;
}

codeforce的大佬还给出了这样一种更加快速的算法。

这种方法体现了贪心的思想,大于T的数字分到一组,小于T的数字分到一组,等于 T / 2 T/2 T/2的数字单独处理。

#include<bits/stdc++.h>
using namespace std;
#define fo(i,n) for(int i=0; i<n; i++)
#define fo1(i,n) for(int i=1; i<=n; i++)
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it!=container.end(); it++)
#define present(container, element) (container.find(element)!=container.end())
#define pb push_back
typedef vector< int > vi;
typedef long long ll;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n, t;
		cin >> n >> t;
		int a[n];
		int prev = 0;
		fo(i, n) {
			cin >> a[i];
			if (t % 2 == 0) {
				if (a[i] < t / 2)
					cout << "0 ";
				else if (a[i] > t / 2)
					cout << "1 ";
				else if (a[i] == t / 2 && prev == 0) {
					cout << "1 ";
					prev = 1;
				}
				else if (a[i] == t / 2 && prev == 1)
				{
					cout << "0 ";
					prev = 0;
				}
			}
			else {
				if (a[i] <= t / 2)
					cout << "0 ";
				else
					cout << "1 ";
			}
		}
		cout << endl;
	}
}
### Codeforces Problem 797B Explanation The problem titled "Restoring the Permutation" requires reconstructing a permutation from its prefix sums modulo \( m \). Given an array of integers representing these prefix sums, one must determine whether it is possible to restore such a permutation. In this context, a **permutation** refers to an ordered set containing each integer exactly once within a specified range. The task involves checking if there exists any valid sequence that matches the provided conditions when performing operations as described in the problem statement[^1]. To solve this issue effectively: - Iterate through all elements while maintaining two variables: `current_sum` which tracks cumulative sum during iteration; and `min_value`, used later for adjustments. ```cpp int n, m; cin >> n >> m; vector<int> s(n); for (auto& x : s) cin >> x; ``` Calculate differences between consecutive terms after adjusting initial values appropriately by subtracting minimum value found so far at every step. This adjustment ensures non-negativity throughout calculations without altering relative order among elements. Check feasibility based on properties derived from constraints given in the question text. Specifically, ensure no duplicate residues appear under modulus operation since they would violate uniqueness required for permutations. Finally, construct answer using adjusted difference list obtained previously along with necessary checks ensuring correctness according to rules outlined above.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值