头像

南骁.




离线:10小时前


最近来访(57)
用户头像
芬达加曼陀罗
用户头像
s.y.
用户头像
xhxhxxh_
用户头像
一万小时定律
用户头像
用户头像
optimjie
用户头像
向往的生活
用户头像
LiSymbol
用户头像
zhyou
用户头像
AXax020913
用户头像
卿故倾.
用户头像
77777777777
用户头像
默_64
用户头像
人间小苦瓜_2
用户头像
AcWing2AK
用户头像
llllll_6
用户头像
冰之韵
用户头像
柳明月清风
用户头像
天气微凉
用户头像
etaf

活动打卡代码 AcWing 797. 差分

南骁.
10小时前
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N],s[N];

void insert(int l,int r,int c){//左右相互抵消使得l至r全+c
    s[l] += c;
    s[r + 1] -= c;
}

int main()
{
    int n,m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ ){
        scanf("%d",&a[i]);
        insert(i,i,a[i]);
    }

    while (m -- ){
        int l,r,c;
        cin>>l>>r>>c;
        insert(l,r,c);//插入
    }
    for (int i = 1; i <= n; i ++ )s[i] += s[i - 1];//前缀和
    for (int i = 1; i <= n; i ++ )printf("%d ",s[i]);
    return 0;
}


活动打卡代码 AcWing 796. 子矩阵的和

南骁.
1天前

复盘

查询是用y1 - 1,x1 - 1定减去的范围不是y2 - y1,x2 - x1;

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1010;
int g[N][N],s[N][N];

int main()
{
    int n,m,q;
    cin>>n>>m>>q;
    for (int i = 1; i <= n; i ++ )
       for(int j = 1; j <= m;j++ )
          cin>>g[i][j];

    for (int i = 1; i <= n; i ++ )
       for(int j = 1; j <= m;j++ )
          s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + g[i][j];


    while(q--){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        int res = s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 -1][y1 -1];
        cout<<res<<endl;
    }
    return 0;
}


活动打卡代码 AcWing 795. 前缀和

南骁.
1天前
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N],s[N];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i = 1; i <= n;i++)cin>>a[i];
    for(int i = 1; i <= n;i++) s[i] += s[i]+s[i - 1]+a[i];

    while(m--){
        int l,r;
        cin>>l>>r;
        cout<<s[r] - s[l - 1]<<endl;
    }
}


活动打卡代码 AcWing 794. 高精度除法

南骁.
1天前
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int remainder = 0;
vector<int> division(vector<int>& a,int b){
    vector<int> c,d;
    int t = 0;
    for(int i = a.size() - 1;i >= 0 ;i--){
        t = t*10+a[i];
        c.push_back(t/b);
        t = t - b*(t/b);
    }
    if(t)remainder = t;
    reverse(c.begin(),c.end());
    while(c.size()>1 && c.back()==0)c.pop_back();
    return c;
}
int main()
{
    string A;
    int b;
    cin>>A>>b;
    vector<int>a;
    for(int i = A.length() - 1 ;i >= 0 ;i--)a.push_back(A[i] - '0');
    auto c = division(a,b);
    for(int i = c.size() - 1 ;i >= 0 ;i--)cout<<c[i];
    cout<<endl<<remainder;
    return 0;
}


活动打卡代码 AcWing 53. 最小的k个数

南骁.
1天前
class Solution {
    public List<Integer> getLeastNumbers_Solution(int[] input, int k) {
        List<Integer> a = new ArrayList<>();
        if(k==0)return a;
        Queue<Integer> que = new PriorityQueue<>();
        for(int in : input) que.add(in);
        while(!que.isEmpty()){
                    a.add(que.remove());
                    if(a.size()==k)return a;
                }
        return null;
    }
}



南骁.
1天前

暴力枚举

class Solution {
    public int[] findNumbersWithSum(int[] nums, int target) {
        int res[] = new int[2];
        for(int i = 0;i<nums.length;i++){
            for(int j = 0;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    res[0] = nums[i];
                    res[1] = nums[j];
                    return res;
                }
            }
        }
        return res;
    }
}

Hash高级做法


class Solution {
    public int[] findNumbersWithSum(int[] nums, int target) {
       Scanner sc = new Scanner(System.in);
       Set<Integer> hash = new HashSet<>();
       for(int x : nums){
           int y = target - x;
           if(hash.contains(y))
                return new int[]{x,y};
           hash.add(x);
       }
       return null;
    }
}


活动打卡代码 AcWing 829. 模拟队列

南骁.
1天前
import java.util.*;
public class Main{
    public static void main(String[] args){
       Queue<Integer> que = new LinkedList<>();
       Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       for(int i = 0; i < n;i++){
           String op = sc.next();
           if("push".equals(op)){
               int x = sc.nextInt();
               que.add(x);
           }
           else if("pop".equals(op)){
               que.remove();
           }
           else if("empty".equals(op)){
               if(que.isEmpty()){
                  System.out.println("YES");
               }
               else{
                   System.out.println("NO");
               }
           }
           else{
               System.out.println(que.peek());
           }
       }
    }
}


活动打卡代码 AcWing 828. 模拟栈

南骁.
1天前
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
       Stack<Integer> stack = new Stack<Integer>();
       int n = sc.nextInt();
       for (int i = 0; i < n;i++){
           String s = sc.next();
           if("push".equals(s)){
               int x = sc.nextInt();
               stack.push(x);
           }
           else if("query".equals(s)&&!stack.empty()){
               System.out.println(stack.peek());
           }
           else if("pop".equals(s)&&!stack.empty()){
               stack.pop();
           }
           else {
               if(stack.empty()){
                   System.out.println("YES");
               }
               else {
                   System.out.println("NO");
               }
           }
       }
    }
}


活动打卡代码 AcWing 862. 三元组排序

南骁.
2天前

数组做法

import java.util.*;
public class Main{
    public static int N = 100010;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[n];
        float d[] = new float[N];
        String s[] = new String[N];
        for(int i = 0;i < n;i++){
            a[i] = sc.nextInt();
            d[a[i]] = sc.nextFloat();
            s[a[i]] = sc.next();
        }
        Arrays.sort(a);
        for(int i = 0;i < n;i++){
            System.out.printf("%d %.2f %s\n",a[i],d[a[i]],s[a[i]]);
        }
    }
}

类做法

import java.util.*;
class Data implements Comparable<Data>{
    int x;
    double y;
    String s;
    public Data(int x,double y,String s){
        this.x = x;
        this.y = y;
        this.s = s;
    }
    public int compareTo(Data t){
        return this.x - t.x;
    }
}
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Data a[] = new Data[n];
        for(int i = 0;i < n;i++){
            a[i] = new Data(sc.nextInt(),sc.nextDouble(),sc.next());
        }
        Arrays.sort(a);
        for(int i = 0;i < n;i++){
            System.out.printf("%d %.2f %s\n",a[i].x,a[i].y,a[i].s);
        }
    }
}



南骁.
2天前

状态数组做法

class Solution {
    public ListNode deleteDuplication(ListNode head) {
        if(head==null)return head;
        int st[] = new int[200];
        ListNode h = head;
        ListNode n = null;
        while(h != null){
            if(st[h.val]==1||st[h.val]==2){
                st[h.val]=2;
                n.next= h.next;
                h = h.next;
            }
            else {
                st[h.val] = 1;
                n = h;
                h = h.next;
            }
        }
        h = head;
        while(h.next != null){
            if(st[h.val]==2&&h==head){
                head = head.next;
                h = head;
            }
            else if(st[h.next.val]==2){
                h.next = h.next.next;
            }
            else h = h.next;
        }
        if(st[head.val]==2)head=null;
        return head;
    }
}

双指针

class Solution {
    public ListNode deleteDuplication(ListNode head) {
        ListNode dummy = new ListNode(-1);
        ListNode p = dummy;
        dummy.next = head;
        while(p != null){
            ListNode q = p.next;
            while(q != null && p.next.val==q.val)q = q.next;
            if(p.next!=null&&p.next.next != q) p.next = q;
            else p =p.next;
        }
        return dummy.next;
    }
}