【比赛】USACO21 Jan

本文详细介绍了USACO2021年1月比赛中的三道题目,涉及字符串的子序列划分、最短路径计算和未完整解答的舞蹈动作问题。通过状态压缩动态规划解决字符串问题,建立分层图求解最短路径,文章分享了比赛心得并提及晋级情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【比赛】USACO21 Jan


Dec的时候进了Gold,于是这次只参加了Gold

P7296 [USACO21JAN] Uddered but not Herd G

题意:
给定字符串s,试构造一种小写字母排列p,使得将s划分为若干组,每组字符串均为p的子序列,且使得组数最少,输出最少组数。

保证s长度小于等于1e5且s出现的不同字符不超过二十个

题解:

因为 ∣ p ∣ |p| p很小,可以考虑状压DP

正难则反,考虑把每个字符划分为一段

如果相邻两个字母在排列中顺序就把他们合并起来

d p [ i ] dp[i] dp[i]表示当前的排列中已经选择了的字母集合

更新时枚举下一个字母的选择

对于在排列中出现过的字母如果在原串中位于当前字母之后

则他们无法合并,产生对答案的贡献

可以先处理出每一对相邻字母数量

记忆化搜索即可

#include<bits/stdc++.h>
using namespace std;
inline int Read(){
   
  	int s = 0 , w = 1;
	char ch = getchar();
	while(ch > '9' || ch < '0'){
   
		if(ch == '-') w = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9'){
   
		s = (s << 1) + (s << 3) + ch - '0';
		ch = getchar();
	}
	return s * w;
}
const int MAXN = 1e5 + 50;
char s[MAXN];
int a[MAXN];
int p[25][25];
int n;
int id[300],tot = 0;
int f[(1 << 21) + 10];
int DP(int ss){
   
	if(ss == (1 << tot) - 1) return 0;
	if(f[ss]) return f[ss];
	int res = 0;
	for(int i = 1 ; i <= tot ; i ++){
   
		if((ss >> (i - 1)) & 1) continue;
		int tmp = 0;
		for(int j = 1 ; j <= tot ; j ++){
   
			if((ss >> (j - 1)) & 1){
   
				tmp += p[j][i];
			}
		}
		res = max(res,tmp + DP((1 << (i - 1)) | ss));
	}
	return (f[ss] = res);
}
int main(){
   
	scanf("%s",s + 1);
	n = strlen(s + 1);
	for(int i = 1 ; i <= n ; i ++){
   
		if(!id[s[i]]) id[s[i]] = ++ tot;
		a[i] = id[s[i]];
	}
	for(int i = 1 ; i + 1 <= n ; i ++){
   
		p[a[i]][a[i + 1]] ++;
	}
	
### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值