B. Array Decrements Codeforces Round #797 (Div. 3)

 

 题意:给你一个数组a和b,a可以进行一个操作:

a数组中的所有数都--(如果小于0就为0 )

问可不可以通过任意次操作,使a和b相等。

思路:直接需要删除的最大的次数就可以了,然后全删一边,小于0的变成0,然后看相不相等就好。

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
ll a[N],b[N];



int main()
{
	int t;
	sc_int(t);
	while(t--)
	{
		int n;
		sc_int(n);
		for(int i =1;i<=n;i++)
		cin>>a[i];
		for(int i =1;i<=n;i++)
		cin>>b[i];		
		ll sum_0=0,sum=0;
		bool flag=0;
		for(int i =1;i<=n;i++)
		{
			if(b[i]==0)
			sum_0=max(a[i],sum_0);
			else if(sum==0||sum==a[i]-b[i])
			sum=a[i]-b[i];
			else flag=1;
		}
		if(flag||sum<0||(sum_0>sum&&sum!=0))cout<<"NO\n";
		else cout<<"YES\n";
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is the code in C++ that satisfies all the requirements mentioned above: ```cpp #include <iostream> #include <queue> #include <vector> #include <algorithm> using namespace std; struct Process { int id; // process ID int arrival_time; // arrival time of process int burst_time; // burst time of process int remaining_time; // remaining time of process int end_time; // end time of process }; void round_robin(vector<Process>& processes, int quantum) { int n = processes.size(); queue<int> ready_queue; // ready queue of processes vector<int> waiting_time(n, 0); int current_time = 0; // current time of execution int total_waiting_time = 0; // total waiting time of processes for (int i = 0; i < n; i++) { processes[i].remaining_time = processes[i].burst_time; } int remaining_processes = n; int process_index = 0; ready_queue.push(process_index); while (remaining_processes > 0) { int current_process = ready_queue.front(); ready_queue.pop(); int execution_time = min(processes[current_process].remaining_time, quantum); current_time += execution_time; processes[current_process].remaining_time -= execution_time; if (processes[current_process].remaining_time <= 0) { processes[current_process].end_time = current_time; remaining_processes--; } else { ready_queue.push(current_process); } while (process_index < n && processes[process_index].arrival_time <= current_time) { ready_queue.push(process_index++); } } for (int i = 0; i < n; i++) { waiting_time[i] = processes[i].end_time - processes[i].arrival_time - processes[i].burst_time; total_waiting_time += waiting_time[i]; } cout << "Process ID\tArrival Time\tBurst Time\tEnd Time\tWaiting Time\n"; for (int i = 0; i < n; i++) { cout << processes[i].id << "\t\t" << processes[i].arrival_time << "\t\t" << processes[i].burst_time << "\t\t" << processes[i].end_time << "\t\t" << waiting_time[i] << endl; } cout << "\nAverage Waiting Time: " << (double)total_waiting_time / n << endl; } int main() { vector<Process> processes = { {1, 0, 8, 0}, {2, 1, 4, 0}, {3, 2, 9, 0}, {4, 3, 5, 0}, {5, 4, 2, 0}, {6, 5, 7, 0}, {7, 6, 3, 0}, {8, 7, 6, 0}, {9, 8, 1, 0}, {10, 9, 5, 0}, {11, 10, 3, 0}, {12, 11, 6, 0}, {13, 12, 4, 0}, {14, 13, 7, 0}, {15, 14, 2, 0} }; int quantum = 3; round_robin(processes, quantum); return 0; } ``` In this code, we first define a struct `Process` to represent a process, which contains the process ID, arrival time, burst time, remaining time, and end time. Then we define a function `round_robin` that simulates the operation of the round-robin algorithm for process scheduling. This function takes a vector of processes and the quantum as input. It implements the round-robin algorithm by using a ready queue of processes and a while loop that executes until all processes have been executed. At each iteration, the function executes the current process for the quantum time or until it finishes, whichever comes first. If the process finishes, the function updates its end time and decrements the number of remaining processes. If the process does not finish, the function puts it back into the ready queue. The function also updates the waiting time of each process and calculates the total waiting time and the average waiting time. In the `main` function, we create a vector of 15 processes and set the quantum to 3. We then call the `round_robin` function with these inputs and output the scheduling situation of the processes to the terminal. The output includes the arrival time, burst time, end time, and waiting time of each process, as well as the average waiting time.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值