yp蓝桥杯刷题521~604(更新中)

day1

92递归实现指数型枚举

#include <bits/stdc++.h>
using namespace std;
int n;
void printfdd(int m)
{
    int now = 1;
    while (m)
    {
        int a = m & 1;
        m >>= 1;
        if (a)
        {
            cout << now << " ";
        }
        now++;
    }
    cout << endl;
}
int main()
{
    cin >> n;
    for (int i = 0; i < 1 << n; i++)
    {
        printfdd(i);
    }
    //system("pause");
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n;
void dfs(int u, int state)
{
    if (u == n)
    {
        for (int i = 0; i < n; i++)
        {
            if (state >> i & 1)
            {
                cout << i + 1 << " ";
            }
        }
        cout << endl;
        return;
    }
    dfs(u + 1, state);          //啥也没干就相当于或0
    dfs(u + 1, state | 1 << u); //有一个虚拟位置
}
int main()
{
    cin >> n;
    dfs(0, 0);
    system("pause");
    return 0;
}

94递归实现排列型枚举

#include <bits/stdc++.h>
using namespace std;
int n;
int arr[10];
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        arr[i] = i;
    }
    do
    {
        for (int i = 1; i <= n; i++)
        {
            cout << arr[i] << " ";
        }
        puts("");
    } while (next_permutation(arr + 1, arr + 1 + n));
    //system("pause");
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> path;
void dfs(int u, int state)
{
    if (u == n)
    {
        for (auto x : path)
            cout << x << " ";
        cout << endl;
        return;
    }
    for (int i = 0; i < n; i++)
    {
        if (!(state >> i & 1))
        {
            path.push_back(i + 1);
            dfs(u + 1, state | 1 << i);
            path.pop_back();
        }
    }
}
int main()
{
    cin >> n;
    dfs(0, 0);
    system("pause");
    return 0;
}

二分模板

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int arr[10] = {0, 1, 2, 3, 3, 3, 3, 3, 4, 5};
    int x = 3;
    int l = 0, r = 10;
    while (r > l)
    {
        int mid = l + r >> 1;
        if (arr[mid] >= x)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    cout << l << endl;
    l = 0, r = 10;
    while (r > l)
    {
        int mid = l + r + 1 >> 1;
        if (arr[mid] <= x)
        {
            l = mid;
        }
        else
        {
            r = mid - 1;
        }
    }
    cout << l << endl;
    system("pause");
    return 0;
}

789acwing二分

#include <bits/stdc++.h>
using namespace std;

vector<int> arr;
int n, m;
int main()
{
    cin >> n;
    cin >> m;
    for (int i = 0; i < n; i++)
    {
        int dd;
        cin >> dd;
        arr.push_back(dd);
    }
    while (m--)
    {
        int now;
        cin >> now;
        int l = 0, r = n;
        while (r > l)
        {
            int mid = l + r >> 1;
            if (arr[mid] >= now)
            {
                r = mid;
            }
            else
            {
                l = mid + 1;
            }
        }
        if (arr[l] == now)
        {
            cout << l << " ";
        }
        else
        {
            cout << "-1"
                 << " -1" << endl;
            continue;
        }
        l = 0;
        r = n;
        while (r > l)
        {
            int mid = l + r + 1 >> 1;
            if (arr[mid] <= now)
            {
                l = mid;
            }
            else
            {
                r = mid - 1;
            }
        }
        if (l == n)
        {
            cout << l - 1 << endl;
        }
        else
        {
            cout << l << endl;
        }
    }
    system("pause");
    return 0;
}

790精度二分

#include <bits/stdc++.h>
using namespace std;
double n;
int main()
{
    cin >> n;
    double ans;
    double l = -10000, r = 10000;
    double mid;
    while ((r - l) > 1e-8)
    {
        mid = (r + l) / 2;
        if (mid * mid * mid >= n)
        {
            r = mid;
        }
        else
        {
            l = mid;
        }
    }
    cout << fixed << setprecision(6) << mid << endl;
    system("pause");
    return 0;
}

730 机器人能量2e-h

#include <bits/stdc++.h>
using namespace std;

int n;
int arr[100005];

bool check(int m)
{
    for (int i = 0; i < n; i++)
    {
        m = m * 2 - arr[i];
        if (m < 0)
        {
            return false;
        }
        if (m > 1e6)
            return true;
    }
    return true;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int l = 0, r = 100000;
    while (r > l)
    {
        int mid = r + l >> 1;
        if (check(mid))
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    cout << l << endl;
    system("pause");
    return 0;
}

day2

1205买不到的糖果

#include<bits/stdc++.h>
using namespace std;

int main(){
    int m,n;
    cin>>m>>n;
    cout<<m*n-m-n<<endl;
    return 0;
}

795前缀和

#include <bits/stdc++.h>
using namespace std;
int m, n, l, r;
int arr[100005];
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int now;
        cin >> now;
        arr[i] = arr[i - 1] + now;
    }
    while (m--)
    {
        cin >> l >> r;
        cout << arr[r] - arr[l - 1] << endl;
    }
    system("pause");
    return 0;
}

子矩阵和

#include <bits/stdc++.h>
using namespace std;
int m = 0, n = 0, q = 0, now = 0;
int a, b, c, d;
int arr[1005][1005];
int main()
{
    memset(arr, 0, sizeof(arr));
    cin >> n >> m >> q;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> now;
            arr[i][j] = arr[i - 1][j] + arr[i][j - 1] + now - arr[i - 1][j - 1];
        }
    }
    
    while (q--)
    {
        cin >> a >> b >> c >> d;
        cout << (arr[c][d] - arr[a - 1][d] - arr[c][b - 1] + arr[a-1][b-1]) << endl;
    }
    system("pause");
    return 0;
}

分巧克力

#include <bits/stdc++.h>
using namespace std;
int n, k;
int arr[100005][2];
bool check(int mid)
{
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        ans += (arr[i][0] / mid) * (arr[i][1] / mid);
    }
    return ans >= k;
}
int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> arr[i][0] >> arr[i][1];
    }
    int l = 1, r = 100000;
    int mid;
    while (r > l)
    {
        mid = l + r + 1 >> 1;
        if (check(mid))
        {
            l = mid;
        }
        else
        {
            r = mid - 1;
        }
    }
    cout << l << endl;
    system("pause");
    return 0;
}

1230 k倍区间

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100005;
int n, k, now;
ll ans;
ll arr[N];
int cnt[N];

int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin>>now;
        arr[i] = (arr[i - 1] + now)%k;
        ans += cnt[arr[i]];//找起始位置
        cnt[arr[i]]++;
    }
    ans += cnt[0];//加上从最初开始的匹配空间
    cout << ans << endl;
    system("pause");
    return 0;
}

1236递增三元组

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
typedef long long ll;
int a[N], b[N], c[N];
ll ans=0;
int n;

int main()
{
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i)
    {
        cin >> b[i];
    }
    for (int i = 0; i < n; ++i)
    {
        cin >> c[i];
    }
    sort(a, a + n);
    sort(b, b + n);
    sort(c, c + n);

    for (int i = 0; i < n; i++)
    {
        int x = (lower_bound(a, a + n, b[i]) - a);
        int y = n - (upper_bound(c, c + n, b[i]) - c);
        ans += (ll)x * y;//注意longlong溢出
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

1015摘花生

#include <bits/stdc++.h>
using namespace std;
int T;
int r, c;
int arr[105][105];
int main()
{
    cin >> T;
    while (T--)
    {
        cin >> r >> c;
        for (int i = 1; i <= r; i++)
        {
            for (int j = 1; j <= c; j++)
            {
                cin >> arr[i][j];
                arr[i][j] += (max(arr[i - 1][j], arr[i][j - 1]));
            }
        }
        cout << arr[r][c] << endl;
    }
    system("pause");
    return 0;
}

895lis

O(n2)

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
#define INF 0x7f7f7f7f
int len[N];
int arr[N];
int n;
int ans = -1;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> arr[i];
        len[i] = 1;
    }
    for (int i = 1; i <= n;i++){
        for (int j = 1; j < i;j++){
            if(arr[j]<arr[i])
                len[i] = max(len[i], len[j] + 1);
        }
        ans=max(ans, len[i]);
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

O(nlogn)

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
#define INF 0x7f7f7f7f

int arr[N];
int tmp[N];
int len = -1;

int n;
int ans = -1;
int main()
{
    cin >> n;
    fill(tmp, tmp + n, INF);
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
        int j = lower_bound(tmp, tmp + n, arr[i]) - tmp;//从0开始的位置
        len = j + 1;//所以加1
        ans = max(ans, len);//判断有没有追加
        tmp[j] = arr[i];//如果追加,更新末尾,如果没有则更新当前距离的最小尾值
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

2 01背包

#include <bits/stdc++.h>
using namespace std;
int N, V;
int v, w;
int arr[1005];
int main()
{
    cin >> N >> V;
    while (N--)
    {
        cin >> v >> w;
        for (int i = V; i - v >= 0; i--)
        {
            arr[i] = max(arr[i], arr[i - v] + w);
        }
    }
    cout << arr[V] << endl;
    system("pause");
    return 0;
}

day3

1211 蚂蚁感冒

#include <bits/stdc++.h>
using namespace std;

int n;
int arr[55];
int ans;

int main()
{
    int left = 0;
    int right = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    for (int i = 1; i < n; i++)
    {
        if (abs(arr[i]) > abs(arr[0]) && arr[i] < 0)
        {
            left++;
        }

        if (abs(arr[i]) < abs(arr[0]) && arr[i] > 0)
        {
            right++;
        }
    }
    if (left == 0 && arr[0] > 0 || right == 0 && arr[0] < 0) cout<<1<<endl;
    else{
        cout << right + left + 1 << endl;
    }
    system("pause");
    return 0;
}

1216 换饮料

#include <bits/stdc++.h>
using namespace std;

int n;
int main()
{
    cin >> n;
    int ans = n;
    while (n >= 3)
    {
        int x = n / 3;
        ans += x;
        int y = n % 3;
        n = x + y;
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

1210连号区间数

#include <bits/stdc++.h>
using namespace std;

int n;
int arr[10005];
int main()
{
    cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> arr[i];
    }
    for (int l = 1; l <= n; l++)
    {
        int max1 = INT_MIN;
        int min1 = INT_MAX;
        for (int r = l; r <= n; r++)
        {
            max1 = max(max1, arr[r]);
            min1 = min(min1, arr[r]);
            if((max1-min1)==(r-l))
                ans++;
        }
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

1229日期问题

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define piii pair<pii, int>
#define mp make_pair
int addyear(int num)
{
    if (num >= 60)
        return num + 1900;
    return 2000 + num;
}
int delyear(int num)
{
    if (num >= 2000)
        return num - 2000;
    return num - 1900;
}

bool operator<(const piii &a, const piii &b)
{
    if (a.first.first > b.first.first)
    {
        return true;
    }
    else if (a.first.first == b.first.first)
    {
        if (a.first.second > b.first.second)
        {
            return true;
        }
        else if (a.first.second == b.first.second)
        {
            return a.second > b.second;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

int arr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isDate(int y, int m, int d)
{
    y = addyear(y);
    if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
    {
        arr[2]++;
    }
    if (m >= 1 && m <= 12)
    {
        if (d <= arr[m] && d > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    return false;
}
string s;
int a, b, c;
set<piii> q;
int main()
{
    cin >> s;
    a = (int)(s[0] - '0') * 10 + (int)(s[1] - '0');
    b = (int)(s[3] - '0') * 10 + (int)(s[4] - '0');
    c = (int)(s[6] - '0') * 10 + (int)(s[7] - '0');
    if (isDate(a, b, c))
    {
        a = addyear(a);
        q.insert(mp(mp(a, b), c));
        a = delyear(a);
    }
    if (isDate(c, a, b))
    {
        c = addyear(c);
        q.insert(mp(mp(c, a), b));
        c = delyear(c);
    }
    if (isDate(c, b, a))
    {
        c = addyear(c);
        q.insert(mp(mp(c, b), a));
        c = delyear(c);
    }
    for (set<piii>::iterator it = q.begin(); it != q.end(); it++)
    {
        piii pp = *it;
        cout << pp.first.first << "-" << setw(2) << setfill('0') << pp.first.second << "-" << setw(2) << setfill('0') << pp.second << endl;
    }
    system("pause");
    return 0;
}

1245特别数的和

#include <bits/stdc++.h>
using namespace std;
#define ll long long
bool check(int n)
{
    while (n)
    {
        int a = n % 10;
        n /= 10;
        if (a == 2 || a == 0 || a == 1 || a == 9)
            return true;
        
    }
    return false;
}
ll ans;
int n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        if (check(i))
            ans += i;
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

1238日志统计

尺取法

#include <bits/stdc++.h>
using namespace std;
#define N 100005
int arr[N][2];
vector<int> t[N];
int ans[N];
int n, d, k;
bool judge(int x){
    int len = t[x].size();
    if(len<k)
        return 0;
    sort(t[x].begin(), t[x].end());
    int l = 0, r = 0, sum = 0;
    while(l<=r&&r<len){
        sum++;
        if(sum>=k){
            if(t[x][r]-t[x][l]<d){
                return 1;
            }else{
                sum--, l++;
            }
        }
        r++;
    }
    return 0;
}
int main()
{
    int x, y;
    cin >> n >> d >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        t[y].push_back(x);
    }
    int cnt = 0;
    for(int i=0;i<N;i++){
        if(judge(i))
            ans[++cnt] = i;
    }
    for (int i = 1; i <= cnt;i++){
        cout << ans[i] << endl;
    }
    system("pause");
    return 0;
}

day4

1215小朋友排队

#include <bits/stdc++.h>
using namespace std;
#define N 100050
int n, m, now;
int tree[N];
int arr[N];
void add(int x, int k)
{
    for (x; x <= N; x += (x & -x))
        tree[x] = max(tree[x], k);
}

int ask(int l, int r)
{
    int res = 0;
    while (r >= l)
    {
        int len = (r & -r);
        if ((r - len + 1) < l)
        { //越界了
            res = max(res, arr[r]);
            r--;
        }
        else
        {
            res = max(res, tree[r]);
            r -= len;
        }
    }
    return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> arr[i];
        add(i, arr[i]);
    }
    int l, r;
    while (m--)
    {
        cin >> l >> r;
        cout << ask(l, r) << endl;
    }
    system("pause");
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.0-0.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值