洛谷100题计划(10/100)

洛谷100题计划(10/100)

P1031 [NOIP2002 提高组] 均分纸牌 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

因为第\(1\)堆只能移动到第\(2\)堆,且第\(N\)堆只能移动到第\(N-1\)堆,所以直接从左边往右边转移就行,这里是都减了一个平均数,看所有堆都差多少,如果左右两个都没过平均数,可以看成先让右边的给点纸牌让左边达到平均数,然后左边欠的让右边去欠,这个过程右边给了左边一次所以要加上

#include<bits/stdc++.h>

using i64 = long long;

using namespace std;

typedef pair<i64, i64> PII;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    vector<i64> a(N);
    i64 sum = 0;
    for(auto &i : a) {
        cin >> i;
        sum += i;
    }

    sum /= N;
    for(auto &i : a)
        i -= sum;

    int ans = 0;
    for(int i = 0;i < N - 1;i ++){
        if(a[i] != 0){
            a[i + 1] += a[i];
            ans ++;
        }
    }

    cout << ans << '\n';

    return 0;
}

P1036 [NOIP2002 普及组] 选数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

以前做过的直接上代码了(

反正\(n\)很小,直接暴搜了,\(m\)就是现在放了几个数,\(sum\)就是现在放了的数的和,\(st\)就是放第几个数(

#include<bits/stdc++.h>
using namespace std;
int a[20], n, k, ans;
int ss(int x)
{
    if (x == 2) return 1;
    for (int i = 2; i <= sqrt(x); i++)
        if (x % i == 0) return 0;
    return 1;
}
void dfs(int m, int sum, int st)
{
    if (m == k)
    {
        if (ss(sum)) ans++; return ;
    }
    for (int i = st; i < n; i++)
        dfs(m + 1, sum + a[i], i + 1);
    return ;
}
int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    dfs(0, 0, 0);
    cout << ans;
    return 0;
}

P1060 [NOIP2006 普及组] 开心的金明 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

典型的\(01\)背包模版题

#include<bits/stdc++.h>

using i64 = long long;

using namespace std;

typedef pair<i64, i64> PII;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<int> v(m), p(m);
    for (int i = 0; i < m; i ++)
        cin >> v[i] >> p[i];

    vector<i64> dp(n + 1, 0);

    for (int i = 0; i < m; i ++) {
        for (int j = n; j >= v[i]; j--) {
            dp[j] = max(dp[j], dp[j - v[i]] + v[i] * p[i]);
        }
    }

    cout << dp[n] << '\n';

    return 0;
}

P1100 高低位交换 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

先把\(n\)的前\(16\)位都取出来然后左移16位就得到了答案的前\(16\)位,然后直接将\(n\)右移\(16\)位就得到后\(16\)位了,最后加起来就行

#include<bits/stdc++.h>

using i64 = long long;

using namespace std;

typedef pair<i64, i64> PII;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n ;

    i64 p = (n & 65535), q = (n >> 16);

    p <<= 16;

    cout << (p + q) << '\n';
    return 0;
}

P1097 [NOIP2007 提高组] 统计数字 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

就是统计每个数字的出现次数以及排序,可以直接用map,map里的数会自动排序

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 10;

int main() {

    int n,m;
    cin >> n;
    map<int,int> mp;
    for(int i = 0 ;i < n;i ++){
        cin >> m;
        mp[m]++;
    }
    for(auto [x,y] : mp)
        cout << x << ' ' << y << endl;

    return 0;
}
posted @ 2023-08-23 17:34  Ke_scholar  阅读(20)  评论(0编辑  收藏  举报