2021.1.8寒假打卡Day5

寒假打卡 Day 5

数据结构

队列🔗

规则:先进先出(和我们食堂里排队一样,先来的人先轮到打饭 食堂好像不用排队
操作:

enqueue(x);		//在队列末尾添加元素x
dequeue();		//从队列开头取出元素
isEmpty();		//检查队列是否为空
isFull();		//检查队列是否已满

Queue🔗
There are n processes in a queue. Each process has namei and timei. The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue.

For example, we have the following queue with the quantum of 100ms.

A(150) - B(80) - C(200) - D(200)

First, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms).

B(80) - C(200) - D(200) - A(50)

Next, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue.

C(200) - D(200) - A(50)

Your task is to write a program which simulates the round-robin scheduling.

Input
n q
name1 time1
name2 time2

namen timen

In the first line the number of processes n and the quantum q are given separated by a single space.

In the following n lines, names and times for the n processes are given. namei and timei are separated by a single space.

Output
For each process, prints its name and the time the process finished in order.

Constraints
1 ≤ n ≤ 100000
1 ≤ q ≤ 1000
1 ≤ timei ≤ 50000
1 ≤ length of namei ≤ 10
1 ≤ Sum of timei ≤ 1000000

Sample Input 1

5 100
p1 150
p2 80
p3 200
p4 350
p5 20

Sample Output 1

p2 180
p5 400
p1 450
p3 550
p4 800

Notes
Template in C

code in book

#include<stdio.h>
#include<string.h>
#define len 100005;

typedef struct pp{
	char name[100];
	int t;
}p;

p q[len];
int head,tail,n;

viod enqueue(){
	q[tail]=x;
	tail=(tail+1)%len;
}

p dequeue(){
	p x=q[head];
	head=(head+1)%len;
	return x;
}

int min(int a,int b){return a<b?a:b;}

int main(){
	int elaps=0,c;
	int i,q;
	p u;
	scanf("%d %d",&n,&q);
	for(i=1;i<=n;i++){
		scanf("%s",q[i].name);
		scnaf("%d",&q[i].t);
	}
	head=1;tail=n+1;
	while(head!=tail){
		u=dequeue();
		c=min(q,u.t);
		u.t-=c;
		elaps+=c;
		if(u.t>0) enqueue(u);
		else printf("%s %d",u.name,elaps);
	}
	return 0;
}

链表🔗

可以理解成一串用指针串起来的非连续数组

Doubly Linked List🔗
Your task is to implement a double linked list.

Write a program which performs the following operations:

insert x: insert an element with key x into the front of the list.
delete x: delete the first element which has the key of x from the list. 
			If there is not such element, you need not do anything.
deleteFirst: delete the first element from the list.
deleteLast: delete the last element from the list.

Input
The input is given in the following format:
n
command1
command2

commandn

In the first line, the number of operations n is given.
In the following n lines, the above mentioned operations are given in the following format:

insert x
delete x
deleteFirst
deleteLast

Output
Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

Constraints
The number of operations ≤ 2,000,000
The number of delete operations ≤ 20
0 ≤ value of a key ≤ 109

The number of elements in the list does not exceed 106
For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

Sample Input 1

7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5

Sample Output 1

6 1 2

Sample Input 2

9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast

Sample Output 2

1

code in book

#include<cstdio>
#include<cstdlib>
#include<cstring>

struct Node{
	int key;
	Node *next, *prev;
};

Node *nil;

Node* listSearch(int key){
	Node *cur=nil->next;
	while ( cur != nil && cur->key != key ) {
		cur=cur->next;
	}
	return cur;
}

void init(){
	nil = (Node *)malloc(sizeof(Node));
	nil->next = nil;
	nil->prev = nil;
}

void printList(){
	Node *cur = nil->next;
	int isf = 0;
	while(1){
		if (cur==nil) break;
		if (isf++>0) printf(" ");
		printf("%d",cur->key);
		cur=cur->next;
	}
	printf("\n");
}

void deleteNode(Node *t){
	if(t==nil) return;
	t->prev->next=t->next;
	t->next->prev=t->prev;
	free(t);
}

void deleteFirst(){
	deleteNode(nil->next);
}

void deleteLast(){
	deleteNode(nil->prev) ;
}

void deleteKey(int key){
	deleteNode(listSearch(key)) ;
}

void insert(int key){
	Node=(Node)malloc(sizeof(Node));
	x->key=key;
	x->next=nil->next;
	nil->next->prev= x;
	nil->next=x;
	x->prev=nil;
}

int main(){
	int key, n, i;
	int size=0;
	char com[20];
	int np=0,nd=0;
	scanf("%d",&n);
	init();
	for(i=0;i<n;i++){
		scanf("%s%d",com,&key);
		if(com[0]=='i'){
			insert(key);
			np++;
			size++;
		}
		else if(com[0]=='d'){
			if(strlen(com)>6){ 
				if(com[6]=='F') deleteFirst();
				else if(com[6]=='L') deleteLast();
			}
			else{
				deleteKey(key);
				nd++;
			}
			size--;
		}
	}
	printList();
	return 0;
}

哈尔滨学院2021ACM冬令营Day1

A - Download Manager

Jiajia downloads a lot, a lot more than you can even imagine. Some say that he starts downloading up to 20,000 files together. If 20,000 files try to share a limited bandwidth then it will be a big hazard and no files will be downloaded properly. That is why, he uses a download manager.

If there are T files to download, the download manger uses the following policy while downloading files:

  1. The download manager gives the smaller files higher priority, so it starts downloading the smallest n files at startup. If there is a tie, download manager chooses the one with less bytes remaining (for download). We assume that with at least 50 Mega Bytes/sec of bandwidth, n files can be downloaded simultaneously without any problem.

  2. The available bandwidth is equally shared by the all the files that are being downloaded. When a file is completely downloaded its bandwidth is instantaneously given to the next file. If there are no more files left except the files that are being downloaded, this bandwidth is immediately shared equally by all remaining files that are being downloaded.

Given the size and completed percentage of each file, your task is to intelligently simulate the behavior of the download manager to find the total time required to download all the files.

Input
The will be at most 10 test cases. Each case begins with three integers T (1 <= T <= 20000), n (1 <= n <= 2000 and 1 <= n <= T) and B (50 <= B <= 1000). Here B denotes the total bandwidth available to Jiajia (In Megabytes/sec). Please note that the download manager always downloads n files in parallel unless there are less than n files available for download. Each of next T lines contains one non-negative floating-point number S (less than 20,000, containing at most 2 digits after the decimal places) and one integer P (0 <= P <= 100). These two numbers denote a file whose size is S megabyte and which has been downloaded exactly P% already. Also note that although theoretically it is not possible that the size of a file or size of its remaining part is a fraction when expressed in bytes, for simplicity please assume that such thing is possible in this problem. The last test case is followed by T=n=B=0, which should not be processed.

Output
For each case, print the case number and the total time required to download all the files, expressed in hours and rounded to 2 digits after the decimal point. Print a blank line after the output of each test case.

Sample Input

6 3 90
100.00 90
40.40 70
60.30 70
40.40 80
40.40 85
40.40 88
1 1 56
12.34 100
0 0 0

Sample Output

Case 1: 0.66

Case 2: 0.00

Explanation
In the first sample, there are 6 files and the download manager can download 3 files simultaneously. The size of the smallest file is 40.40 Megabyte but there are four such files (2nd, 4th, 5th and 6th files). So the download manager chooses the 6th, 5th and 4th files for download as they have less bytes remaining. All these files get equal bandwidth (30.00 Megabyte/Sec). Of these three files the 8th file is finished first. So instantaneously the 2nd file starts downloading. Then, 5th file is finished. So the next larger file (3rd file) starts downloading. This process goes on until all files are downloaded.

题目大意:每个案例中共有T个文件,下载速度是B M/s,告知这T个文件的大小S和下载进度P%,计算每个案例下载所需的时间。
什么?你和我说下载规则?再怎么花式下载,下载需要的时间难道不就是取决于文件大小和下载速度嘛? 要真的按原样模拟出来了,那你也的确是一个大佬(๑•̀ㅂ•́)و✧因为我不会

思路:ans=(S1 * (100 - P1) / 100 + S2 * (100 - P2) / 100 + ··· + Sn * (100 - Pn) / 100) / B

#include<iostream>
using namespace std;

int main(){
	int t,n,b,p,cnt=1;
	double s,ans=0;
	cin>>t>>n>>b;
	while(t){
		while(t--){
			cin>>s>>p;
			ans+=s*(100-p)/100;
		}
		ans/=b;
		if(cnt>1) cout<<endl;
		printf("Case %d: %.2lf\n",cnt,ans);
		ans=0;
		/* next case */
		++cnt;
		cin>>t>>n>>b;
	}
	return 0;
}

B - sort

给你n个整数,请按从大到小的顺序输出其中前m大的数。

Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。

Output
对每组测试数据按从大到小的顺序输出前m大的数。

Sample Input

5 3
3 -35 92 213 -644

Sample Output

213 92 3

直接上快排!!!

#include<iostream>
using namespace std;
int a[1000005];

void qsort(int l,int r){
    int k=a[(l+r)/2];
    int i=l,j=r;
    do{
        while(a[i]<k) i++;
        while(a[j]>k) j--;
        if(i<=j){
            swap(a[i],a[j]);
            i++;
            j--;
        }
    }while(i<=j);
    if(l<j) qsort(l,j);
    if(i<r) qsort(i,r);
}

int main(){
	int m,n;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    qsort(1,n);
    for(int i=1;i<=m;i++){
    	if(i<1) cout<<' '<<a[i];
    	else cout<<a[i];
    return 0;
}

C - 圆桌问题

圆桌上围坐着2n个人。其中n个人是好人,另外n个人是坏人。如果从第一个人开始数数,数到第m个人,则立即处死该人;然后从被处死的人之后开始数数,再将数到的第m个人处死……依此方法不断处死围坐在圆桌上的人。试问预先应如何安排这些好人与坏人的座位,能使得在处死n个人之后,圆桌上围坐的剩余的n个人全是好人。

Input
多组数据,每组数据输入:好人和坏人的人数n(<=32767)、步长m(<=32767)

Output
对于每一组数据,输出2n个大写字母,‘G’表示好人,‘B’表示坏人,50个字母为一行,不允许出现空白字符。相邻数据间留有一空行。

Sample Input

2 3
2 4

Sample Output

GBBG

BGGB

思路:模拟圆桌——vector< int > v;记录处死位置——int seat[32767*2];

#include<iostream>
#include<vector>
using namespace std;

int main(){
	vector<int> v;
	int seat[32767*2]={0};
	int m,n,k=0;
	cin>>n>>m;
	for(int i=0;i<2*n;i++) vector.push_back(i);
	for(int i=0;i<n;i++){
		k=(k+m-1)%v.size();
		seat[v[k]]=1;
		v.erase(v.begin()+k-1);
	}
	for(int i=0;i<2*n;i++){
		if(seat[i]==0) cout<<'G';
		else cout<<'B';
		if((i+1)%50==0) cout<<endl;
	}
	return 0;
}

D - Text Reverse

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output
For each test case, you should output the text which is processed.

Sample Input

3
olleh !dlrow
m'I morf .udh
I ekil .mca

Sample Output

hello world!
I'm from hdu.
I like acm.

Hint
Remember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.

思路:用char数组存储句子,用 i 指向单词开头,用 j 指向空格,然后倒序输出,再处理下一个单词……
观察,发现输入输出相反正好可以用实现。
那么思路可以是,入栈 - 空格 - 出栈 - ··· - 入栈 - 空格 - 出栈 - 出栈最后一个单词

#include<iostream>
#include<stack>
using namespace std;

int main(){
	stack<char> s;
	char c;
	int t;
	cin>>t;
	while(t--){
		while((c=putchar())!='\n'){
			if(c==' '){
				while(!s.empty){
					cout>>s.top();
					s.pop();
				}
			}else{
				s.push(c);
			}
		}
		while(!s.empty){
			cout>>s.top();
			s.pop();
		}
	}
	return 0;
}

剩下的EFGHIJK明天再看吧
(。・∀・)ノ゙咕 night ~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值