头像

puppyCha




离线:3天前


最近来访(38)
用户头像
77777777777
用户头像
moli2
用户头像
lyq_摆烂日常
用户头像
要好好学习鸭
用户头像
fightingwin
用户头像
情断青丝
用户头像
404NotFound.
用户头像
一万小时定律
用户头像
yjc123
用户头像
数论贪心图论DP通通不会
用户头像
SE6_01
用户头像
黑黑黑
用户头像
Fatin
用户头像
快快睡觉
用户头像
香香小马
用户头像
Y_Y_Y
用户头像
易思人
用户头像
疾风亦有归途_0
用户头像
xc_5
用户头像
pccc


java

class MyCircularDeque {

    int[] q;
    int tt = 0, hh = 0;

    private int get(int x) {
        return (x + q.length) % q.length;
    }

    public MyCircularDeque(int k) {
        q = new int[k+1];
    }

    public boolean insertFront(int value) {
        if (isFull()) return false;
        hh = get(hh-1);
        q[hh] = value;
        return true;
    }

    public boolean insertLast(int value) {
        if (isFull()) return false;
        q[tt] = value;
        tt = get(tt+1);
        return true;
    }

    public boolean deleteFront() {
        if (isEmpty()) return false;
        hh = get(hh+ 1);
        return true;
    }

    public boolean deleteLast() {
        if (isEmpty()) return false;
        tt = get(tt-1);
        return true;
    }

    public int getFront() {
        if (isEmpty()) return -1;
        return q[hh];
    }

    public int getRear() {
        if (isEmpty()) return -1;
        return q[get(tt-1)];
    }

    public boolean isEmpty() {
        return tt == hh;
    }

    public boolean isFull() {
        return tt == hh-1;
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */

c++

class MyCircularDeque {
public:
    vector<int> q;
    int hh = 0, tt = 0;

    int get(int x) {
        return (x + q.size()) % q.size();
    }

    MyCircularDeque(int k) {
        q.resize(k+1);
    }

    bool insertFront(int value) {
        if (isFull()) return false;
        hh = get(hh-1);
        q[hh] = value;
        return true;
    }

    bool insertLast(int value) {
        if (isFull()) return false;
        q[tt] = value;
        tt = get(tt + 1);
        return true;
    }

    bool deleteFront() {
        if (isEmpty()) return false;
        hh = get(hh+1);
        return true;
    }

    bool deleteLast() {
        if (isEmpty()) return false;
        tt = get(tt-1);
        return true;
    }

    int getFront() {
        if (isEmpty()) return -1;
        return q[hh];
    }

    int getRear() {
        if (isEmpty()) return -1;
        return q[get(tt-1)];
    }

    bool isEmpty() {
        return hh == tt;
    }

    bool isFull() {
        return tt == get(hh-1);
    }
};

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque* obj = new MyCircularDeque(k);
 * bool param_1 = obj->insertFront(value);
 * bool param_2 = obj->insertLast(value);
 * bool param_3 = obj->deleteFront();
 * bool param_4 = obj->deleteLast();
 * int param_5 = obj->getFront();
 * int param_6 = obj->getRear();
 * bool param_7 = obj->isEmpty();
 * bool param_8 = obj->isFull();
 */


活动打卡代码 AcWing 4405. 统计子矩阵

java

import java.io.*;
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
        int[][] s = new int[n+1][m+1];
        for (int i = 1; i <= n; i ++ )
            for (int j = 1; j <= m; j ++ )
                s[i][j] = sc.nextInt() + s[i-1][j];

        long res = 0;
        for (int i = 1; i <= n; i ++ )
            for (int j = i; j <= n; j ++ )
                for (int l = 1, r = 1, sum = 0; r <= m; r ++ )
                {
                    sum += s[j][r] - s[i-1][r];
                    while (sum > k)
                    {
                        sum -= s[j][l] - s[i-1][l];
                        l ++ ;
                    }
                    res += r - l + 1;
                }
        System.out.println(res);
    }
}

c++

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N =  510;

int n, m, k;
int s[N][N];

long long res;

int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
        {
            cin >> s[i][j];
            s[i][j] += s[i-1][j];
        }

    for (int i = 1; i <= n; i ++ )
        for (int j = i; j <= n; j ++ )
            for (int l = 1, r = 1, sum = 0; r <= m; r ++ ){
                sum += s[j][r] - s[i-1][r];
                while (sum > k){
                    sum -= s[j][l] - s[i-1][l];
                    l ++ ;
                }
                res += r - l + 1;
            }

    cout << res << endl;
    return 0;
}



c++代码

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        stack<int> stk;
        for (auto x : arr){
            int t = x;
            while (stk.size() && stk.top() > x){
                t = max(t, stk.top());
                stk.pop();
            }
            stk.push(t);
        }
        return stk.size();
    }
};

java代码

class Solution {
    public int maxChunksToSorted(int[] arr) {
        Stack<Integer> stk = new Stack<>();
        for (int x: arr){
            int t = x;
            while(!stk.empty() && stk.peek() > x){
                t = Math.max(t, stk.peek());
                stk.pop();
            }
            stk.push(t);
        }
        return stk.size();
    }
}


活动打卡代码 LeetCode 1282. 用户分组

c++代码

class Solution {
public:
    vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
        vector<vector<int>> res;
        unordered_map<int, vector<int>> hash;
        for (int i = 0; i < groupSizes.size(); i ++ ){
            int x = groupSizes[i];
            hash[x].push_back(i);
            if (hash[x].size() == x){
                res.push_back(hash[x]);
                hash[x].clear();
            }
        }
        return res;
    }
};

java代码

class Solution {
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        Map<Integer, List<Integer>> hash = new HashMap<Integer, List<Integer>>();
        List<List<Integer>> res = new ArrayList<>();
        int n = groupSizes.length;
        for (int i = 0; i < n; i ++ ){
            int x = groupSizes[i];
            if (hash.get(x) == null) hash.put(x, new ArrayList<>());
            hash.get(x).add(i);
            if (hash.get(x).size() == x){
                res.add(hash.get(x));
                hash.put(x, null);
            }
        }
        return res;
    }
}


活动打卡代码 AcWing 165. 小猫爬山

puppyCha
11天前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 20;

int n, m;
int w[N], sum[N], ans = N;

void dfs(int u, int k)
{
    if (k >= ans) return;
    if (u == n)
    {
        ans = k;
        return;
    }

    for (int i = 0; i < k; i ++ )
        if (sum[i] + w[u] <= m)
        {
            sum[i] += w[u];
            dfs(u+1, k);
            sum [i] -= w[u];
        }

    sum[k] = w[u];
    dfs(u+1, k+1);
    sum[k] = 0;
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) cin >> w[i];

    sort(w, w+n);
    reverse(w, w+n);

    dfs(0, 0);
    cout << ans << endl;

    return 0;
}



puppyCha
12天前
#include <iostream>
using namespace std;
int main(){cout << "I'am so vegetable!!!" << endl;}


活动打卡代码 AcWing 2070. 自行车之旅

puppyCha
12天前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int a[105];

int main()
{
    int cases;
    cin >> cases;
    for (int i = 1; i <= cases; i ++ )
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ ) cin >> a[i];
        printf("Case #%d: ", i);
        int cnt = 0;
        for (int i = 1; i < n - 1; i ++ )
            if (a[i] > a[i-1] && a[i] > a[i+1])
                cnt ++ ;
        cout << cnt << endl;
    }

    return 0;
}


活动打卡代码 AcWing 3526. 素数

puppyCha
12天前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int flag;

bool is_prime(int x)  // 判定质数
{
    if (x < 2) return false;
    for (int i = 2; i <= x / i; i ++ )
        if (x % i == 0 || x % 10 != 1)
            return false;
    return true;
}

int main()
{
    int x;
    while (cin >> x)
    {
        flag = 0;
        for (int i = 11; i < x; i ++ )
            if (is_prime(i)) {
                cout << i << ' ';
                flag = 1;
            }
        if (!flag) cout << -1;
        cout << endl;
    }

    return 0;
}


活动打卡代码 AcWing 3449. 数字根

puppyCha
12天前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    string str;
    while (cin >> str && str != "0")
    {
        int sum = 0;
        for (auto c : str) sum += c - '0';
        sum %= 9;
        if (!sum) sum = 9;
        cout << sum << endl;
    }

    return 0;
}


活动打卡代码 AcWing 3589. 平方因子

puppyCha
12天前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> a;
    for (int i = 2; i <= 100; i ++ ) a.push_back(i*i);

    int x;
    while (cin >> x)
    {
        int flag = 1;
        for (int i = 0; i < a.size(); i ++ )
        {
            if (x % a[i] == 0) {
                cout << "Yes" << endl;
                flag = 0;
                break;
            }
        }
        if (flag) cout << "No" << endl;
    }

    return 0;
}