上海理工大学第二届“联想杯”全国程序设计邀请赛

A-A-SOUL!

一开始没想明白,但实际上暴力不可行的话,就需要一边遍历一边求了

每遍历一个数 ,求以当前数结尾的等差为k的等差数列长度

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=1e5+10;
int a[N];
bool flag[N];
int n,k;
void solve() {
	cin>>n>>k;
	map<int,int>mp;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		mp[a[i]]=mp[a[i]-k]+1;
		ans=max(ans,mp[a[i]]);
	}
	cout<<ans<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
//    cin>>t;
	while(t--) {
		solve();
	}
	return 0;
}

C-Counting Cats!

说实话有点坑,题目意思不明,必须是整个字符串作为一个完整的碎片才行

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n;
void solve() {
	cin>>n;
	map<string,int>mp;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		mp[s]++;
	}
	int ans=mp["cat"];
	int t=min(mp["ca"],mp["t"]);
	ans+=t,mp["ca"]-=t,mp["t"]-=t;
	t=min(mp["c"],mp["at"]);
	ans+=t,mp["c"]-=t,mp["at"]-=t;
	t=min({mp["c"],mp["a"],mp["t"]});
	ans+=t;
	cout<<ans<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
//    cin>>t;
	while(t--) {
		solve();
	}
	return 0;
}

K-Kazusa’s Party

数据超级小,直接暴力,dfs,枚举所有的匹配情况。类似于全排列

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 10;
int a[N], b[N];
int ne[N];
bool st[N];
int n;
int ans;
int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
void dfs(int u) {
	if (u == n + 1) {
		int cnt = 0;
		for (int i = 1; i <= n; i++){
			if(gcd(a[i],b[ne[i]])>1) cnt++;
		}
		ans = max(ans, cnt);
		return;
	}
	for (int i = 1; i <= n; i++) { //枚举a[u]匹配b[i]
		if (st[i]) continue;
		ne[u] = i;
		st[i] = true;
		dfs(u + 1);
		st[i] = false;
	}
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) cin >> b[i];
	ans = 0;
	dfs(1);
	cout << ans << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
//    cin>>t;
	while (t--) {
		solve();
	}
	return 0;
}

说到全排列,我们也可以让一个数组不动,让另一个数组进行全排列,进行两两匹配

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 10;
int a[N], b[N];
int n;
int ans;
int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) cin >> b[i];
	ans = 0;
	do{
		int cnt=0;
		for(int i=1;i<=n;i++){
			if(gcd(a[i],b[i])>1) cnt++;
		}
		ans=max(ans,cnt);
	}while(next_permutation(b+1,b+1+n));
	cout<<ans<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
//    cin>>t;
	while (t--) {
		solve();
	}
	return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值