mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-11-07 07:13:48 +00:00
feat: 添加多个算法题目解决方案
添加P14357.cpp、employ.cpp、replace.cpp、road.cpp和club.cpp的初始实现 refactor: 移除P14361.cpp中未使用的dfs函数和smallsolve函数
This commit is contained in:
parent
bdd74246a7
commit
9c505e630f
@ -18,26 +18,6 @@ struct S{
|
||||
std::vector<S>s;
|
||||
std::bitset<maxn> vis;
|
||||
|
||||
sl void dfs(ll now,ll nans){
|
||||
if(now==n+1){
|
||||
ans=std::max(ans,nans);
|
||||
return;
|
||||
}
|
||||
for(ll i=1;i<=3;i++){
|
||||
if(club[i]>=n/2)continue;
|
||||
club[i]++;
|
||||
dfs(now+1,nans+a[now][i]);
|
||||
club[i]--;
|
||||
}
|
||||
}
|
||||
|
||||
sl bool smallsolve(){
|
||||
if(n>10)return false;
|
||||
dfs(0,0);
|
||||
std::cout<<ans<<"\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
sl void solve(){
|
||||
std::cin>>n;
|
||||
ans=0;
|
||||
@ -47,7 +27,6 @@ sl void solve(){
|
||||
for(ll i=1;i<=n;i++){
|
||||
std::cin>>a[i][1]>>a[i][2]>>a[i][3];
|
||||
}
|
||||
if(smallsolve())return;
|
||||
for(ll i=1;i<=n;i++){
|
||||
ll tmp[4];
|
||||
for(ll j=1;j<=3;j++)tmp[j]=a[i][j];
|
||||
|
||||
3
src/11/6/P14357.cpp
Normal file
3
src/11/6/P14357.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
int main(){
|
||||
|
||||
}
|
||||
98
src/11/6/club.cpp
Normal file
98
src/11/6/club.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
const ll maxn = 1e5+7;
|
||||
ll n,a[maxn][4],numclub[3+1],smallans=0;
|
||||
bool viss[maxn];
|
||||
|
||||
#define printf
|
||||
|
||||
static inline void dfs(ll nn,ll nans){
|
||||
if(nn>n){
|
||||
smallans=max(smallans,nans);
|
||||
return;
|
||||
}
|
||||
|
||||
for(ll i=1;i<=3;i++){
|
||||
if(numclub[i]>=n/2)continue;
|
||||
numclub[i]++;
|
||||
dfs(nn+1,nans+a[nn][i]);
|
||||
numclub[i]--;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void solvesmall(){
|
||||
smallans=0;
|
||||
numclub[0]=numclub[1]=numclub[2]=numclub[3]=0;
|
||||
for(ll i=1;i<=n;i++){
|
||||
for(ll j=1;j<=3;j++){
|
||||
cin>>a[i][j];
|
||||
}
|
||||
}
|
||||
dfs(1,0);
|
||||
cout<<smallans<<"\n";
|
||||
}
|
||||
|
||||
static inline void solve(){
|
||||
ll ans=0;
|
||||
cin>>n;
|
||||
if(n<=11){
|
||||
solvesmall();
|
||||
return;
|
||||
}
|
||||
numclub[0]=numclub[1]=numclub[2]=0;
|
||||
for(ll i=0;i<=n;i++)viss[i]=false;
|
||||
struct S{
|
||||
ll diff,size,club,i;
|
||||
inline bool operator<(const S&other)const{
|
||||
if(diff==other.diff)return size>other.size;
|
||||
return diff>other.diff;
|
||||
}
|
||||
};
|
||||
vector<S> s;
|
||||
s.reserve(n+1);
|
||||
for(ll i=1;i<=n;i++){
|
||||
struct Tmp{
|
||||
ll size,j;
|
||||
inline bool operator<(const Tmp&other)const{
|
||||
return size>other.size;
|
||||
}
|
||||
}tmp[4];
|
||||
for(ll j=1;j<=3;j++){
|
||||
ll b;
|
||||
cin>>b;
|
||||
tmp[j]={b,j};
|
||||
}
|
||||
sort(tmp+1,tmp+1+3);
|
||||
for(ll j=1;j<=3;j++){
|
||||
s.push_back({tmp[j].size-tmp[2].size,tmp[j].size,tmp[j].j,i});
|
||||
// printf("S{diff=%lld, size=%lld, club=%lld, i=%lld}\n",s.back().diff,s.back().size,s.back().club,s.back().i);
|
||||
}
|
||||
}
|
||||
sort(s.begin(),s.end());
|
||||
for(ll i=0;i<s.size();i++){
|
||||
printf("S{diff=%lld, size=%lld, club=%lld, i=%lld}\n",s[i].diff,s[i].size,s[i].club,s[i].i);
|
||||
}
|
||||
for(ll i=0;i<s.size();i++){
|
||||
if(viss[s[i].i])continue;
|
||||
if(numclub[s[i].club]<n/2){
|
||||
numclub[s[i].club]++;
|
||||
viss[s[i].i]=true;
|
||||
ans+=s[i].size;
|
||||
}
|
||||
}
|
||||
cout<<ans<<"\n";
|
||||
printf("------\n");
|
||||
}
|
||||
|
||||
int main(){
|
||||
freopen("club.in","r",stdin);
|
||||
freopen("club.out","w",stdout);
|
||||
iostream::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
ll t;
|
||||
cin>>t;
|
||||
while(t--)solve();
|
||||
}
|
||||
34
src/11/6/employ.cpp
Normal file
34
src/11/6/employ.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
const ll maxn = 500+7,p=998'244'353;
|
||||
ll n,m,a[maxn],ans=0;
|
||||
string s;
|
||||
|
||||
int main(){
|
||||
// freopen("employ.in","r",stdin);
|
||||
// freopen("employ.out","w",stdout);
|
||||
iostream::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
cin>>n>>m;
|
||||
cin>>s;
|
||||
s=" "+s;
|
||||
for(ll i=1;i<=n;i++){
|
||||
cin>>a[i];
|
||||
}
|
||||
sort(a+1,a+1+n);
|
||||
do{
|
||||
ll no=0;
|
||||
for(ll i=1;i<=n;i++){
|
||||
if(no>=a[i]){
|
||||
continue;
|
||||
}
|
||||
if(s[i]=='0'){
|
||||
no++;
|
||||
}
|
||||
}
|
||||
if(n-no>=m)ans=(ans+1)%p;
|
||||
}while(next_permutation(a+1,a+1+n));
|
||||
cout<<ans<<"\n";
|
||||
}
|
||||
45
src/11/6/replace.cpp
Normal file
45
src/11/6/replace.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
const ll maxn = 2e5+7;
|
||||
ll n,q;
|
||||
|
||||
string s[maxn][2],r[maxn][2];
|
||||
static inline bool issame(int idx,const string&a,const string&b){
|
||||
if(idx+b.size()>a.size()){
|
||||
return false;
|
||||
}
|
||||
for(int i=0;i<b.size();i++){
|
||||
if(a[i+idx]!=b[i])return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(){
|
||||
freopen("replace.in","r",stdin);
|
||||
freopen("replace.out","w",stdout);
|
||||
iostream::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin>>n>>q;
|
||||
for(int i=1;i<=n;i++){
|
||||
cin>>s[i][0]>>s[i][1];
|
||||
}
|
||||
for(ll i=1;i<=q;i++){
|
||||
cin>>r[i][0]>>r[i][1];
|
||||
ll ans=0;
|
||||
for(ll j=1;j<=n;j++){
|
||||
for(ll k=0;k<r[i][0].size();k++){
|
||||
if(issame(k,r[i][0],s[j][0])){
|
||||
auto tmp = r[i][0];
|
||||
for(int l=0;l<s[j][0].size();l++){
|
||||
tmp[l+k]=s[j][1][l];
|
||||
}
|
||||
if(tmp==r[i][1])ans++;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout<<ans<<'\n';
|
||||
}
|
||||
}
|
||||
95
src/11/6/road.cpp
Normal file
95
src/11/6/road.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
#define sl static inline
|
||||
const int maxm = 1e6+7,maxn=1e4+7;
|
||||
int n,m,k;
|
||||
struct R{
|
||||
int u,v,w;
|
||||
inline bool operator<(const R&o)const{
|
||||
return w<o.w;
|
||||
}
|
||||
}r[maxm];
|
||||
struct C{
|
||||
int w;
|
||||
int cw[maxn];
|
||||
}c[17];
|
||||
|
||||
int bcj[maxm];
|
||||
|
||||
sl ll find(ll n){
|
||||
if(bcj[n]==n)return n;
|
||||
return bcj[n]=find(bcj[n]);
|
||||
}
|
||||
|
||||
sl bool same(ll a,ll b){
|
||||
return find(a)==find(b);
|
||||
}
|
||||
|
||||
sl void merge(ll a,ll b){
|
||||
bcj[find(a)]=find(b);
|
||||
}
|
||||
|
||||
static inline bool checkA(){
|
||||
if(k==0)return false;
|
||||
for(int i=1;i<=k;i++){
|
||||
if(c[i].w!=0)return false;
|
||||
}
|
||||
vector<R> vr(r+1,r+1+m);
|
||||
for(int i=1;i<=k;i++){
|
||||
for(ll j=1;j<n;j++){
|
||||
for(ll k=j+1;k<=n;k++){
|
||||
vr.push_back({j,k,c[i].cw[j]+c[i].cw[k]});
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=1;i<=n;i++)bcj[i]=i;
|
||||
sort(vr.begin(),vr.end());
|
||||
ll ans=0;
|
||||
for(int i=0;i<vr.size();i++){
|
||||
if(same(vr[i].u,vr[i].v))continue;
|
||||
ans+=vr[i].w;
|
||||
merge(vr[i].u,vr[i].v);
|
||||
}
|
||||
cout<<ans<<"\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static inline bool checkB(){
|
||||
if(k!=0)return false;
|
||||
for(int i=1;i<=n;i++)bcj[i]=i;
|
||||
sort(r+1,r+1+m);
|
||||
ll ans=0;
|
||||
for(int i=1;i<=m;i++){
|
||||
if(same(r[i].u,r[i].v))continue;
|
||||
ans+=r[i].w;
|
||||
merge(r[i].u,r[i].v);
|
||||
}
|
||||
cout<<ans<<"\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(){
|
||||
freopen("road.in","r",stdin);
|
||||
freopen("road.out","w",stdout);
|
||||
iostream::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin>>n>>m>>k;
|
||||
for(int i=1;i<=m;i++){
|
||||
cin>>r[i].u>>r[i].v>>r[i].w;
|
||||
}
|
||||
for(int i=1;i<=k;i++){
|
||||
cin>>c[i].w;
|
||||
for(int j=1;j<=n;j++){
|
||||
cin>>c[i].cw[j];
|
||||
}
|
||||
}
|
||||
if(checkA()){
|
||||
return 0;
|
||||
}
|
||||
if(checkB()){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user