1 solutions
-
0
C :
#include <stdio.h> #include <stdlib.h> struct list{ int num; struct list *next; }; typedef struct list Sqlist; Sqlist* qAdd(Sqlist*, const int); int qDel(Sqlist**); Sqlist* ADD(Sqlist*, const int); int GET(Sqlist*, const int); int main(){ int M, N, i, j, k, command, tmp, num; Sqlist *head; Sqlist *rear; head = (Sqlist*)malloc(sizeof (Sqlist)); rear = (Sqlist*)malloc(sizeof (Sqlist)); while (EOF != scanf("%d%d", &M, &N)){ head->next = NULL; rear->next = rear; for (i = 0; i < M; i++){ scanf("%d", &num); rear = qAdd(rear, num); } i = 0; k = 0; for (j = 0; j < N; j++){ scanf("%d", &command); while (k < command){ head = ADD(head, qDel(&rear)); k++; } tmp = GET(head, i); i++; printf("%d\n", tmp); } } return 0; } Sqlist* ADD(Sqlist* head, const int num) { Sqlist *p, *q, *s; p = head; while (q = p->next){ if (q == NULL){ break; } if (q->num < num){ p = q; }else{ break; } } s = (Sqlist*)malloc(sizeof (Sqlist)); s->num = num; s->next = q; p->next= s; return head; } int GET(Sqlist* head, const int num) { int i; Sqlist *q; q = head->next; for (i = 0; i < num; i++){ if (q == NULL){ break; } q = q->next; } return q->num; } Sqlist* qAdd(Sqlist* rear, const int num) { Sqlist *head, *s; s = (Sqlist*)malloc(sizeof (Sqlist)); s->num = num; s->next = rear->next;; rear->next = s; rear = s; return rear; } int qDel(Sqlist** _rear) { int tmp; Sqlist *head, *q; head = (*_rear)->next; q = head->next; head->next = q->next; tmp = q->num; free(q); return tmp; }C++ :
#include<stdio.h> #include<queue> #include<vector> #include <cstring> #include<algorithm> #define MAX 30010 using namespace std; int num[MAX], a[MAX]; int main(){ int i, j, n, m, tem; //freopen("1.in", "r", stdin); //freopen("1.out", "w", stdout); while(scanf("%d%d", &n, &m) != EOF){ priority_queue<int, vector<int>, greater<int> > Squeue; priority_queue<int, vector<int>, less<int> > Gqueue; memset(a, 0, sizeof(a)); for(i = 1; i <= n; i++){ scanf("%d", &num[i]); } for(i = 1; i <= m; i++){ scanf("%d", &a[i]); } j = 1; for(i = 1; i <= n; i++){ Squeue.push(num[i]); if(!Gqueue.empty() && Squeue.top() < Gqueue.top()){ int S = Squeue.top(); int G = Gqueue.top(); Squeue.pop(); Gqueue.pop(); Squeue.push(G); Gqueue.push(S); } while(a[j] == i){ printf("%d\n", Squeue.top()); Gqueue.push(Squeue.top()); Squeue.pop(); j++; } } } return 0; }
- 1
Information
- ID
- 19360
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By