【codeforces #275(div1)】AB题解

A. Diverse Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.

Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.

Input

The single line of the input contains two space-separated positive integers nk (1 ≤ k < n ≤ 105).

Output

Print n integers forming the permutation. If there are multiple answers, print any of them.

Sample test(s)
input
3 2
output
1 3 2
input
3 1
output
1 2 3
input
5 2
output
1 3 2 4 5
Note

By |x| we denote the absolute value of number x.


构造题。


我们首先让序列有k-1个不同的,然后剩下全是1即可。


如何有k-1个不同的?


一头一尾即可~


比如n=10  那么1,10,2,9,3,8.....

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
	int n,k;
        scanf("%d%d",&n,&k);
	int la=1,l=1,r=n+1,now=n-1;
	cout<<l;
	for (int i=1;i<k;i++)
	{
		if ((i&1)==0)
		{
			l=r-now;
			now--;
			la=l;
			cout<<" "<<l;
		}
		else 
		{
			r=l+now;
			la=r;
			cout<<" "<<r;
			now--;
		}
	}
	if (la==l)
	{
		for (int i=la+1;i<r;i++)
			cout<<" "<<i;
	}
	else
	{
		for (int i=la-1;i>l;i--)
			cout<<" "<<i;
	}
	cout<<endl;
	return 0;
}

B. Interesting Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers liriqi (1 ≤ li ≤ ri ≤ n) meaning that value  should be equal to qi.

Your task is to find any interesting array of n elements or state that such array doesn't exist.

Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".

Input

The first line contains two integers nm (1 ≤ n ≤ 1051 ≤ m ≤ 105) — the number of elements in the array and the number of limits.

Each of the next m lines contains three integers liriqi (1 ≤ li ≤ ri ≤ n0 ≤ qi < 230) describing the i-th limit.

Output

If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integersa[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.

If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.

Sample test(s)
input
3 1
1 3 3
output
YES
3 3 3
input
3 2
1 3 3
1 3 2
output
NO


每一位分别做。


这道题一定要考虑清楚再写!!


枚举每一位,如果一个区间的这一位为1,那么这一段必须全是1;如果这一位是0,那么这个区间至少有一个0。


我们先把必须是1的赋值为1,用前缀和判断是否有0即可。


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define M 100000+5
using namespace std;
struct data
{
	int l,r,q;
}a[M];
int n,c[M][35],m,sum[M],ans[M][36];
bool cmp(data a,data b)
{
	if (a.l==b.l) return a.r<b.r;
	return a.l<b.l;
}
void Prepare()
{
	memset(c,0,sizeof(c));
	for (int i=1;i<=m;i++)
	{
		int x=a[i].q;
		while (x)
		{
			c[i][0]++;
			c[i][c[i][0]]=x&1;
			x>>=1;
		}
	}
}
void Print()
{
	for (int i=1;i<=n;i++)
	{
		if (!ans[i][0]) printf("0 ");
		else
		{
			int x=0,b=1;
			for (int j=1;j<=ans[i][0];j++)
			{
				x=x+b*ans[i][j];
				b*=2;
			}
			printf("%d ",x);
		}
	}
	cout<<endl;
}
int main()
{
        scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].q);
	}
	sort(a+1,a+1+m,cmp);
	Prepare();
	for (int i=1;i<=n;i++)
		ans[i][0]=0;
	a[0].r=0;
	int f=1,now=0;
	for (int i=1;i<=30;i++)
	{
		for (int j=0;j<=n;j++)
			sum[j]=0;
		for (int j=1;j<=m;j++)
		{
			if (c[j][i])
			{
				for (int k=max(a[j].l,now);k<=a[j].r;k++)
					sum[k]=1,ans[k][i]=1,ans[k][0]=i;
				now=max(a[j].r+1,now);
			}
		}
		for (int j=2;j<=n;j++)
			sum[j]+=sum[j-1];
		now=0;
		for (int j=1;j<=m;j++)
		{
			if (!c[j][i])
			{
				if (sum[a[j].r]-sum[a[j].l-1]==a[j].r-a[j].l+1)
					f=0;
			}
		}
		if (!f) break;
	}
	if (f) puts("YES"),Print();
	else puts("NO");
	return 0;
}


### 关于 Codeforces Round 839 Div 3 的题目与解答 #### 题目概述 Codeforces Round 839 Div 3 是一场面向不同编程水平参赛者的竞赛活动。这类比赛通常包含多个难度层次分明的问题,旨在测试选手的基础算法知识以及解决问题的能力。 对于特定的比赛问题及其解决方案,虽然没有直接提及 Codeforces Round 839 Div 3 的具体细节[^1],但是可以根据以往类似的赛事结构来推测该轮次可能涉及的内容类型: - **输入处理**:给定一组参数作为输入条件,这些参数定义了待解决的任务范围。 - **逻辑实现**:基于输入构建满足一定约束条件的结果集。 - **输出格式化**:按照指定的方式呈现最终答案。 考虑到提供的参考资料中提到的其他几场赛事的信息[^2][^3],可以推断出 Codeforces 圆桌会议的一般模式是围绕着组合数学、图论、动态规划等领域展开挑战性的编程任务。 #### 示例解析 以一个假设的例子说明如何应对此类竞赛中的一个问题。假设有如下描述的一个简单排列生成问题: > 对于每一个测试案例,输出一个符合条件的排列——即一系列数字组成的集合。如果有多种可行方案,则任选其一给出即可。 针对上述要求的一种潜在解法可能是通过随机打乱顺序的方式来获得不同的合法排列形式之一。下面是一个 Python 实现示例: ```python import random def generate_permutation(n, m, k): # 创建初始序列 sequence = list(range(1, n + 1)) # 执行洗牌操作得到新的排列 random.shuffle(sequence) return " ".join(map(str, sequence[:k])) # 测试函数调用 print(generate_permutation(5, 2, 5)) # 输出类似于 "4 1 5 2 3" ``` 此代码片段展示了怎样创建并返回一个长度为 `k` 的随机整数列表,其中元素取自 `[1..n]` 这个区间内,并且保证所有成员都是唯一的。需要注意的是,在实际比赛中应当仔细阅读官方文档所提供的精确规格说明,因为这里仅提供了一个简化版的方法用于解释概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值