2023/02/17刷题

A. Two Bags of Potatoes

链接

A. Two Bags of Potatoes

这个题就是求出大于等于y的k的倍数,并且再1-n之内的倍数全部将这个倍数减去y的最后的值打印出来

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;
const int N = 100008;


signed main () {
    int x = 0;
    int cnt = 0;
    int y, k, n;
    cin >> y >> k >> n;
    while (1) {
        if (((y + k - 1) / k + cnt)*k > n) {
            //先让y除以k加上cnt然后向上取整
            //当这个数大于n的时候退出,小于等于n的时候进行判断
            break;
        } else {
            if (((y + k - 1) / k + cnt)*k - y != 0) {//因为x>=1所以如果是0的话不打印
                cout << ((y + k - 1) / k + cnt)*k - y << ' ';//打印
                x++;//计数器
            }


        }


        cnt++;//每次更新到下一个倍数
    }
    if (x == 0) {
        cout << -1 << '\n';//如果x==0打印-1


    }
    return 0;
}

B. Little Pony and Sort by Shift

链接

B. Little Pony and Sort by Shift

这个题就是寻找有几个a[i]小于a[i-1]的点,如果是0个打印0,如果大于等于2的话,打印-1,否则的话进行特别判断如果a[1]<a[n]打印-1否则打印res的值

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;
const int N = 100008;


signed main () {
    int n;
    scanf("%lld", &n);
    int a[N];
    vector <int> res;
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);

    }
    //寻找有没有后面的点比前面的点小的情况,如果有的话打印用res记录下来该点之后全部移动需要的次数
    for (int i = 2; i <= n; i++) {
        if (a[i] < a[i - 1]) {
            res.push_back(n - i + 1);
        }
    }
//if(a[1]<a[n]){
//    res.push_back(1);
//}
    if (res.size() == 0) {//如果没有直接打印0
        cout << 0 << ' ';
    } else if (res.size() == 1) {//如果等于1的话
        if (a[1] < a[n]) {//判断一下a[1]和a[n]的大小关系如果a[1]较小是不可能排成有序的
            cout << -1 << ' ';
        } else {
            cout << res[0] << ' ';//否则打印res储存的东西

        }

    } else {//如果大于1的话是不能完成任务的
        cout << -1 << '\n';

    }



    return 0;
}

E. Scuza

链接

E. Scuza

这个题是个二分题,先对数组求前缀和,但是发现给的数组的内容不是非递减的,所以我们要把数组变成非递减的,我们每次都用a0---ai的最大值赋值给ai这样能保证a[i]是非递减的,然后进行二分查找打印出来

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;
const int N = 200008;
int s[N];

void solve() {
    int n, q;
    int a[N];
    scanf("%lld %lld", &n, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        s[i] = s[i - 1] + a[i];//求前缀和
    }
    int mmax = a[0];//先让mmax为a[0]
    for (int i = 1; i <= n; i++) {
        if (a[i] > mmax) {
            mmax = a[i];
        } else {
            a[i] = mmax;
        }

    }//通过这个实现非递减的数组

//for(int i=0;i<n;i++){
//
//    cout<<a[i]<<' ';
//
//}


    while (q--) {
        int x;//每次读入x
        scanf("%lld", &x);
        int l = 0, r = n + 1;
        while (l + 1 != r) {
            int mid = (l + r) / 2;
            if (a[mid] <= x) {//因为要找最大值所以要让a[mid]<=x

                l = mid;
            } else {
                r = mid;
            }

        }
        cout << s[l] << ' ';//最后打印l坐标的前缀和的值

    }
    printf("\n");


}
signed main () {
    int t;
    cin >> t;
    while (t) {
        solve();
        t--;
    }


    return 0;
}

A. Drazil and Date

链接

A. Drazil and Date

这个题是一个水题,如果给的步数小于最小的步数就直接打印no,否则的话可以证明如果还要走到终点每次都要多走两步我们让差值直接模2

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;
const int N = 100008;


signed main () {
    int a, b, s;
    cin >> a >> b >> s;
    int len = abs(a) + abs(b);
    if (s < len) {//如果s小于len的话打印no
        cout << "No" << '\n';
        return 0;
    }
    if ((len - s) % 2 == 0) {//否则对差值进行模2如果为0打印yes
        cout << "Yes" << '\n';
    } else {
        cout << "No" << '\n';//否则打印no


    }


    return 0;
}
posted @ 2023-03-17 21:38  harper886  阅读(19)  评论(0编辑  收藏  举报