算法分析与设计练习-3

7-1 小H分糖果

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n,a[N],cnt[N];
int main()
{
    cin >> n;
    for(int i = 1;i <= n;i ++)
    {
        cin >> a[i];
        cnt[i] = 1;
    }
    ll ans = 0;
    a[0] = a[1];
    for(int i = 1;i <= n; i++)
    {
        if(a[i] > a[i - 1]) cnt[i] = cnt[i-1] + 1;
    }
    for(int i = n-1;i > 0;i --)
    {
        if(a[i] > a[i+1]) cnt[i] = max(cnt[i], cnt[i+1] + 1);
    }
    for(int i=1;i<=n;i++) ans += cnt[i];
    cout << ans;
}

7-2 子集和问题

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N];
int n,w;
vector<int>v;
void DFS(int u,int k)
{
    if(u == w) 
    {
        for(int it : v)
            cout << it << ' ';
        cout << endl;
        return;
    }
    for(int i=0;i<n;i++)
    {
        if(u + a[i] <= w && i > k)
        {
            v.push_back(a[i]);
            u += a[i];
            DFS(u,i);
            v.pop_back();
            u -= a[i];
        }
    }
}
int main()
{
    cin >> n >> w;
    for(int i=0;i<n;i++) cin >> a[i];
    DFS(0,-1);
}

7-3 数列游戏

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define INF 0x3f3f3f3f
const int N = 1e4 + 10;
int n;
int dp[N], p[N];
vector<int>a;
void DP()
{
    int pos = 0, mx = -1;
    for (int i = 0; i < n; i++)
    {
        dp[i] = 1;
        p[i] = i;
        for (int j = 0; j < i; j++)
        {
            if (a[j] <= a[i])
            {
                if(dp[i] < dp[j] + 1)
                {
                    dp[i] = dp[j] + 1;
                    p[i] = j;
                    if (dp[i] > mx)
                    pos = i;
                    mx = max(mx, dp[i]);
                }
            }
        }
    }
    vector<int>s;
    while(p[pos] != pos)
    {
        s.push_back(a[pos]);
        pos = p[pos];
    }s.push_back(a[pos]);
    cout << s.size() << endl;
    for (int i = s.size() - 1; i >= 0; i --)
    {
        if (i != s.size() - 1) cout << " ";
        cout << s[i];
    }cout << endl;
    multiset<int>st;
    for(int i = 0;i < n;i ++)
    {
        multiset<int>::iterator it = st.begin();
        if(st.empty()|| a[i] < *it) st.insert(a[i]);
        else 
        {
            auto tt = st.upper_bound(a[i]);
            tt --;
            st.erase(tt);
            st.insert(a[i]);
        }
    }
    cout << st.size() <<endl;
}
int main()
{
    cin >> n;
    int x;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        a.push_back(x);
    }
    if(n == 1)
        cout << 1 <<endl << a[0] << endl << 1;
    else
        DP();
}

7-4 叠猫猫

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int fa[N];
int ran[N];
int n, m;
void init()
{
    for (int i = 0; i < N; i++)
    {
        fa[i] = i;
        ran[i] = 1;
    }
}
int find(int x)
{
    if (x != fa[x])
        fa[x] = find(fa[x]);
    return fa[x];
}
void unite(int x, int y)
{
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (ran[x] > ran[y])
        swap(x, y);
    fa[x] = y;
    ran[y] += ran[x];
}
int main()
{
    cin >> n >> m;
    init();
    for (int i = 1; i <= m; i++)
    {
        int x, y;
        cin >> x >> y;
        unite(x, y);
        cout << ran[find(y)] << endl;
    }
}

7-5 范围和

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010;
int num[N];
int n;
int main()
{
    cin >> n;
    for (int i=0;i<n;i++) cin >> num[i];
    ll ans = 0;
    for(int i=0;i<n;i++)
    {
        int mx = INT_MIN,mn = INT_MAX;
        for(int j=i;j<n;j++)
        {
            mx = max(mx,num[j]);
            mn = min(mn,num[j]);
            ans += mx - mn;
        }
    }
    cout << ans;
}

7-6 ACM宣传

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define INF 0x3f3f3f3f
const int N = 1e6 + 10;
int n, m;
struct node
{
    int v,w;
    node(int vv,int ww):v(vv),w(ww){}
};
vector<node>e[2][N];
int dis[2][N];
bool vis[N];
void dijkstra(int t)
{
    memset(vis,0,sizeof(vis));
    priority_queue<P,vector<P>,greater<P> >q;
    for(int i=2;i<=n;i++) dis[t][i] = INF;
    q.push(P(dis[t][1],1));
    while (!q.empty())
    {
        P tp = q.top();
        q.pop();
        int u = tp.second;
        if(vis[u]) continue;
        vis[u] = true;
        for(int i=0;i<e[t][u].size();i++)
        {
            int v = e[t][u][i].v;
            int w = e[t][u][i].w;
            if(dis[t][u] + w < dis[t][v])
            {
                dis[t][v] = dis[t][u] + w;
                q.push(P(dis[t][v],v)); 
            }
        }
    }
    
}
int main()
{
    int T,x,y,z;
    cin >> T;
    while (T --)
    {
        memset(e,0,sizeof(e));
        cin >> n >> m;
        while (m --)
        {
            cin >> x >> y >> z;
            e[0][x].push_back(node(y,z));
            e[1][y].push_back(node(x,z));
        }
        dijkstra(0);
        dijkstra(1);
        ll ans = 0;
        for(int i=2;i<=n;i++)
        ans += dis[0][i] + dis[1][i];
        cout << ans <<endl;
    }
}
posted @ 2022-12-21 15:46  jojoqiao  阅读(164)  评论(0)    收藏  举报