8.8总结

1.今天写了两道算法题,首先第一道

这个就是一道排序题,直接用贪心算法就能解出来

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
const int N = 2e5+10;


void solve() {
	int n , a[N];
	scanf("%d", &n);
	int mx, id = -1;

	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		if (id == -1 || a[i] < mx) {
			mx = a[i];
			id = i;
		}
	}

	for (int i = id + 1; i <= n; i++) {
		if (a[i] < a[i - 1]) {
			puts("-1");
			return;
		}
	}
	printf("%d\n", id - 1);
}
int main() {
	int t = 1;
	scanf("%d", &t);
	while (t--) solve();
	return 0;
}

 

第二道是一个优先队列的问题,是要进行三种操作

  1. 将一个写有整数X的球放入袋子内;
  2. 将袋子里所有球上的数字加上X;
  3. 拿出袋子内数字最小的球并记录这个数字。
    对于每一个操作3,输出记录的数字。

用优先队列的方法保证最小值一直在队首就可以保证他第一个出队

注意 这道题的数据值很大 记得要开long long,还有一点就是要注意在提交的时候如果语言选择C语言的话是不能使用全局变量的,只有c++语言用全局变量才可以过

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
using namespace std;

typedef long long ll;
const int N = 1e6 + 5;

ll heap[N];
int sized = 0;
ll sum = 0;

void push(ll x) {
    int i = sized++;
    while (i > 0) {
        int parent = (i - 1) / 2;
        if (heap[parent] <= x) break;
        heap[i] = heap[parent];
        i = parent;
    }
    heap[i] = x;
}

ll pop() {
    if (sized == 0) return -1;
    ll min_val = heap[0];
    ll last = heap[--sized];
    int i = 0;
    while (i * 2 + 1 < sized) {
        int child = i * 2 + 1;
        if (child + 1 < sized && heap[child + 1] < heap[child]) {
            child++;
        }
        if (last <= heap[child]) break;
        heap[i] = heap[child];
        i = child;
    }
    heap[i] = last;
    return min_val;
}

int main() {
    ll q;
    scanf("%lld", &q);

    while (q--) {
        ll f, x;
        scanf("%lld", &f);
        if (f == 1) {
            scanf("%lld", &x);
            push(x - sum);
        }
        else if (f == 2) {
            scanf("%lld", &x);
            sum += x;
        }
        else {

            printf("%lld\n", pop() + sum);
        }
    }

    return 0;
}

 2。有关于项目 这两天完善了创建班级,加入班级和签到的内容使他更加符合实际,对于列表的更新这里使用了每过一段时间过后就定时刷新一下,使列表能够实现实时更新

    private void setupRefresh() {

        Duration refreshInterval = Duration.millis(7);

        refreshTimeline = new Timeline(new KeyFrame(refreshInterval, event -> refreshSignInRecords()));
        refreshTimeline.setCycleCount(Timeline.INDEFINITE);
        refreshTimeline.play();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值