CF 1497:A-C2

本文分享了Codeforces Round #708 (Div.2)的三道编程题目A、B、C的解题思路和代码实现。A题要求按升序输出数组,重复元素放最后;B题是构造题,找出数组能被m整除的最少分组数;C题分为两种情况,分别处理奇数和偶数n的n-LCM问题。通过分析题目和样例,给出了解题策略和代码实现。

Codeforces Round #708 (Div. 2)

A题
B题
C1
C2

A. Meximization

其实说白了,这道题就是把数组按从小到大输出,重复的放在最后
本人用的set,略显复杂

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		set<int> s1;
		multiset<int> s2;//重复的可能不止一个,所以用multiset
		set<int>::iterator it1,it2;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			if(!s1.count(x)) s1.insert(x);
			else s2.insert(x);
		}
		
		for(it1=s1.begin();it1!=s1.end();it1++) cout<<*it1<<' ';
		
		if(!s2.empty())
		{
			for(it2=s2.begin();it2!=s2.end();it2++) 
			 cout<<*it2<<' ';
		}   
		cout<<endl;
	}
	
	return 0;
 } 

B. M-arrays

一道构造题。
有一个大小为 n n n a [ n ] a[n] a[n]数组,多个数相加能被 m m m整除的话构成一个数组,单独一个也可以单独成为一个数组。问: a [ n ] a[n] a[n]数组最少可以被分成多少个像这样能被m整除的数组? ( 1 ≤ a i ≤ 1 0 9 ) (1≤ai≤10^9) 1ai109
其实我们可以从余数出发。
%m的情况:
1 , 2 , 3...... m − 2 , m − 1 1,2,3......m-2,m-1 1,2,3......m2,m1
那我们就可以1和m-1组成一组,2和m-2组成一组,以此类推。余数为0的归到一组,因为题目要求的是最少组数,这些余数为0不用去带其他人。然后我们就可以发现一些规律。

	for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			mod[x%m]++;
		}

读进来数据时,先把它存在 m o d mod mod数组中
a = m o d [ i ] a=mod[i] a=mod[i],
b = m o d [ m − i ] b=mod[m-i] b=mod[mi]
这样a和b可以凑成一对。
1.如果a和b都不为0,且a和b不是同一个数,那么就是一种。举个例子:比如m为8,i为4,那么m-i也是4,他们只能凑成一种。
2.不然就是 a b s ( m o d [ i ] − m o d [ i − 1 ] ) abs(mod[i]-mod[i-1]) abs(mod[i]mod[i1])(这个我也不知道该怎么解释,反正多走几遍数据就是了)

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m,ans=0;
		cin>>n>>m;
		int mod[100000]={0};
		
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			mod[x%m]++;
		}
		if(mod[0]>0) ans++;
		
		for(int i=1;i<=m/2;i++)
		{
			if(mod[i]&&mod[i]==mod[m-i]) ans++;
			 else ans=ans+abs(mod[i]-mod[m-i]);
		}
		
		cout<<ans<<endl;
	}
	return 0;
}

C1. k-LCM (easy version)

分类讨论的一道题目。
1.n奇数
那么三个数分别是:
n / 2 , n/2, n/2, n / 2 n/2 n/2 1 1 1
2.n为偶数
可以转化为 ( n − 1 ) + 1 (n-1)+1 (n1)+1
n-1是奇数按上面方法分解
最后整合一下就是
n / 2 − 1 , n / 2 − 1 , 2 n/2-1,n/2-1,2 n/21,n/21,2
3.但是还有一些数是例外。
例如8如果按照方法2来分解,就会得到 3 , 3 , 2 3,3,2 3,3,2
12如果按照方法2来分解,就会得到 5 , 5 , 2 5,5,2 5,5,2
这都是不符合要求的。
试了几个,我们发现这些数有这样一个特点:
2 n + 2 ( n 为 奇 数 ) 2n+2(n为奇数) 2n+2(n)
那我们可以进一步写成:
2 ( 2 k + 1 ) + 2 2(2k+1)+2 2(2k+1)+2
化简可得:
4 k + 4 4k+4 4k+4
可见这些数都是4的倍数
其实直接一眼看出来
那我们该怎么办呢?这个时候,样例又给了我们启发:

input: 8 3
output:4 2 2

n / 2 , n / 4 , n / 4 n/2,n/4,n/4 n/2,n/4,n/4多试了几个,就他了!!!

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		if(n%2!=0)
		{
			cout<<n/2<<' '<<n/2<<' '<<1;
			cout<<endl;
		}
		else if(n%4!=0)
		{
			cout<<n/2-1<<' '<<n/2-1<<' '<<2;
			cout<<endl;
		}
		else 
		{
			cout<<n/2<<' '<<n/4<<' '<<n/4;
			cout<<endl;
		}
	}
	return 0;
}

C2. k-LCM (hard version)

数学老师曾经告诉我们:一个题目有多个问,那么只些问之间必然有联系!
C2告诉我不仅有联系,而且可以直接copy
C1我们已经处理了 k = 3 k=3 k=3的情况,那么C2直接套就可以了。 k > 3 k>3 k>3的部分直接输出1,剩下来的 n − ( k − 3 ) = n − k + 3 n-(k-3)=n-k+3 n(k3)=nk+3部分同C1

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		k-=3;
		for(int i=1;i<=k;i++)
		 cout<<"1"<<' ';
		n-=k;
		if(n%2!=0)
		{
			cout<<n/2<<' '<<n/2<<' '<<1;
			cout<<endl;
		}
		else if(n%4!=0)
		{
			cout<<n/2-1<<' '<<n/2-1<<' '<<2;
			cout<<endl;
		}
		else 
		{
			cout<<n/2<<' '<<n/4<<' '<<n/4;
			cout<<endl;
		}
	}
	return 0;
}
请解释该broute表:Bridge table: broute Bridge chain: BROUTING, entries: 22, policy: ACCEPT -p 0x893a -j ieee1905 --1905-data 00000004 --1905-target DROP -p 0x893a -d 00:0A:EB:13:09:69 -j DROP -d 02:0A:EB:13:09:69 -j DROP -d 02:0A:EB:13:09:6A -j DROP -p 0x88cc -d 01:80:C2:00:00:0E -j DROP -d 01:80:C2:00:00:13 -j DROP -j QoSTypeRule0 -j QoSTypeRule1 -j QoSTypeRule3 -j QoSTypeRule4 -j QoSTypeRule5 -j QoSTypeRule6 -j QoSTypeRule7 -j QoSTypeRule8 -j QoSTypeRule9 -j QoSTypeRule10 -j QoSTypeRule11 -j QoSTypeRule12 -j QoSTypeRule13 -j QoSTypeRule14 -j QoSTypeRule15 -j QoSTypeRule2 Bridge chain: QoSTypeRule0, entries: 0, policy: RETURN Bridge chain: QoSVlanRule0, entries: 0, policy: RETURN Bridge chain: QoSTypeRule0Remark, entries: 2, policy: RETURN -j mark --mark-or 0x2 --mark-target CONTINUE -j mark --mark-or 0x10 --mark-target ACCEPT Bridge chain: QoSTypeRule1, entries: 1, policy: RETURN -s C2:CF:39:62:E8:57 -j QoSTypeRule1Remark Bridge chain: QoSVlanRule1, entries: 0, policy: RETURN Bridge chain: QoSTypeRule1Remark, entries: 2, policy: RETURN -j mark --mark-or 0x1002 --mark-target CONTINUE -j mark --mark-or 0x20 --mark-target ACCEPT Bridge chain: QoSTypeRule2, entries: 1, policy: RETURN -j QoSTypeRule2Remark Bridge chain: QoSVlanRule2, entries: 0, policy: RETURN Bridge chain: QoSTypeRule2Remark, entries: 2, policy: RETURN -j mark --mark-or 0x2002 --mark-target CONTINUE -j mark --mark-or 0x30 --mark-target ACCEPT Bridge chain: QoSTypeRule3, entries: 1, policy: RETURN -s E0:D3:62:46:55:6B -j QoSTypeRule3Remark Bridge chain: QoSVlanRule3, entries: 0, policy: RETURN Bridge chain: QoSTypeRule3Remark, entries: 2, policy: RETURN -j mark --mark-or 0x3002 --mark-target CONTINUE -j mark --mark-or 0x40 --mark-target ACCEPT
最新发布
09-23
<think> Hmm, the user is asking about the detailed structure and rules within a specific ebtables broute table configuration. From the output provided, this seems to be a complex network setup likely from a home router or enterprise gateway device. First, I recognize that the BROUTING chain has 22 entries with various packet handling rules - some dropping specific protocols like 0x893a (likely HSML/1905.1) and LLDP frames, while others jump to QoS-related chains. Interesting how it mixes both security drops (blocking certain multicast MACs) and QoS marking in the same table. The QoS chains follow a clear pattern: each "QoSTypeRuleX" chain references a corresponding "Remark" chain that applies packet marks using bitwise OR operations. For instance, QoSTypeRule3 first applies mark 0x3002 then 0x40. This layered marking suggests sophisticated traffic classification - probably for prioritizing different device types or services. Noticing the MAC-specific rules too: QoSTypeRule1 and QoSTypeRule3 activate only for specific source MAC addresses (C2:CF:39:62:E8:57 and E0:D3:62:46:55:6B). That's likely targeting individual devices for special QoS treatment. The empty QoSVlanRule chains indicate VLAN handling might be configured elsewhere. What's clever is how the design separates matching (in QoSTypeRule chains) from marking actions (in Remark chains). This modular approach would make rule maintenance easier. The CONTINUE target after initial marking allows multiple rules to cumulatively modify the packet mark - a smart use of ebtables' capabilities. The user seems to be analyzing network equipment configurations, possibly for troubleshooting or optimization. Their persistence in digging into broute details suggests they're either a network admin or an advanced user. Since they've asked about rule behavior before, they're probably trying to understand how packet flows get processed holistically. </think> 以下是对 `ebtables -t broute -L` 输出结果的完整解析,重点关注表结构和规则含义: ### BROUTE 表结构解析 1. **主链 BROUTING** - **策略**:`policy: ACCEPT`(默认允许通过) - **规则明细**: - `-p 0x893a -j ieee1905 --1905-data 00000004 --1905-target DROP` → 丢弃 IEEE 1905.1 协议(0x893a)且数据为 `00000004` 的包 - `-d 02:0A:EB:13:09:69 -j DROP` → 丢弃目标 MAC 为 `02:0A:EB:13:09:69`(保留组播地址)的包 - `-j QoSTypeRuleX` → 跳转到对应的 QoS 子链(共 16 条,用于分层处理 QoS 标记) 2. **QoS 子链结构**(以 QoSTypeRule3 为例) ```markdown Bridge chain: QoSTypeRule3, entries: 1, policy: RETURN -s E0:D3:62:46:55:6B -j QoSTypeRule3Remark # 仅匹配特定源MAC时跳转 Bridge chain: QoSTypeRule3Remark, entries: 2, policy: RETURN -j mark --mark-or 0x3002 --mark-target CONTINUE # 添加标记位 0x3002 -j mark --mark-or 0x40 --mark-target ACCEPT # 添加标记位 0x40 后通过 ``` - **分层处理逻辑**: 1. 主链通过 `-j` 将包分发到子链 2. 子链通过 MAC 匹配(如 `-s E0:D3:62:46:55:6B`)过滤设备 3. `mark --mark-or` 对包进行位或标记(多级叠加) 4. `CONTINUE` 使包继续匹配后续规则 5. `ACCEPT` 终止当前链处理 3. **关键设计特点** - **安全隔离**:丢弃 HSML/1905.1协议(`0x893a`)及特定组播地址(如 `01:80:C2:00:00:0E`) - **模块化 QoS**: - 通过 16 条独立子链实现分类流量标记 - 标记值设计(如 `0x3002` + `0x40`)支持位掩码组合 - **链式处理**: `RETURN` 策略确保未匹配时返回父链,避免规则冲突 ### 核心功能总结 - **核心作用**:控制桥接流量的路由决策(BROUTING)和 QoS 分级(QoSTypeRuleX) - **标记原理**:`--mark-or` 对包标记进行位或运算(如 `0x3002 | 0x40 = 0x3042`),用于后续路由策略识别 - **设备绑定**:子链通过源 MAC(如 `E0:D3:62:46:55:6B`)将规则绑定到特定设备 > 此配置常见于智能网关设备,实现多设备差异化 QoS 和协议过滤。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值