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;
}