SDUT《 算法分析与设计》 实验一-分治算法

74 篇文章 12 订阅

A - 众数问题

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e6 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

int tot[N];

inline void solve(){
    int n;
    cin>>n;
    int k;
    int maxn=-1;
    for(int i=1;i<=n;i++) cin>>k,tot[k]++,maxn=max(maxn,k);
    int res=-1,num=0;
    for(int i=1;i<=maxn;i++){
        if(tot[i]>res){
            res=tot[i];
            num=i;
        }
    }
    cout<<num<<endl<<res<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}

B - 整数因子分解问题

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

ll tot=0;
ll fac[N],f[N];

void div(ll n){
    tot=0;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0){
            fac[tot++]=i;
            if(n/i!=i) fac[tot++]=n/i;
        }
    }
    fac[tot++]=n;
}

inline void solve(){
    ll n;
    cin>>n;
    div(n);
    sort(fac,fac+tot);
    for(int i=0;i<tot;i++){
        f[i]=1;
        for(int j=i-1;j>=0;j--){
            if(fac[i]%fac[j]==0) f[i]+=f[j];
        }
    }
    cout<<f[tot-1]<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}

法二:

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 1e7 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

map<ll,ll>a;

ll DFS(ll x){
    if(x<N&&a[x]) return a[x];
    ll res=1;
    for(int i=2;i<=sqrt(x);i++){
        if(x%i==0){
            if(x/i==i) res+=DFS(i);
            else{
                res+=DFS(x/i);
                res+=DFS(i);
            }
        }
    }
    if(x<N) a[x]=res;
    return res;
}

inline void solve(){
    ll n;
    cin>>n;
    cout<<DFS(n)<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // freopen("textin.txt","r",stdin);
    // freopen("textout.txt","w",stdout);
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}

C - 顺序表应用7:最大子段和之分治递归法

分治算法:

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

int n;
int a[N];
int maxn=0;
int tot=0;

int get(int l,int r){
    tot++;
    if(l==r){
        if(a[l]<0) return 0;
        return a[l];
    }
    int maxn=0;
    int mid=l+r-1>>1;
    int sum=0;
    int maxl=get(l,mid);
    int maxr=get(mid+1,r);
    int ml=0,mr=0;
    for(int i=mid+1;i<=r;i++){
        sum+=a[i];
        mr=max(mr,sum);
    }
    sum=0;
    for(int i=mid;i>=l;i--){
        sum+=a[i];
        ml=max(ml,sum);
    }
    maxn=max(max(maxl,maxr),ml+mr);
    if(maxn<0) maxn=0;
    return maxn;
}

inline void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    cout<<get(1,n)<<" "<<tot<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    while(t--) solve();
    return 0;
}

dp算法:

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

ll n;
ll a[N];
ll maxn=-1;


inline void solve(){
    cin>>n;
    for(ll i=1;i<=n;i++) cin>>a[i];
    ll sum=0;
    ll res=-1;
    for(ll i=1;i<=n;i++){
        sum+=a[i];
        if(sum<0) sum=0;
        res=max(res,sum);
    }
    cout<<res<<" "<<2*n-1<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}

D - 骨牌铺方格

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

ll a[55];

void get(int n){
    a[1]=1;
    a[2]=2;
    for(int i=3;i<=n;i++) a[i]=a[i-1]+a[i-2];
}

inline void solve(){
    int n;
    cin>>n;
    get(n);
    cout<<a[n]<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值