problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02264
C++
Memory Limit Exceeded
// // ALDS1_3_B.cpp // // // Created by mk on 2015/12/02. // // #include <iostream> #include <stdio.h> #include <string.h> #define LEN 100005 using namespace ::std; typedef struct pp { char name[1000]; int t; } P; P Q[LEN]; int head, tail, n; void enqueue(P x) { Q[tail] = x; tail = (tail + 1) % LEN; } P dequeue() { P x = Q[head]; head = (head + 1) % LEN; return x; } int main(int argc, const char *argv[]) { int elaps = 0, c; int q; P u; scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%s", Q[i].name); scanf("%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\n", u.name, elaps); } } return 0; }
// // ALDS1_3_B.cpp // // // Created by mk on 2015/12/02. // // #include <iostream> #include <stdio.h> #include <string.h> #define LEN 100005 using namespace ::std; typedef struct pp { char name[100]; int t; } P; P Q[LEN]; int head, tail, n; void enqueue(P x) { Q[tail] = x; tail = (tail + 1) % LEN; } P dequeue() { P x = Q[head]; head = (head + 1) % LEN; return x; } int main(int argc, const char *argv[]) { int elaps = 0, c; int q; P u; scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%s", Q[i].name); scanf("%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\n", u.name, elaps); } } return 0; }
replace
16
17
16
17
MLE
p02264
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, q; cin >> n; cin >> q; // vector<int> tm[n]; // vector<string> name[n]; int tm[100000]; string name[100000]; int total = 0; //??????????????¨ for (int i = 0; i < n; i++) { cin >> name[i]; cin >> tm[i]; total += tm[i]; } int ptm = 0; // passed time while (1) { for (int i = 0; i < n; i++) { if (tm[i] <= q) { if (tm[i] == 0) continue; ptm += tm[i]; tm[i] = 0; cout << name[i] << " " << ptm << endl; if (total == ptm) return 0; } else { ptm += q; tm[i] -= q; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, q; cin >> n; cin >> q; // vector<int> tm[n]; // vector<string> name[n]; int tm[100000]; string name[100000]; int total = 0; //??????????????¨ for (int i = 0; i < n; i++) { cin >> name[i]; cin >> tm[i]; total += tm[i]; } int ptm = 0; // passed time while (1) { for (int i = 0; i < n; i++) { if (tm[i] == 0) { } else if (tm[i] <= q) { ptm += tm[i]; tm[i] = 0; cout << name[i] << " " << ptm << endl; if (total == ptm) return 0; } else { ptm += q; tm[i] -= q; } } } return 0; }
replace
20
23
20
23
TLE
p02264
C++
Runtime Error
#include <array> #include <iostream> using namespace std; struct Process { string name; int time; }; class ProcessQueue { public: ProcessQueue(){}; virtual ~ProcessQueue(){}; bool push(Process &process) { if (isFull()) { return false; } queue[head] = process; head++; if (head == MAX) { head = 0; } return true; } Process &pop() { if (isEmpty()) { // TODO: throw exception. } Process &value = queue[tail]; tail++; if (tail == MAX) { tail = 0; } return value; } bool isEmpty() { return head == tail; } bool isFull() { return (head + tail) == MAX; } private: static const int MAX = 100000; array<Process, MAX> queue; int head; int tail; }; static void inputProcess(ProcessQueue &processQueue, int n) { Process in; for (int i = 0; i < n; i++) { cin >> in.name >> in.time; processQueue.push(in); } } int main() { int n, q; cin >> n >> q; ProcessQueue processQueue; inputProcess(processQueue, n); int total = 0; while (!processQueue.isEmpty()) { Process now = processQueue.pop(); if (now.time <= q) { total += now.time; cout << now.name << ' ' << total << endl; } else { now.time -= q; total += q; processQueue.push(now); } } return 0; }
#include <array> #include <iostream> using namespace std; struct Process { string name; int time; }; class ProcessQueue { public: ProcessQueue() : head(), tail(), queue(){}; virtual ~ProcessQueue(){}; bool push(Process &process) { if (isFull()) { return false; } queue[head] = process; head++; if (head == MAX) { head = 0; } return true; } Process &pop() { if (isEmpty()) { // TODO: throw exception. } Process &value = queue[tail]; tail++; if (tail == MAX) { tail = 0; } return value; } bool isEmpty() { return head == tail; } bool isFull() { return (head + tail) == MAX; } private: static const int MAX = 100000; array<Process, MAX> queue; int head; int tail; }; static void inputProcess(ProcessQueue &processQueue, int n) { Process in; for (int i = 0; i < n; i++) { cin >> in.name >> in.time; processQueue.push(in); } } int main() { int n, q; cin >> n >> q; ProcessQueue processQueue; inputProcess(processQueue, n); int total = 0; while (!processQueue.isEmpty()) { Process now = processQueue.pop(); if (now.time <= q) { total += now.time; cout << now.name << ' ' << total << endl; } else { now.time -= q; total += q; processQueue.push(now); } } return 0; }
replace
12
13
12
13
-11
p02264
C++
Runtime Error
#include <iostream> #include <string> using namespace std; struct Process { char name[10]; int time; }; static const int MAX = 10000; Process P[MAX]; int tail, head; void enqueue(struct Process &p) { P[tail] = p; tail = (tail + 1) % MAX; } void dequeue(struct Process &p) { p = P[head]; head = (head + 1) % MAX; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> P[i].name >> P[i].time; } int count = 0; tail = n; struct Process p; while (head != tail) { dequeue(p); p.time -= q; if (p.time <= 0) { count += q + p.time; cout << p.name << " " << count << endl; } else { count += q; enqueue(p); } } }
#include <iostream> #include <string> using namespace std; struct Process { char name[10]; int time; }; static const int MAX = 100000; Process P[MAX]; int tail, head; void enqueue(struct Process &p) { P[tail] = p; tail = (tail + 1) % MAX; } void dequeue(struct Process &p) { p = P[head]; head = (head + 1) % MAX; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> P[i].name >> P[i].time; } int count = 0; tail = n; struct Process p; while (head != tail) { dequeue(p); p.time -= q; if (p.time <= 0) { count += q + p.time; cout << p.name << " " << count << endl; } else { count += q; enqueue(p); } } }
replace
10
11
10
11
0
p02264
C++
Runtime Error
#include <iostream> #include <queue> #include <string> using namespace std; #define max_datasets 1000000 int main() { queue<string> data1; queue<int> data2; string kari_data1; int kari_data2; int all_time = 0; int n, q; string name[max_datasets]; int time[max_datasets]; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> name[i] >> time[i]; data1.push(name[i]); data2.push(time[i]); } while (data1.size() != 0) { if (data2.front() <= q) { all_time += data2.front(); cout << data1.front() << " " << all_time << endl; data1.pop(); data2.pop(); } else { data2.front() -= q; all_time += q; kari_data1 = data1.front(); kari_data2 = data2.front(); data1.pop(); data2.pop(); data1.push(kari_data1); data2.push(kari_data2); } } return 0; }
#include <iostream> #include <queue> #include <string> using namespace std; #define max_datasets 500000 int main() { queue<string> data1; queue<int> data2; string kari_data1; int kari_data2; int all_time = 0; int n, q; string name[max_datasets]; int time[max_datasets]; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> name[i] >> time[i]; data1.push(name[i]); data2.push(time[i]); } while (data1.size() != 0) { if (data2.front() <= q) { all_time += data2.front(); cout << data1.front() << " " << all_time << endl; data1.pop(); data2.pop(); } else { data2.front() -= q; all_time += q; kari_data1 = data1.front(); kari_data2 = data2.front(); data1.pop(); data2.pop(); data1.push(kari_data1); data2.push(kari_data2); } } return 0; }
replace
6
7
6
7
-11
p02264
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int que[100000]; int head = 0; int tail = 0; void enq(int a) { que[tail] = a; tail = (tail + 1) % 100000; } int deq() { return que[head++]; } bool empty() { return head == tail; } int main() { int n, q; char name[100000][11]; int time[100000]; int clock = 0; scanf("%d%d", &n, &q); for (int i = 0; i < n; i++) { scanf("%s%d", name[i], &time[i]); enq(i); } while (!empty()) { int i = deq(); if (time[i] <= q) { clock += time[i]; time[i] = 0; printf("%s %d\n", name[i], clock); } else { clock += q; time[i] -= q; enq(i); } } }
#include <bits/stdc++.h> using namespace std; int que[100000]; int head = 0; int tail = 0; void enq(int a) { que[tail] = a; tail = (tail + 1) % 100000; } int deq() { int h = head; head = (head + 1) % 100000; return que[h]; } bool empty() { return head == tail; } int main() { int n, q; char name[100000][11]; int time[100000]; int clock = 0; scanf("%d%d", &n, &q); for (int i = 0; i < n; i++) { scanf("%s%d", name[i], &time[i]); enq(i); } while (!empty()) { int i = deq(); if (time[i] <= q) { clock += time[i]; time[i] = 0; printf("%s %d\n", name[i], clock); } else { clock += q; time[i] -= q; enq(i); } } }
replace
11
12
11
16
0
p02264
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <string> #define N 10000 using namespace std; class Queue { private: int num[N]; int head; int tail; public: Queue() { head = 0; tail = 0; } // add to head position void push(int x) { num[tail] = x; tail++; } // delete head position void pop(void) { head++; } // return now of stck int front(void) { return num[head]; } }; int main() { Queue que; // bool finish[N]; int n, q, tim = 0; string name[N]; int time[N], i = 0; int passtime = 0; // load n and q cin >> n >> q; // load name and time for (i = 0; i < n; i++) { cin >> name[i] >> tim; que.push(tim); time[i] = 0; } // format finish[] for (int k = 0; k < n; k++) finish[k] = false; while (1) { int s; bool end = true; for (int k = 0; k < n; k++) { if (!finish[k]) { end = false; break; } } if (end) break; else end = true; if (i == n) i = 0; if (finish[i]) { i++; continue; } s = que.front(); if (s - q > 0) { que.push(s - q); que.pop(); passtime += q; time[i] = passtime; } else { que.pop(); passtime += s; time[i] = passtime; finish[i] = true; cout << name[i] << " " << time[i] << endl; } i++; } return 0; }
#include <cstdlib> #include <iostream> #include <string> #define N 100000 using namespace std; class Queue { private: int num[N]; int head; int tail; public: Queue() { head = 0; tail = 0; } // add to head position void push(int x) { num[tail] = x; tail++; } // delete head position void pop(void) { head++; } // return now of stck int front(void) { return num[head]; } }; int main() { Queue que; // bool finish[N]; int n, q, tim = 0; string name[N]; int time[N], i = 0; int passtime = 0; // load n and q cin >> n >> q; // load name and time for (i = 0; i < n; i++) { cin >> name[i] >> tim; que.push(tim); time[i] = 0; } // format finish[] for (int k = 0; k < n; k++) finish[k] = false; while (1) { int s; bool end = true; for (int k = 0; k < n; k++) { if (!finish[k]) { end = false; break; } } if (end) break; else end = true; if (i == n) i = 0; if (finish[i]) { i++; continue; } s = que.front(); if (s - q > 0) { que.push(s - q); que.pop(); passtime += q; time[i] = passtime; } else { que.pop(); passtime += s; time[i] = passtime; finish[i] = true; cout << name[i] << " " << time[i] << endl; } i++; } return 0; }
replace
4
5
4
5
0
p02264
C++
Runtime Error
#include <cstring> #include <iostream> using namespace std; int main() { int n, q, head = -1, tail, time_queue[100001] = {}, now = 0; char name_queue[100001][15]; cin >> n >> q; for (int i = 0; i < n; i++) cin >> name_queue[i] >> time_queue[i]; tail = (n - 1) % 100001; while (tail != head) { head++; if (time_queue[head] <= q) { now += time_queue[head]; cout << name_queue[head] << " " << now << endl; } else { now += q; time_queue[(tail + 1) % 100001] = time_queue[head] - q; strcpy(name_queue[(tail + 1) % 100001], name_queue[head]); tail = (tail + 1) % 100001; } } return 0; }
#include <cstring> #include <iostream> using namespace std; int main() { int n, q, head = -1, tail, time_queue[100001] = {}, now = 0; char name_queue[100001][15]; cin >> n >> q; for (int i = 0; i < n; i++) cin >> name_queue[i] >> time_queue[i]; tail = (n - 1) % 100001; while (tail != head) { head = (head + 1) % 100001; if (time_queue[head] <= q) { now += time_queue[head]; cout << name_queue[head] << " " << now << endl; } else { now += q; time_queue[(tail + 1) % 100001] = time_queue[head] - q; strcpy(name_queue[(tail + 1) % 100001], name_queue[head]); tail = (tail + 1) % 100001; } } return 0; }
replace
12
13
12
13
0
p02264
C++
Runtime Error
#include <iostream> using namespace std; int Q[100000]; int f = 0, e = 0, f2 = 0, e2 = 0; int Q2[100000]; void input_time(int a) { Q[e] = a; e = (e + 1) % 100000; } int output_time() { int a = Q[f]; f = (f + 1) % 100000; return a; } void input_name(int a) { Q2[e2] = a; e2 = (e2 + 1) % 100000; } int output_name() { int a = Q2[f2]; f2 = (f2 + 1) % 100000; return a; } char name[15][100000]; int main() { int n, q; int time, count; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> name[i] >> time; input_time(time); input_name(i); } count = 0; while (1) { if ((time = output_time()) > q) { input_time(time - q); input_name(output_name()); count += q; } else { count += time; cout << name[output_name()] << ' ' << count << endl; } if (e == f) { break; } } return 0; }
#include <iostream> using namespace std; int Q[100000]; int f = 0, e = 0, f2 = 0, e2 = 0; int Q2[100000]; void input_time(int a) { Q[e] = a; e = (e + 1) % 100000; } int output_time() { int a = Q[f]; f = (f + 1) % 100000; return a; } void input_name(int a) { Q2[e2] = a; e2 = (e2 + 1) % 100000; } int output_name() { int a = Q2[f2]; f2 = (f2 + 1) % 100000; return a; } char name[100000][15]; int main() { int n, q; int time, count; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> name[i] >> time; input_time(time); input_name(i); } count = 0; while (1) { if ((time = output_time()) > q) { input_time(time - q); input_name(output_name()); count += q; } else { count += time; cout << name[output_name()] << ' ' << count << endl; } if (e == f) { break; } } return 0; }
replace
29
30
29
30
0
p02264
C++
Runtime Error
#include <algorithm> #include <iostream> #define MAX 100005 using namespace std; struct process { int time; string name; }; class my_queue { private: int head, tail; process queue[100000]; public: my_queue() { head = 0; tail = 0; } bool isEmpty() { return head == tail; } bool isFull() { return head == (tail + 1) % MAX; } void enqueue(process n) { if (isFull()) { cout << "Queue is full." << endl; return; } queue[tail++] = n; if (tail == MAX) tail = 0; return; } process dequeue() { if (isEmpty()) { cout << "Queue is empty." << endl; exit(-1); } process ret = queue[head]; if (head + 1 == MAX) head = 0; else head++; return ret; } }; int main(void) { my_queue queue; process proc; int n, q; int time = 0; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> proc.name >> proc.time; queue.enqueue(proc); } while (!queue.isEmpty()) { proc = queue.dequeue(); if (proc.time <= q) { time += proc.time; cout << proc.name << " " << time << endl; } else { proc.time = proc.time - q; queue.enqueue(proc); time += q; } } return 0; }
#include <algorithm> #include <iostream> #define MAX 100005 using namespace std; struct process { int time; string name; }; class my_queue { private: int head, tail; process queue[MAX]; public: my_queue() { head = 0; tail = 0; } bool isEmpty() { return head == tail; } bool isFull() { return head == (tail + 1) % MAX; } void enqueue(process n) { if (isFull()) { cout << "Queue is full." << endl; return; } queue[tail++] = n; if (tail == MAX) tail = 0; return; } process dequeue() { if (isEmpty()) { cout << "Queue is empty." << endl; exit(-1); } process ret = queue[head]; if (head + 1 == MAX) head = 0; else head++; return ret; } }; int main(void) { my_queue queue; process proc; int n, q; int time = 0; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> proc.name >> proc.time; queue.enqueue(proc); } while (!queue.isEmpty()) { proc = queue.dequeue(); if (proc.time <= q) { time += proc.time; cout << proc.name << " " << time << endl; } else { proc.time = proc.time - q; queue.enqueue(proc); time += q; } } return 0; }
replace
15
16
15
16
0
p02264
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <vector> #define SIZE 200 using namespace std; struct Queue { int list[SIZE]; int head, tail; void init() { head = tail = 0; } bool isEmpty() { return head == tail; } bool isFull() { return head == (tail + 1) % SIZE; } // int head(){ return list[head]; } // int tail(){ return list[tail]; } void enqueue(int x) { if (isFull()) { cout << "ERROR(Over flow)" << endl; exit(1); } list[tail] = x; tail++; tail %= SIZE; } int dequeue() { if (isEmpty()) { cout << "ERROR(Under flow)" << endl; exit(1); } int x = list[head]; head++; head %= SIZE; return x; } }; struct Process { string n; int t; }; int main() { int n, q, idx, now = 0; vector<Process> p; Queue que; // input,initialize que.init(); cin >> n >> q; p.resize(n); for (int i = 0; i < n; i++) { cin >> p[i].n >> p[i].t; que.enqueue(i); } // process while (!que.isEmpty()) { idx = que.dequeue(); if (p[idx].t - q > 0) { p[idx].t -= q; now += q; que.enqueue(idx); } else { now += p[idx].t; p[idx].t = now; cout << p[idx].n << " " << p[idx].t << endl; } } return 0; }
#include <cstdlib> #include <iostream> #include <vector> #define SIZE 100005 using namespace std; struct Queue { int list[SIZE]; int head, tail; void init() { head = tail = 0; } bool isEmpty() { return head == tail; } bool isFull() { return head == (tail + 1) % SIZE; } // int head(){ return list[head]; } // int tail(){ return list[tail]; } void enqueue(int x) { if (isFull()) { cout << "ERROR(Over flow)" << endl; exit(1); } list[tail] = x; tail++; tail %= SIZE; } int dequeue() { if (isEmpty()) { cout << "ERROR(Under flow)" << endl; exit(1); } int x = list[head]; head++; head %= SIZE; return x; } }; struct Process { string n; int t; }; int main() { int n, q, idx, now = 0; vector<Process> p; Queue que; // input,initialize que.init(); cin >> n >> q; p.resize(n); for (int i = 0; i < n; i++) { cin >> p[i].n >> p[i].t; que.enqueue(i); } // process while (!que.isEmpty()) { idx = que.dequeue(); if (p[idx].t - q > 0) { p[idx].t -= q; now += q; que.enqueue(idx); } else { now += p[idx].t; p[idx].t = now; cout << p[idx].n << " " << p[idx].t << endl; } } return 0; }
replace
3
4
3
4
0
p02264
C++
Runtime Error
#include <iostream> #include <string> using namespace std; struct Process { string name; int time; }; int main() { int n, q; Process P[100000]; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> P[i].name >> P[i].time; } int count = 0; int origin = n; for (int i = 0; i < n; i++) { P[i].time -= q; if (P[i].time <= 0) { count += q + P[i].time; cout << P[i].name << " " << count << endl; } else { n++; P[n - 1] = P[i]; count += q; } } }
#include <iostream> #include <string> using namespace std; struct Process { string name; int time; }; int main() { int n, q; Process P[1000000]; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> P[i].name >> P[i].time; } int count = 0; int origin = n; for (int i = 0; i < n; i++) { P[i].time -= q; if (P[i].time <= 0) { count += q + P[i].time; cout << P[i].name << " " << count << endl; } else { n++; P[n - 1] = P[i]; count += q; } } }
replace
12
13
12
13
0
p02264
C++
Time Limit Exceeded
#include <iostream> #include <queue> #include <string> using namespace std; int main() { int n, q; int elapsed_time = 0; //???????????? queue<int> TIME; queue<string> NAME; cin >> n >> q; //??\??? for (int i = 0; i < n; i++) { string name; int time; cin >> name >> time; NAME.push(name); TIME.push(time); } //?????? while (!NAME.empty()) { if (TIME.front() > q) { elapsed_time += q; //?????????????????£????????????????????? string temp_name = NAME.front(); NAME.pop(); NAME.push(temp_name); int temp_time = TIME.front(); TIME.pop(); TIME.push(temp_time); } else { elapsed_time += TIME.front(); TIME.pop(); cout << NAME.front() << " " << elapsed_time << endl; NAME.pop(); } } return 0; }
#include <iostream> #include <queue> #include <string> using namespace std; int main() { int n, q; int elapsed_time = 0; //???????????? queue<int> TIME; queue<string> NAME; cin >> n >> q; //??\??? for (int i = 0; i < n; i++) { string name; int time; cin >> name >> time; NAME.push(name); TIME.push(time); } //?????? while (!NAME.empty()) { if (TIME.front() > q) { elapsed_time += q; //?????????????????£????????????????????? string temp_name = NAME.front(); NAME.pop(); NAME.push(temp_name); int temp_time = TIME.front() - q; TIME.pop(); TIME.push(temp_time); } else { elapsed_time += TIME.front(); TIME.pop(); cout << NAME.front() << " " << elapsed_time << endl; NAME.pop(); } } return 0; }
replace
32
33
32
33
TLE
p02264
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; pair<string, int> p[10000000]; pair<string, int> key; int tail; int head; void push(string str, int n) { p[tail].first = str; p[tail].second = n; tail++; } void pop() { key = p[head]; head++; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; } tail = n; int cnt = 0; int sum = 0; while (cnt != n) { pop(); string name = key.first; int time = key.second; // cout << name << " " << time << endl; if (time - q > 0) { sum += q; push(name, time - q); } else { cnt++; sum += time; cout << name << " " << sum << endl; } } }
#include <bits/stdc++.h> using namespace std; pair<string, int> p[1000000]; pair<string, int> key; int tail; int head; void push(string str, int n) { p[tail].first = str; p[tail].second = n; tail++; } void pop() { key = p[head]; head++; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; } tail = n; int cnt = 0; int sum = 0; while (cnt != n) { pop(); string name = key.first; int time = key.second; // cout << name << " " << time << endl; if (time - q > 0) { sum += q; push(name, time - q); } else { cnt++; sum += time; cout << name << " " << sum << endl; } } }
replace
3
4
3
4
MLE
p02264
C++
Runtime Error
#include <iostream> #define INT_MAX 2147483647 struct Process { std::string name; int time; }; class Queue { private: Process Table[INT_MAX]; int head; int top; public: Queue() { head = 0; top = 0; }; void Enqueue(Process P) { Table[top++] = P; }; Process Dequeue() { return Table[head++]; }; bool isEmpty() { return head == top; }; void show() { std::cout << "head : " << head << '\n'; std::cout << "top : " << top << '\n'; }; }; int main(int argc, char const *argv[]) { int n; int q; int cumtime = 0; Process P; Queue queue = Queue(); std::cin >> n >> q; for (size_t i = 0; i < n; i++) { std::cin >> P.name; std::cin >> P.time; queue.Enqueue(P); } while (!queue.isEmpty()) { P = queue.Dequeue(); P.time -= q; if (P.time > 0) { queue.Enqueue(P); cumtime += q; } else { cumtime += (q + P.time); std::cout << P.name << ' ' << cumtime << '\n'; } } return 0; }
#include <iostream> #define INT_MAX 2147483647 struct Process { std::string name; int time; }; class Queue { private: Process Table[1000000]; int head; int top; public: Queue() { head = 0; top = 0; }; void Enqueue(Process P) { Table[top++] = P; }; Process Dequeue() { return Table[head++]; }; bool isEmpty() { return head == top; }; void show() { std::cout << "head : " << head << '\n'; std::cout << "top : " << top << '\n'; }; }; int main(int argc, char const *argv[]) { int n; int q; int cumtime = 0; Process P; Queue queue = Queue(); std::cin >> n >> q; for (size_t i = 0; i < n; i++) { std::cin >> P.name; std::cin >> P.time; queue.Enqueue(P); } while (!queue.isEmpty()) { P = queue.Dequeue(); P.time -= q; if (P.time > 0) { queue.Enqueue(P); cumtime += q; } else { cumtime += (q + P.time); std::cout << P.name << ' ' << cumtime << '\n'; } } return 0; }
replace
10
11
10
11
-11
p02264
C++
Runtime Error
#include <iostream> #include <string> #include <utility> using namespace std; int main(void) { int Q[100001]; int tail = 0, head = 0; pair<string, int> p[100001]; int n, q, su = 0; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; Q[tail] = i; tail++; su += p[i].second; } int sum = 0, k; while (su != sum) { int d = Q[head]; head++; if (p[d].second > q) { p[d].second = p[d].second - q; Q[tail] = d; tail++; sum += q; } else { sum += p[d].second; cout << p[d].first << " " << sum << endl; } } return 0; }
#include <iostream> #include <string> #include <utility> using namespace std; int main(void) { int Q[1000000]; int tail = 0, head = 0; pair<string, int> p[100001]; int n, q, su = 0; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; Q[tail] = i; tail++; su += p[i].second; } int sum = 0, k; while (su != sum) { int d = Q[head]; head++; if (p[d].second > q) { p[d].second = p[d].second - q; Q[tail] = d; tail++; sum += q; } else { sum += p[d].second; cout << p[d].first << " " << sum << endl; } } return 0; }
replace
6
7
6
7
0
p02264
C++
Runtime Error
#include <iostream> #include <string> #include <vector> #define MAX 100000 using namespace std; struct pp { int t; string s; }; class queue { public: int head; int tail; vector<pp> v; queue(); void enqueue(pp x); pp dequeue(); bool isEmpty(); }; queue::queue() { head = 0; tail = 0; v.resize(MAX); } void queue::enqueue(pp x) { v[tail].t = x.t; v[tail].s = x.s; tail++; } pp queue::dequeue() { head++; return v[head - 1]; } bool queue::isEmpty() { return head == tail; } int main() { int n; cin >> n; int q; cin >> q; queue q1; for (int i = 0; i < n; i++) { pp p; cin >> p.s >> p.t; // cout << p.s << endl; q1.enqueue(p); } int passtime = 0; vector<pp> v; while (!q1.isEmpty()) { pp ptmp = q1.dequeue(); // cout << ptmp.s << endl; if (ptmp.t > q) { passtime += q; ptmp.t -= q; q1.enqueue(ptmp); } else { passtime += ptmp.t; ptmp.t = passtime; v.push_back(ptmp); } } for (int i = 0; i < v.size(); i++) { cout << v[i].s << " " << v[i].t << endl; } }
#include <iostream> #include <string> #include <vector> #define MAX 1000000 using namespace std; struct pp { int t; string s; }; class queue { public: int head; int tail; vector<pp> v; queue(); void enqueue(pp x); pp dequeue(); bool isEmpty(); }; queue::queue() { head = 0; tail = 0; v.resize(MAX); } void queue::enqueue(pp x) { v[tail].t = x.t; v[tail].s = x.s; tail++; } pp queue::dequeue() { head++; return v[head - 1]; } bool queue::isEmpty() { return head == tail; } int main() { int n; cin >> n; int q; cin >> q; queue q1; for (int i = 0; i < n; i++) { pp p; cin >> p.s >> p.t; // cout << p.s << endl; q1.enqueue(p); } int passtime = 0; vector<pp> v; while (!q1.isEmpty()) { pp ptmp = q1.dequeue(); // cout << ptmp.s << endl; if (ptmp.t > q) { passtime += q; ptmp.t -= q; q1.enqueue(ptmp); } else { passtime += ptmp.t; ptmp.t = passtime; v.push_back(ptmp); } } for (int i = 0; i < v.size(); i++) { cout << v[i].s << " " << v[i].t << endl; } }
replace
3
4
3
4
0
p02264
C++
Runtime Error
#include <iostream> using namespace std; int head, tail, n; struct task { char name[100]; int time; } t, Q[100005]; void enqueue(task x) { Q[tail] = x; tail++; } task dequeue() { task x = Q[head]; head++; return x; } int main() { int q; cin >> n >> q; for (int i = 1; i <= n; i++) cin >> Q[i].name >> Q[i].time; head = 1; tail = n + 1; int sum = 0; while (head != tail) { t = dequeue(); if (t.time > q) { sum += q; t.time -= q; enqueue(t); } else { sum += t.time; cout << t.name << ' ' << sum << endl; } } return 0; }
#include <iostream> using namespace std; int head, tail, n; struct task { char name[100]; int time; } t, Q[1000000]; void enqueue(task x) { Q[tail] = x; tail++; } task dequeue() { task x = Q[head]; head++; return x; } int main() { int q; cin >> n >> q; for (int i = 1; i <= n; i++) cin >> Q[i].name >> Q[i].time; head = 1; tail = n + 1; int sum = 0; while (head != tail) { t = dequeue(); if (t.time > q) { sum += q; t.time -= q; enqueue(t); } else { sum += t.time; cout << t.name << ' ' << sum << endl; } } return 0; }
replace
6
7
6
7
0
p02265
C++
Time Limit Exceeded
#include <cassert> #include <algorithm> #include <deque> #include <iostream> #include <string> using namespace std; #define DEBUG(x) cerr << #x << " = " << x << endl int main() { int n; cin >> n; deque<int> l; while (n--) { string s; cin >> s; if (s == "insert") { int x; cin >> x; l.push_front(x); } else if (s == "delete") { int x; cin >> x; auto it = find(l.begin(), l.end(), x); if (it != l.end()) { l.erase(it); } } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } else { assert(false); } } int k = 0; for (int item : l) { cout << item << " \n"[k++ == l.size() - 1]; } }
#include <cassert> #include <algorithm> #include <deque> #include <iostream> #include <string> using namespace std; #define DEBUG(x) cerr << #x << " = " << x << endl int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; deque<int> l; while (n--) { string s; cin >> s; if (s == "insert") { int x; cin >> x; l.push_front(x); } else if (s == "delete") { int x; cin >> x; auto it = find(l.begin(), l.end(), x); if (it != l.end()) { l.erase(it); } } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } else { assert(false); } } int k = 0; for (int item : l) { cout << item << " \n"[k++ == l.size() - 1]; } }
insert
12
12
12
15
TLE
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++) using namespace std; typedef long long ll; int main(void) { ll N; cin >> N; deque<ll> q; REP(i, 0, N) { string C; ll X; cin >> C; if (C == "insert" || C == "delete") cin >> X; if (C == "insert") { q.push_front(X); } if (C == "delete") { deque<ll> _q; bool f = false; while (q.size()) { if (q.front() != X || f) { _q.push_back(q.front()); } else if (!f) { f = true; } q.pop_front(); } swap(q, _q); } if (C == "deleteFirst") { q.pop_front(); } if (C == "deleteLast") { q.pop_back(); } } while (q.size()) { cout << q.front() << (q.size() != 1 ? " " : "\n"); q.pop_front(); } }
#include <bits/stdc++.h> #define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++) using namespace std; typedef long long ll; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; deque<ll> q; REP(i, 0, N) { string C; ll X; cin >> C; if (C == "insert" || C == "delete") cin >> X; if (C == "insert") { q.push_front(X); } if (C == "delete") { deque<ll> _q; bool f = false; while (q.size()) { if (q.front() != X || f) { _q.push_back(q.front()); } else if (!f) { f = true; } q.pop_front(); } swap(q, _q); } if (C == "deleteFirst") { q.pop_front(); } if (C == "deleteLast") { q.pop_back(); } } while (q.size()) { cout << q.front() << (q.size() != 1 ? " " : "\n"); q.pop_front(); } }
insert
6
6
6
9
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <list> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, a; string str; list<int> l; cin >> n; for (int i = 0; i < n; i++) { cin >> str; if (str == "insert") { cin >> a; l.push_front(a); } else if (str == "delete") { cin >> a; auto itr = l.begin(); for (; *itr != a; ++itr) ; l.erase(itr); } else if (str == "deleteFirst") { l.pop_front(); } else if (str == "deleteLast") { l.pop_back(); } } auto itr = l.begin(); cout << *itr; ++itr; for (; itr != l.end(); ++itr) { cout << " " << *itr; } cout << endl; return 0; }
#include <algorithm> #include <iostream> #include <list> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, a; string str; list<int> l; cin >> n; for (int i = 0; i < n; i++) { cin >> str; if (str == "insert") { cin >> a; l.push_front(a); } else if (str == "delete") { cin >> a; auto itr = l.begin(); for (; itr != l.end(); ++itr) if (*itr == a) { l.erase(itr); break; } } else if (str == "deleteFirst") { l.pop_front(); } else if (str == "deleteLast") { l.pop_back(); } } auto itr = l.begin(); cout << *itr; ++itr; for (; itr != l.end(); ++itr) { cout << " " << *itr; } cout << endl; return 0; }
replace
23
26
23
28
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <iterator> #include <list> #include <string> using namespace std; int main() { // const string // str_insert = "insert", // str_delete = "delete", // str_delete_first = "deleteFirst", // str_delete_last = "deleteLast"; int N; cin >> N; list<int> l; // typedef enum{INS, DEL, DEL_F, DEL_L} // command; for (int i = 0; i < N; ++i) { string s; cin >> s; // command c; if (s[0] == 'i') { int n; cin >> n; l.push_front(n); // break; } else if (s.size() == 6) { int n; cin >> n; auto itr = find(l.begin(), l.end(), n); if (itr != l.end()) { l.erase(itr); } // break; } else if (s[6] == 'F') { l.pop_front(); // break; } else if (s[6] == 'L') { l.pop_back(); // break; } // cout << *(l.begin()); // for(std::list<int>::iterator itr = next(l.begin()); itr!=l.end(); ++itr){ // cout << ' ' << *itr; // } // cout << endl; } cout << *(l.begin()); for (std::list<int>::iterator itr = next(l.begin()); itr != l.end(); ++itr) { cout << ' ' << *itr; } cout << endl; return 0; }
#include <algorithm> #include <iostream> #include <iterator> #include <list> #include <string> using namespace std; int main() { ios::sync_with_stdio(false); // const string // str_insert = "insert", // str_delete = "delete", // str_delete_first = "deleteFirst", // str_delete_last = "deleteLast"; int N; cin >> N; list<int> l; // typedef enum{INS, DEL, DEL_F, DEL_L} // command; for (int i = 0; i < N; ++i) { string s; cin >> s; // command c; if (s[0] == 'i') { int n; cin >> n; l.push_front(n); // break; } else if (s.size() == 6) { int n; cin >> n; auto itr = find(l.begin(), l.end(), n); if (itr != l.end()) { l.erase(itr); } // break; } else if (s[6] == 'F') { l.pop_front(); // break; } else if (s[6] == 'L') { l.pop_back(); // break; } // cout << *(l.begin()); // for(std::list<int>::iterator itr = next(l.begin()); itr!=l.end(); ++itr){ // cout << ' ' << *itr; // } // cout << endl; } cout << *(l.begin()); for (std::list<int>::iterator itr = next(l.begin()); itr != l.end(); ++itr) { cout << ' ' << *itr; } cout << endl; return 0; }
insert
9
9
9
10
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> using namespace std; struct Node { int data; Node *next, *prev; }; class List { Node *nil; Node *searchNode(int x) { Node *p = nil->next; while (p != nil && p->data != x) { p = p->next; } return p; } void deleteNode(Node *node) { if (node == nil) return; node->prev->next = node->next; node->next->prev = node->prev; delete (node); } public: List() { nil = new Node(); nil->next = nil; nil->prev = nil; } void insertFirst(int x) { Node *node = new Node(); node->data = x; node->next = nil->next; node->prev = nil; nil->next->prev = node; nil->next = node; } void deleteData(int x) { deleteNode(searchNode(x)); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } void print() { Node *p = nil->next; while (p != nil) { if (p != nil->next) cout << " "; cout << p->data; p = p->next; } cout << endl; } }; int main() { List l; int n; cin >> n; for (int i = 0; i < n; ++i) { int num; string command; cin >> command; if (command == "insert") { cin >> num; l.insertFirst(num); } else if (command == "delete") { cin >> num; l.deleteData(num); } else if (command == "deleteFirst") { l.deleteFirst(); } else if (command == "deleteLast") { l.deleteLast(); } } l.print(); return 0; }
#include <iostream> using namespace std; struct Node { int data; Node *next, *prev; }; class List { Node *nil; Node *searchNode(int x) { Node *p = nil->next; while (p != nil && p->data != x) { p = p->next; } return p; } void deleteNode(Node *node) { if (node == nil) return; node->prev->next = node->next; node->next->prev = node->prev; delete (node); } public: List() { nil = new Node(); nil->next = nil; nil->prev = nil; } void insertFirst(int x) { Node *node = new Node(); node->data = x; node->next = nil->next; node->prev = nil; nil->next->prev = node; nil->next = node; } void deleteData(int x) { deleteNode(searchNode(x)); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } void print() { Node *p = nil->next; while (p != nil) { if (p != nil->next) cout << " "; cout << p->data; p = p->next; } cout << endl; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); List l; int n; cin >> n; for (int i = 0; i < n; ++i) { int num; string command; cin >> command; if (command == "insert") { cin >> num; l.insertFirst(num); } else if (command == "delete") { cin >> num; l.deleteData(num); } else if (command == "deleteFirst") { l.deleteFirst(); } else if (command == "deleteLast") { l.deleteLast(); } } l.print(); return 0; }
insert
57
57
57
60
TLE
p02265
C++
Runtime Error
#include <iostream> #include <list> #include <sstream> #include <string> using namespace std; list<int>::iterator find_key(list<int> &li, int key) { list<int>::iterator li_itr = li.begin(); while (li_itr != li.end()) { if (*li_itr == key) { return li_itr; } ++li_itr; } return li.end(); } int main(void) { ios::sync_with_stdio(false); int n; list<int> li; cin >> n; for (int i = 0; i < n; i++) { string command; cin >> command; int key; if (command == "insert") { cin >> key; li.push_front(key); } else if (command == "deleteFirst") { li.pop_front(); } else if (command == "deleteLast") { li.pop_back(); } else { cin >> key; li.erase(find_key(li, key)); } } list<int>::iterator itr = li.begin(); while (itr != li.end()) { cout << *itr; ++itr; itr == li.end() ? cout << "" : cout << " "; } cout << endl; }
#include <iostream> #include <list> #include <sstream> #include <string> using namespace std; list<int>::iterator find_key(list<int> &li, int key) { list<int>::iterator li_itr = li.begin(); while (li_itr != li.end()) { if (*li_itr == key) { return li_itr; } ++li_itr; } return li.end(); } int main(void) { ios::sync_with_stdio(false); int n; list<int> li; cin >> n; for (int i = 0; i < n; i++) { string command; cin >> command; int key; if (command == "insert") { cin >> key; li.push_front(key); } else if (command == "deleteFirst") { li.pop_front(); } else if (command == "deleteLast") { li.pop_back(); } else { cin >> key; list<int>::iterator erace_itr = find_key(li, key); if (erace_itr != li.end()) { li.erase(find_key(li, key)); } } } list<int>::iterator itr = li.begin(); while (itr != li.end()) { cout << *itr; ++itr; itr == li.end() ? cout << "" : cout << " "; } cout << endl; }
replace
38
39
38
42
0
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #define REP(i, a, b) for (i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) using namespace std; int main() { list<int> data; list<int>::iterator it; int key, n, i; string command; cin >> n; rep(i, n) { cin >> command; if (command == "insert") { cin >> key; data.push_front(key); } else if (command == "delete") { cin >> key; it = data.begin(); while (it != data.end()) { if (*it == key) { data.erase(it); break; } it++; } } else if (command == "deleteFirst") { data.pop_front(); } else if (command == "deleteLast") { data.pop_back(); } } it = data.begin(); while (it != data.end()) { cout << *it; it++; if (it != data.end()) cout << " "; } cout << endl; return 0; }
#include <iostream> #include <list> #define REP(i, a, b) for (i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) using namespace std; int main() { std::cin.tie(0); // tie std::ios::sync_with_stdio(false); // sync list<int> data; list<int>::iterator it; int key, n, i; string command; cin >> n; rep(i, n) { cin >> command; if (command == "insert") { cin >> key; data.push_front(key); } else if (command == "delete") { cin >> key; it = data.begin(); while (it != data.end()) { if (*it == key) { data.erase(it); break; } it++; } } else if (command == "deleteFirst") { data.pop_front(); } else if (command == "deleteLast") { data.pop_back(); } } it = data.begin(); while (it != data.end()) { cout << *it; it++; if (it != data.end()) cout << " "; } cout << endl; return 0; }
insert
9
9
9
12
TLE
p02265
C++
Runtime Error
#include <iostream> #include <string> using namespace std; class Node { private: int data; Node *next; Node *prev; public: Node() { next = NULL; prev = NULL; } void setData(int data) { this->data = data; } void setNext(Node *next) { this->next = next; } void setPrev(Node *prev) { this->prev = prev; } int getData() { return data; } Node *getNext() { return next; } Node *getPrev() { return prev; } }; class List { private: Node *head; Node *tail; public: List() { head = new Node; tail = new Node; head->setPrev(tail); // headの後ろがtail tail->setNext(head); } void insert(int x) { Node *newNode; newNode = new Node; newNode->setData(x); newNode->setNext(head); newNode->setPrev(head->getPrev()); head->getPrev()->setNext(newNode); head->setPrev(newNode); } void deleteNode(int x) { tail->setData(x); Node *i = head->getPrev(); while (i->getData() != x) i = i->getPrev(); if (i != tail) { i->getNext()->setPrev(i->getPrev()); i->getPrev()->setNext(i->getNext()); delete i; } } void deleteFirst() { Node *tmp = head->getPrev(); tmp->getPrev()->setNext(head); head->setPrev(tmp->getPrev()); delete tmp; } void deleteLast() { Node *tmp = tail->getNext(); tmp->getNext()->setPrev(tail); tail->setPrev(tmp->getNext()); delete tmp; } void showList() { Node *i = head->getPrev()->getPrev(); cout << head->getPrev()->getData(); while (i != tail) { cout << " " << i->getData(); i = i->getPrev(); } cout << endl; } ~List() { Node *tmp = head->getPrev(); while (tmp != tail) { delete tmp->getNext(); tmp = tmp->getPrev(); } delete tmp; } }; int main() { List list; unsigned n; string command; // コマンド int x; // 値 cin >> n; for (int i = 0; i < n; i++) { cin >> command; if (command == "deleteFirst") { list.deleteFirst(); } else if (command == "deleteLast") { list.deleteLast(); } else if (command == "insert") { cin >> x; list.insert(x); } else if (command == "delete") { cin >> x; list.deleteNode(x); } else { cout << "Unknown command" << endl; } } list.showList(); return 0; }
#include <iostream> #include <string> using namespace std; class Node { private: int data; Node *next; Node *prev; public: Node() { next = NULL; prev = NULL; } void setData(int data) { this->data = data; } void setNext(Node *next) { this->next = next; } void setPrev(Node *prev) { this->prev = prev; } int getData() { return data; } Node *getNext() { return next; } Node *getPrev() { return prev; } }; class List { private: Node *head; Node *tail; public: List() { head = new Node; tail = new Node; head->setPrev(tail); // headの後ろがtail tail->setNext(head); } void insert(int x) { Node *newNode; newNode = new Node; newNode->setData(x); newNode->setNext(head); newNode->setPrev(head->getPrev()); head->getPrev()->setNext(newNode); head->setPrev(newNode); } void deleteNode(int x) { tail->setData(x); Node *i = head->getPrev(); while (i->getData() != x) i = i->getPrev(); if (i != tail) { i->getNext()->setPrev(i->getPrev()); i->getPrev()->setNext(i->getNext()); delete i; } } void deleteFirst() { Node *tmp = head->getPrev(); tmp->getPrev()->setNext(head); head->setPrev(tmp->getPrev()); delete tmp; } void deleteLast() { Node *tmp = tail->getNext(); tmp->getNext()->setPrev(tail); tail->setNext(tmp->getNext()); delete tmp; } void showList() { Node *i = head->getPrev()->getPrev(); cout << head->getPrev()->getData(); while (i != tail) { cout << " " << i->getData(); i = i->getPrev(); } cout << endl; } ~List() { Node *tmp = head->getPrev(); while (tmp != tail) { delete tmp->getNext(); tmp = tmp->getPrev(); } delete tmp; } }; int main() { List list; unsigned n; string command; // コマンド int x; // 値 cin >> n; for (int i = 0; i < n; i++) { cin >> command; if (command == "deleteFirst") { list.deleteFirst(); } else if (command == "deleteLast") { list.deleteLast(); } else if (command == "insert") { cin >> x; list.insert(x); } else if (command == "delete") { cin >> x; list.deleteNode(x); } else { cout << "Unknown command" << endl; } } list.showList(); return 0; }
replace
65
66
65
66
0
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; Node *search(int k) { Node *x = nil->next; while (x != nil && x->key != k) x = x->next; return x; } void insert(int k) { Node *x = (Node *)malloc(sizeof(Node)); x->key = k; x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } void deleteNode(Node *x) { x->prev->next = x->next; x->next->prev = x->prev; free(x); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } int main() { int n, k; char command[20]; nil = (Node *)malloc(sizeof(Node)); nil->prev = nil; nil->next = nil; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", command); if (strcmp(command, "insert") == 0) { scanf("%d", &k); insert(k); } else if (strcmp(command, "delete") == 0) { scanf("%d", &k); deleteNode(search(k)); } else if (strcmp(command, "deleteFirst") == 0) { deleteFirst(); } else if (strcmp(command, "deleteLast") == 0) { deleteLast(); } } k = 0; Node *x = nil->next; while (x != nil) { printf((k ? " " : "")); printf("%d", x->key); k++; x = x->next; } printf("\n"); return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; Node *search(int k) { Node *x = nil->next; while (x != nil && x->key != k) x = x->next; return x; } void insert(int k) { Node *x = (Node *)malloc(sizeof(Node)); x->key = k; x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } void deleteNode(Node *x) { if (x == nil) return; x->prev->next = x->next; x->next->prev = x->prev; free(x); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } int main() { int n, k; char command[20]; nil = (Node *)malloc(sizeof(Node)); nil->prev = nil; nil->next = nil; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", command); if (strcmp(command, "insert") == 0) { scanf("%d", &k); insert(k); } else if (strcmp(command, "delete") == 0) { scanf("%d", &k); deleteNode(search(k)); } else if (strcmp(command, "deleteFirst") == 0) { deleteFirst(); } else if (strcmp(command, "deleteLast") == 0) { deleteLast(); } } k = 0; Node *x = nil->next; while (x != nil) { printf((k ? " " : "")); printf("%d", x->key); k++; x = x->next; } printf("\n"); return 0; }
insert
35
35
35
37
TLE
p02265
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <forward_list> #include <functional> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <set> #include <sstream> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // use printf, scanf instead of cout, cin. using namespace std; template <typename T> struct Node { Node(const T &value) : mValue(value), mNext(nullptr), mPrev(nullptr) {} T mValue; Node<T> *mNext; Node<T> *mPrev; }; template <typename T> class List { private: Node<T> *mFront; Node<T> *mBack; public: List() : mFront(nullptr), mBack(nullptr) {} ~List() { while (mFront) popFront(); } void pushFront(const T &val) { auto node(new Node<T>(val)); if (mFront) { node->mNext = mFront; mFront->mPrev = node; mFront = node; } else { mFront = node; mBack = node; } } void pushBack(const T &val) { auto node(new Node<T>(val)); if (mBack) { node->mPrev = mBack; mBack->mNext = node; mBack = node; } else { mFront = node; mBack = node; } } void popFront() { if (mFront) { auto next(mFront->mNext); delete mFront; if (next) { next->mPrev = nullptr; mFront = next; } else { mFront = mBack = nullptr; } } } void popBack() { if (mBack) { auto prev(mBack->mPrev); delete mBack; if (prev) { prev->mNext = nullptr; mBack = prev; } else { mFront = mBack = nullptr; } } } void erase(const T &val) { auto node(mFront); while (node) { if (node->mValue == val) { if (node->mPrev) node->mPrev->mNext = node->mNext; else mFront = node->mNext; if (node->mNext) node->mNext->mPrev = node->mPrev; else mBack = node->mPrev; delete node; break; } node = node->mNext; } } void print() { auto node(mFront); size_t i(0); while (node) { printf(i++ ? " %d" : "%d", node->mValue); node = node->mNext; } printf("\n"); } }; int main() { List<int> list; int n; scanf("%d", &n); for (size_t i(0); i < n; ++i) { char command[16]; scanf("%s", command); if (!strcmp(command, "insert")) { int x; scanf("%d", &x); list.pushFront(x); } else if (!strcmp(command, "delete")) { int x; scanf("%d", &x); list.erase(x); } else if (!strcmp(command, "deleteFirst")) { list.popFront(); } else if (!strcmp(command, "deleteLast")) { list.popBack(); } } list.print(); while (true) { } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <forward_list> #include <functional> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <set> #include <sstream> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // use printf, scanf instead of cout, cin. using namespace std; template <typename T> struct Node { Node(const T &value) : mValue(value), mNext(nullptr), mPrev(nullptr) {} T mValue; Node<T> *mNext; Node<T> *mPrev; }; template <typename T> class List { private: Node<T> *mFront; Node<T> *mBack; public: List() : mFront(nullptr), mBack(nullptr) {} ~List() { while (mFront) popFront(); } void pushFront(const T &val) { auto node(new Node<T>(val)); if (mFront) { node->mNext = mFront; mFront->mPrev = node; mFront = node; } else { mFront = node; mBack = node; } } void pushBack(const T &val) { auto node(new Node<T>(val)); if (mBack) { node->mPrev = mBack; mBack->mNext = node; mBack = node; } else { mFront = node; mBack = node; } } void popFront() { if (mFront) { auto next(mFront->mNext); delete mFront; if (next) { next->mPrev = nullptr; mFront = next; } else { mFront = mBack = nullptr; } } } void popBack() { if (mBack) { auto prev(mBack->mPrev); delete mBack; if (prev) { prev->mNext = nullptr; mBack = prev; } else { mFront = mBack = nullptr; } } } void erase(const T &val) { auto node(mFront); while (node) { if (node->mValue == val) { if (node->mPrev) node->mPrev->mNext = node->mNext; else mFront = node->mNext; if (node->mNext) node->mNext->mPrev = node->mPrev; else mBack = node->mPrev; delete node; break; } node = node->mNext; } } void print() { auto node(mFront); size_t i(0); while (node) { printf(i++ ? " %d" : "%d", node->mValue); node = node->mNext; } printf("\n"); } }; int main() { List<int> list; int n; scanf("%d", &n); for (size_t i(0); i < n; ++i) { char command[16]; scanf("%s", command); if (!strcmp(command, "insert")) { int x; scanf("%d", &x); list.pushFront(x); } else if (!strcmp(command, "delete")) { int x; scanf("%d", &x); list.erase(x); } else if (!strcmp(command, "deleteFirst")) { list.popFront(); } else if (!strcmp(command, "deleteLast")) { list.popBack(); } } list.print(); return 0; }
delete
171
174
171
171
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <stdlib.h> #include <string> using namespace std; class node { public: int data; node *prev; node *next; node() { prev = 0; next = 0; data = 0; } node(int data) { prev = 0; next = 0; this->data = data; } node(int data, node *next) { prev = 0; this->next = next; this->data = data; } friend class double_linked_list; }; class double_linked_list { public: node *first; node *last; double_linked_list() { first = 0; last = 0; } void insert_value(int data); void delete_value(int data); void delete_first(); void delete_last(); }; void double_linked_list::insert_value(int data) { if (first == 0) { node *new_first = new node(data); first = new_first; last = new_first; } else { node *new_first = new node(data, first); first->prev = new_first; first = new_first; } } void double_linked_list::delete_value(int data) { node *current_node = first; // no data if (current_node == 0) { return; } // first if (current_node->data == data) { this->delete_first(); return; } current_node = current_node->next; while (current_node != 0) { if (current_node->data == data) { if (current_node->next != 0) { current_node->prev->next = current_node->next; current_node->next->prev = current_node->prev; } else { // last current_node->prev->next = 0; last = current_node->prev; } return; } current_node = current_node->next; } } void double_linked_list::delete_first() { node *current_node = first; // no data if (current_node == 0) { return; } if (current_node->next != 0) { current_node->next->prev = 0; first = current_node->next; return; } first = 0; last = 0; return; } void double_linked_list::delete_last() { if (this->last == 0) { return; } if (last->prev != 0) { last->prev->next = 0; last = last->prev; } else { last = 0; first = 0; } } int main() { int num; cin >> num; string command; int value; double_linked_list *result_list = new double_linked_list(); for (int i = 0; i < num; ++i) { cin >> command; if (command[0] == 'i') { cin >> value; result_list->insert_value(value); } else { if (command[6] == '\0') { cin >> value; result_list->delete_value(value); } else if (command[6] == 'F') { result_list->delete_first(); } else if (command[6] == 'L') { result_list->delete_last(); } } } node *test = result_list->first; while (test != 0) { cout << test->data; if (test->next != 0) { cout << " "; } test = test->next; } cout << endl; return 0; }
#include <iostream> #include <stdlib.h> #include <string> using namespace std; class node { public: int data; node *prev; node *next; node() { prev = 0; next = 0; data = 0; } node(int data) { prev = 0; next = 0; this->data = data; } node(int data, node *next) { prev = 0; this->next = next; this->data = data; } friend class double_linked_list; }; class double_linked_list { public: node *first; node *last; double_linked_list() { first = 0; last = 0; } void insert_value(int data); void delete_value(int data); void delete_first(); void delete_last(); }; void double_linked_list::insert_value(int data) { if (first == 0) { node *new_first = new node(data); first = new_first; last = new_first; } else { node *new_first = new node(data, first); first->prev = new_first; first = new_first; } } void double_linked_list::delete_value(int data) { node *current_node = first; // no data if (current_node == 0) { return; } // first if (current_node->data == data) { this->delete_first(); return; } current_node = current_node->next; while (current_node != 0) { if (current_node->data == data) { if (current_node->next != 0) { current_node->prev->next = current_node->next; current_node->next->prev = current_node->prev; } else { // last current_node->prev->next = 0; last = current_node->prev; } return; } current_node = current_node->next; } } void double_linked_list::delete_first() { node *current_node = first; // no data if (current_node == 0) { return; } if (current_node->next != 0) { current_node->next->prev = 0; first = current_node->next; return; } first = 0; last = 0; return; } void double_linked_list::delete_last() { if (this->last == 0) { return; } if (last->prev != 0) { last->prev->next = 0; last = last->prev; } else { last = 0; first = 0; } } int main() { int num; cin >> num; char command[12]; int value; double_linked_list *result_list = new double_linked_list(); for (int i = 0; i < num; ++i) { cin >> command; if (command[0] == 'i') { cin >> value; result_list->insert_value(value); } else { if (command[6] == '\0') { cin >> value; result_list->delete_value(value); } else if (command[6] == 'F') { result_list->delete_first(); } else if (command[6] == 'L') { result_list->delete_last(); } } } node *test = result_list->first; while (test != 0) { cout << test->data; if (test->next != 0) { cout << " "; } test = test->next; } cout << endl; return 0; }
replace
112
113
112
113
TLE
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb push_back #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcount #define INF INT_MAX / 3 struct node { node *nxt; node *prv; int val; }; int idx = 0; node resorce[2000000]; int main() { node *start = &resorce[idx++]; node *end = &resorce[idx++]; start->nxt = end; start->prv = NULL; start->val = -1; end->prv = start; end->nxt = NULL; end->val = -1; int q; cin >> q; while (q--) { string com; int x; cin >> com; if (com == "insert") { cin >> x; node *crt = &resorce[idx++]; crt->val = x; crt->nxt = start->nxt; crt->prv = start; start->nxt = crt; crt->nxt->prv = crt; } else if (com == "delete") { cin >> x; node *crt = start; while (crt != end && crt->val != x) crt = crt->nxt; if (crt == end) continue; node *prev = crt->prv; node *next = crt->nxt; prev->nxt = next; next->prv = prev; } else if (com == "deleteFirst") { node *crt = start->nxt; start->nxt = crt->nxt; crt->nxt->prv = start; } else if (com == "deleteLast") { node *crt = end->prv; end->prv = crt->prv; crt->prv->nxt = end; } } vector<ll> res; node *crt = start->nxt; while (crt != end) { res.push_back(crt->val); crt = crt->nxt; } cout << res[0]; repl(i, 1, res.size()) { cout << " " << res[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb push_back #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcount #define INF INT_MAX / 3 struct node { node *nxt; node *prv; int val; }; int idx = 0; node resorce[2000000]; int main() { cin.tie(0); ios::sync_with_stdio(false); node *start = &resorce[idx++]; node *end = &resorce[idx++]; start->nxt = end; start->prv = NULL; start->val = -1; end->prv = start; end->nxt = NULL; end->val = -1; int q; cin >> q; while (q--) { string com; int x; cin >> com; if (com == "insert") { cin >> x; node *crt = &resorce[idx++]; crt->val = x; crt->nxt = start->nxt; crt->prv = start; start->nxt = crt; crt->nxt->prv = crt; } else if (com == "delete") { cin >> x; node *crt = start; while (crt != end && crt->val != x) crt = crt->nxt; if (crt == end) continue; node *prev = crt->prv; node *next = crt->nxt; prev->nxt = next; next->prv = prev; } else if (com == "deleteFirst") { node *crt = start->nxt; start->nxt = crt->nxt; crt->nxt->prv = start; } else if (com == "deleteLast") { node *crt = end->prv; end->prv = crt->prv; crt->prv->nxt = end; } } vector<ll> res; node *crt = start->nxt; while (crt != end) { res.push_back(crt->val); crt = crt->nxt; } cout << res[0]; repl(i, 1, res.size()) { cout << " " << res[i]; } cout << endl; return 0; }
insert
35
35
35
38
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <list> using namespace std; int main() { int n, k; char s[15]; list<int> l; scanf("%d", &n); while (n--) { scanf("%s", s); if (*s == 'i') { scanf("%d", &k); l.push_front(k); continue; } switch (*(s + 6)) { case 'F': l.pop_front(); break; case 'L': l.pop_back(); break; default: scanf("%d", &k); l.erase(find(l.begin(), l.end(), k)); } } int &f = l.front(); for (int &x : l) printf("%s%d", (&f == &x ? "" : " "), x); puts(""); }
#include <algorithm> #include <cstdio> #include <list> using namespace std; int main() { int n, k; char s[15]; list<int> l; scanf("%d", &n); while (n--) { scanf("%s", s); if (*s == 'i') { scanf("%d", &k); l.push_front(k); continue; } switch (*(s + 6)) { case 'F': l.pop_front(); break; case 'L': l.pop_back(); break; default: scanf("%d", &k); auto r = find(l.begin(), l.end(), k); if (r != l.end()) l.erase(r); } } int &f = l.front(); for (int &x : l) printf("%s%d", (&f == &x ? "" : " "), x); puts(""); }
replace
28
29
28
31
0
p02265
C++
Time Limit Exceeded
#include <iostream> #include <queue> #include <string> using namespace std; deque<int> dll; void printList() { deque<int>::iterator itr = dll.begin(); deque<int>::iterator itrE = dll.end(); for (; itr != itrE; itr++) { cout << *itr; if (itr != itrE - 1) cout << ' '; } cout << endl; } int main() { int n; cin >> n; string cmd; int key; for (int i = 0; i < n; i++) { cin >> cmd; if ("insert" == cmd) { cin >> key; dll.push_front(key); } else if ("deleteFirst" == cmd) { dll.pop_front(); } else if ("deleteLast" == cmd) { dll.pop_back(); } else if ("delete" == cmd) { cin >> key; deque<int>::iterator itr = dll.begin(); deque<int>::iterator itrE = dll.end(); for (; itr != itrE; itr++) { if (key == *itr) { dll.erase(itr); break; } } } } printList(); return 0; }
#include <iostream> #include <queue> #include <string> using namespace std; deque<int> dll; void printList() { deque<int>::iterator itr = dll.begin(); deque<int>::iterator itrE = dll.end(); for (; itr != itrE; itr++) { cout << *itr; if (itr != itrE - 1) cout << ' '; } cout << endl; } int main() { int n; ios::sync_with_stdio(false); cin >> n; string cmd; int key; for (int i = 0; i < n; i++) { cin >> cmd; if ("insert" == cmd) { cin >> key; dll.push_front(key); } else if ("deleteFirst" == cmd) { dll.pop_front(); } else if ("deleteLast" == cmd) { dll.pop_back(); } else if ("delete" == cmd) { cin >> key; deque<int>::iterator itr = dll.begin(); deque<int>::iterator itrE = dll.end(); for (; itr != itrE; itr++) { if (key == *itr) { dll.erase(itr); break; } } } } printList(); return 0; }
insert
21
21
21
22
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> // require sort next_permutation count __gcd reverse etc. #include <cctype> // require tolower, toupper #include <cfloat> #include <climits> #include <cmath> // require fabs #include <cstdio> // require scanf printf #include <cstdlib> // require abs exit atof atoi #include <cstring> // require memset #include <ctime> // require srand #include <deque> #include <fstream> // require freopen #include <functional> #include <iomanip> // require setw #include <iostream> #include <limits> #include <map> #include <numeric> // require accumulate #include <queue> #include <set> #include <sstream> // require stringstream #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(A) A.begin(), A.end() using namespace std; typedef long long ll; typedef pair<int, int> P; const string com[4] = {"insert", "delete", "deleteFirst", "deleteLast"}; int main() { deque<int> deq; int n; cin >> n; rep(i, n) { string op = ""; cin >> op; rep(j, 4) { if (op == com[j]) { if (j == 0 || j == 1) { int x; cin >> x; if (j == 0) deq.push_front(x); else { deque<int>::iterator it = deq.begin(); for (; it != deq.end(); it++) { int curr = (*it); if (curr == x) { deq.erase(it); break; } // end if } // end for } // end if } else if (j == 2) { deq.pop_front(); } else { // if (j == 3 ) deq.pop_back(); } // end if } // end if } // end rep } // end rep rep(i, deq.size()) { cout << deq[i] << (i != deq.size() - 1 ? ' ' : '\n'); } // end rep return 0; }
#include <algorithm> // require sort next_permutation count __gcd reverse etc. #include <cctype> // require tolower, toupper #include <cfloat> #include <climits> #include <cmath> // require fabs #include <cstdio> // require scanf printf #include <cstdlib> // require abs exit atof atoi #include <cstring> // require memset #include <ctime> // require srand #include <deque> #include <fstream> // require freopen #include <functional> #include <iomanip> // require setw #include <iostream> #include <limits> #include <map> #include <numeric> // require accumulate #include <queue> #include <set> #include <sstream> // require stringstream #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(A) A.begin(), A.end() using namespace std; typedef long long ll; typedef pair<int, int> P; const string com[4] = {"insert", "delete", "deleteFirst", "deleteLast"}; int main() { ios_base::sync_with_stdio(0); deque<int> deq; int n; cin >> n; rep(i, n) { string op = ""; cin >> op; rep(j, 4) { if (op == com[j]) { if (j == 0 || j == 1) { int x; cin >> x; if (j == 0) deq.push_front(x); else { deque<int>::iterator it = deq.begin(); for (; it != deq.end(); it++) { int curr = (*it); if (curr == x) { deq.erase(it); break; } // end if } // end for } // end if } else if (j == 2) { deq.pop_front(); } else { // if (j == 3 ) deq.pop_back(); } // end if } // end if } // end rep } // end rep rep(i, deq.size()) { cout << deq[i] << (i != deq.size() - 1 ? ' ' : '\n'); } // end rep return 0; }
insert
34
34
34
35
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; int main() { int n; cin >> n; list<int> L; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == "insert") { int v; scanf("%d", &v); L.push_front(v); } else if (s == "delete") { int v; scanf("%d", &v); for (auto itr = L.begin(); itr != L.end(); ++itr) { if (*itr == v) { L.erase(itr); break; } } } else if (s == "deleteFirst") { L.pop_front(); } else { L.pop_back(); } } for (auto itr = L.begin(); itr != L.end(); ++itr) { if (itr != L.begin()) { cout << " " << *itr; } else { cout << *itr; } } cout << endl; return 0; }
#include <iostream> #include <list> #include <string> using namespace std; int main() { int n; scanf("%d", &n); list<int> L; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == "insert") { int v; scanf("%d", &v); L.push_front(v); } else if (s == "delete") { int v; scanf("%d", &v); for (auto itr = L.begin(); itr != L.end(); ++itr) { if (*itr == v) { L.erase(itr); break; } } } else if (s == "deleteFirst") { L.pop_front(); } else { L.pop_back(); } } for (auto itr = L.begin(); itr != L.end(); ++itr) { if (itr != L.begin()) { cout << " " << *itr; } else { cout << *itr; } } cout << endl; return 0; }
replace
9
10
9
10
TLE
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <cstring> struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *cur = (Node *)malloc(sizeof(Node)); cur->key = key; cur->next = nil->next; cur->prev = nil; nil->next->prev = cur; nil->next = cur; } void deleteNode(Node *cur) { cur->next->prev = cur->prev; cur->prev->next = cur->next; free(cur); } Node *listSearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) { cur = cur->next; } return cur; } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } void deleteKey(int key) { Node *cur = listSearch(key); deleteNode(cur); } void printList() { Node *ptr = nil->next; // do{ // if (head == ptr) // printf("%d", ptr->x); // else // printf(" %d", ptr->x); // ptr = ptr->next; // }while(ptr != head); int flag = 1; while (ptr != nil) { if (flag) { printf("%d", ptr->key); flag = 0; } else { printf(" %d", ptr->key); } ptr = ptr->next; } putchar('\n'); } int main() { init(); int n, key; scanf("%d", &n); for (int i = 0; i < n; i++) { char s[10]; scanf("%s", s); if (strcmp(s, "insert") == 0) { scanf("%d", &key); insert(key); } if (strcmp(s, "delete") == 0) { scanf("%d", &key); deleteKey(key); } if (strcmp(s, "deleteFirst") == 0) { deleteFirst(); } if (strcmp(s, "deleteLast") == 0) { deleteLast(); } } printList(); return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *cur = (Node *)malloc(sizeof(Node)); cur->key = key; cur->next = nil->next; cur->prev = nil; nil->next->prev = cur; nil->next = cur; } void deleteNode(Node *cur) { cur->next->prev = cur->prev; cur->prev->next = cur->next; free(cur); } Node *listSearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) { cur = cur->next; } return cur; } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } void deleteKey(int key) { Node *cur = listSearch(key); if (cur == nil) return; deleteNode(cur); } void printList() { Node *ptr = nil->next; // do{ // if (head == ptr) // printf("%d", ptr->x); // else // printf(" %d", ptr->x); // ptr = ptr->next; // }while(ptr != head); int flag = 1; while (ptr != nil) { if (flag) { printf("%d", ptr->key); flag = 0; } else { printf(" %d", ptr->key); } ptr = ptr->next; } putchar('\n'); } int main() { init(); int n, key; scanf("%d", &n); for (int i = 0; i < n; i++) { char s[10]; scanf("%s", s); if (strcmp(s, "insert") == 0) { scanf("%d", &key); insert(key); } if (strcmp(s, "delete") == 0) { scanf("%d", &key); deleteKey(key); } if (strcmp(s, "deleteFirst") == 0) { deleteFirst(); } if (strcmp(s, "deleteLast") == 0) { deleteLast(); } } printList(); return 0; }
insert
46
46
46
48
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <memory> using namespace std; template <typename T> class Node { private: T _key; shared_ptr<Node<T>> _next; weak_ptr<Node<T>> _prev; public: Node(T k) : _key(k), _next(nullptr), _prev() {} T key() const { return _key; } shared_ptr<Node<T>> next() const { return _next; } shared_ptr<Node<T>> prev() const { return shared_ptr<Node<T>>(_prev); } bool is_first() const { return _prev.expired(); } bool is_last() const { return _next.get() == nullptr; } void set_first() { _prev = weak_ptr<Node<T>>(); } void set_last() { _next = nullptr; } void set_next(shared_ptr<Node<T>> next) { _next = next; } void set_prev(shared_ptr<Node<T>> prev) { _prev = weak_ptr<Node<T>>(prev); } }; template <typename T> class DoublyLinkedList { private: shared_ptr<Node<T>> _first; shared_ptr<Node<T>> _last; public: DoublyLinkedList() : _first(nullptr), _last(nullptr) {} void insertNode(T key) { auto n = make_shared<Node<T>>(key); if (is_empty()) { _first = n; _last = n; } else { _first->set_prev(n); n->set_next(_first); _first = n; } } void deleteNode(T key) { auto n = _first; while (n != nullptr) { if (n->key() == key) { if (n->is_first() && n->is_last()) { clear(); } else if (n->is_first()) { deleteFirst(); } else if (n->is_last()) { deleteLast(); } else { auto next = n->next(); auto prev = n->prev(); prev->set_next(next); next->set_prev(prev); } break; } n = n->next(); } } void deleteFirst() { if (_first->is_last()) { clear(); } else { auto next = _first->next(); _first = next; next->set_first(); } } void deleteLast() { if (_last->is_first()) { clear(); } else { auto prev = _last->prev(); _last = prev; prev->set_last(); } } bool is_empty() const { return _first.get() == nullptr; } void clear() { _first = nullptr; _last = nullptr; } string to_string() { auto n = _first; string s; while (n != nullptr) { s += std::to_string(n->key()); if (n != _last) s += " "; n = n->next(); } return s; } }; int main() { int n; cin >> n; DoublyLinkedList<int> list; for (int i = 0; i < n; i++) { string op; cin >> op; if (op == "insert") { int in; cin >> in; list.insertNode(in); } else if (op == "delete") { int in; cin >> in; list.deleteNode(in); } else if (op == "deleteFirst") { list.deleteFirst(); } else if (op == "deleteLast") { list.deleteLast(); } } cout << list.to_string() << endl; return 0; }
#include <iostream> #include <memory> using namespace std; template <typename T> class Node { private: T _key; shared_ptr<Node<T>> _next; weak_ptr<Node<T>> _prev; public: Node(T k) : _key(k), _next(nullptr), _prev() {} T key() const { return _key; } shared_ptr<Node<T>> next() const { return _next; } shared_ptr<Node<T>> prev() const { return shared_ptr<Node<T>>(_prev); } bool is_first() const { return _prev.expired(); } bool is_last() const { return _next.get() == nullptr; } void set_first() { _prev = weak_ptr<Node<T>>(); } void set_last() { _next = nullptr; } void set_next(shared_ptr<Node<T>> next) { _next = next; } void set_prev(shared_ptr<Node<T>> prev) { _prev = weak_ptr<Node<T>>(prev); } }; template <typename T> class DoublyLinkedList { private: shared_ptr<Node<T>> _first; shared_ptr<Node<T>> _last; public: DoublyLinkedList() : _first(nullptr), _last(nullptr) {} void insertNode(T key) { auto n = make_shared<Node<T>>(key); if (is_empty()) { _first = n; _last = n; } else { _first->set_prev(n); n->set_next(_first); _first = n; } } void deleteNode(T key) { auto n = _first; while (n != nullptr) { if (n->key() == key) { if (n->is_first() && n->is_last()) { clear(); } else if (n->is_first()) { deleteFirst(); } else if (n->is_last()) { deleteLast(); } else { auto next = n->next(); auto prev = n->prev(); prev->set_next(next); next->set_prev(prev); } break; } n = n->next(); } } void deleteFirst() { if (_first->is_last()) { clear(); } else { auto next = _first->next(); _first = next; next->set_first(); } } void deleteLast() { if (_last->is_first()) { clear(); } else { auto prev = _last->prev(); _last = prev; prev->set_last(); } } bool is_empty() const { return _first.get() == nullptr; } void clear() { _first = nullptr; _last = nullptr; } string to_string() { auto n = _first; string s; while (n != nullptr) { s += std::to_string(n->key()); if (n != _last) s += " "; n = n->next(); } return s; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; DoublyLinkedList<int> list; for (int i = 0; i < n; i++) { string op; cin >> op; if (op == "insert") { int in; cin >> in; list.insertNode(in); } else if (op == "delete") { int in; cin >> in; list.deleteNode(in); } else if (op == "deleteFirst") { list.deleteFirst(); } else if (op == "deleteLast") { list.deleteLast(); } } cout << list.to_string() << endl; return 0; }
insert
112
112
112
115
TLE
p02265
C++
Runtime Error
#include <iostream> #include <list> using namespace std; int main() { list<int> list; int n; cin >> n; string cmd; int i, arg; for (i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> arg; list.push_front(arg); } else if (cmd == "delete") { cin >> arg; int pos = 0; for (int tmp : list) { if (tmp == arg) break; pos++; } list.erase(next(list.begin(), pos)); } else if (cmd == "deleteFirst") { list.pop_front(); } else if (cmd == "deleteLast") { list.pop_back(); } } int size = static_cast<int>(list.size()); if (size > 0) { cout << list.front(); list.pop_front(); for (i = 1; i < size; i++) { cout << ' ' << list.front(); list.pop_front(); } } cout << endl; return 0; }
#include <iostream> #include <list> using namespace std; int main() { list<int> list; int n; cin >> n; string cmd; int i, arg; for (i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> arg; list.push_front(arg); } else if (cmd == "delete") { cin >> arg; int pos = 0; for (int tmp : list) { if (tmp == arg) break; pos++; } if (pos != static_cast<int>(list.size())) list.erase(next(list.begin(), pos)); } else if (cmd == "deleteFirst") { list.pop_front(); } else if (cmd == "deleteLast") { list.pop_back(); } } int size = static_cast<int>(list.size()); if (size > 0) { cout << list.front(); list.pop_front(); for (i = 1; i < size; i++) { cout << ' ' << list.front(); list.pop_front(); } } cout << endl; return 0; }
replace
25
26
25
27
0
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cmath> #include <cstdint> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <string> #include <vector> using namespace std; struct Node { Node *prev; Node *next; int value; Node(Node *prev, Node *next, int value) : prev(prev), next(next), value(value) {} }; class List { Node *begin = new Node(nullptr, nullptr, -1); Node *end = new Node(nullptr, nullptr, -1); public: List() { begin->next = end; end->prev = begin; } void insert(int value) { auto next = begin->next; auto n = new Node(begin, next, value); next->prev = n; begin->next = n; } void deleteNode(int value) { auto node = begin; for (node = begin->next; node != end; node = node->next) { if (node->value == value) { node->prev->next = node->next; node->next->prev = node->prev; return; } } } void deleteFirst() { auto old = begin->next; auto next = old->next; begin->next = next; next->prev = begin; delete old; } void deleteLast() { auto old = end->prev; auto prev = old->prev; prev->next = end; end->prev = prev; delete old; } void print() { for (auto node = begin->next; node != end; node = node->next) { cout << node->value; if (node != end->prev) { cout << " "; } } cout << endl; } }; int main() { int n; cin >> n; List list; for (int i = 0; i < n; ++i) { string command; cin >> command; if (command == "deleteFirst") { list.deleteFirst(); } else if (command == "deleteLast") { list.deleteLast(); } else if (command == "insert") { int value; cin >> value; list.insert(value); } else if (command == "delete") { int value; cin >> value; list.deleteNode(value); } } list.print(); }
#include <algorithm> #include <climits> #include <cmath> #include <cstdint> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <string> #include <vector> using namespace std; struct Node { Node *prev; Node *next; int value; Node(Node *prev, Node *next, int value) : prev(prev), next(next), value(value) {} }; class List { Node *begin = new Node(nullptr, nullptr, -1); Node *end = new Node(nullptr, nullptr, -1); public: List() { begin->next = end; end->prev = begin; } void insert(int value) { auto next = begin->next; auto n = new Node(begin, next, value); next->prev = n; begin->next = n; } void deleteNode(int value) { auto node = begin; for (node = begin->next; node != end; node = node->next) { if (node->value == value) { node->prev->next = node->next; node->next->prev = node->prev; return; } } } void deleteFirst() { auto old = begin->next; auto next = old->next; begin->next = next; next->prev = begin; delete old; } void deleteLast() { auto old = end->prev; auto prev = old->prev; prev->next = end; end->prev = prev; delete old; } void print() { for (auto node = begin->next; node != end; node = node->next) { cout << node->value; if (node != end->prev) { cout << " "; } } cout << endl; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; List list; for (int i = 0; i < n; ++i) { string command; cin >> command; if (command == "deleteFirst") { list.deleteFirst(); } else if (command == "deleteLast") { list.deleteLast(); } else if (command == "insert") { int value; cin >> value; list.insert(value); } else if (command == "delete") { int value; cin >> value; list.deleteNode(value); } } list.print(); }
insert
74
74
74
77
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> using namespace std; int main() { list<int> s; list<int>::iterator it, endit; int n; cin >> n; pair<string, int> p; string str; for (int i = 0; i < n; i++) { cin >> str; if (str != "deleteFirst" && str != "deleteLast") { cin >> p.second; p.first = str; if (p.first == "insert") s.push_front(p.second); else if (p.first == "delete") { it = s.begin(), endit = s.end(); for (; it != endit; ++it) { if (*it == p.second) { s.erase(it); break; } } } } else if (str == "deleteFirst") { s.pop_front(); } else s.pop_back(); } cout << s.front(); s.pop_front(); for (; !s.empty();) { cout << " " << s.front(); s.pop_front(); } cout << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); list<int> s; list<int>::iterator it, endit; int n; cin >> n; pair<string, int> p; string str; for (int i = 0; i < n; i++) { cin >> str; if (str != "deleteFirst" && str != "deleteLast") { cin >> p.second; p.first = str; if (p.first == "insert") s.push_front(p.second); else if (p.first == "delete") { it = s.begin(), endit = s.end(); for (; it != endit; ++it) { if (*it == p.second) { s.erase(it); break; } } } } else if (str == "deleteFirst") { s.pop_front(); } else s.pop_back(); } cout << s.front(); s.pop_front(); for (; !s.empty();) { cout << " " << s.front(); s.pop_front(); } cout << endl; }
insert
18
18
18
20
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <string> using namespace std; struct Node { int key; Node *prev; Node *next; }; class List { Node *nil; void delete_node(Node *n) { n->prev->next = n->next; n->next->prev = n->prev; delete n; } public: List() { nil = new Node; nil->prev = nil; nil->next = nil; } void insert_key(int key) { Node *n = new Node; n->key = key; nil->next->prev = n; n->next = nil->next; nil->next = n; n->prev = nil; } void delete_key(int key) { for (Node *n = nil->next; n != nil; n = n->next) { if (n->key == key) { delete_node(n); return; } } } void print_all() { for (Node *n = nil->next; n != nil; n = n->next) { if (n != nil->next) cout << " "; cout << n->key; } cout << endl; } void deleteFirst() { delete_node(nil->next); } void deleteLast() { delete_node(nil->prev); } }; int main() { int n; cin >> n; List l; for (int i = 0; i < n; i++) { string command; cin >> command; if (command == "deleteFirst") { l.deleteFirst(); continue; } if (command == "deleteLast") { l.deleteLast(); continue; } int key; cin >> key; if (command == "insert") { l.insert_key(key); continue; } if (command == "delete") { l.delete_key(key); continue; } } l.print_all(); return 0; }
#include <iostream> #include <string> using namespace std; struct Node { int key; Node *prev; Node *next; }; class List { Node *nil; void delete_node(Node *n) { n->prev->next = n->next; n->next->prev = n->prev; delete n; } public: List() { nil = new Node; nil->prev = nil; nil->next = nil; } void insert_key(int key) { Node *n = new Node; n->key = key; nil->next->prev = n; n->next = nil->next; nil->next = n; n->prev = nil; } void delete_key(int key) { for (Node *n = nil->next; n != nil; n = n->next) { if (n->key == key) { delete_node(n); return; } } } void print_all() { for (Node *n = nil->next; n != nil; n = n->next) { if (n != nil->next) cout << " "; cout << n->key; } cout << endl; } void deleteFirst() { delete_node(nil->next); } void deleteLast() { delete_node(nil->prev); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; List l; for (int i = 0; i < n; i++) { string command; cin >> command; if (command == "deleteFirst") { l.deleteFirst(); continue; } if (command == "deleteLast") { l.deleteLast(); continue; } int key; cin >> key; if (command == "insert") { l.insert_key(key); continue; } if (command == "delete") { l.delete_key(key); continue; } } l.print_all(); return 0; }
insert
53
53
53
55
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; int main() { list<int> L; int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "deleteFirst") { L.pop_front(); continue; } if (s == "deleteLast") { L.pop_back(); continue; } int num; cin >> num; if (s == "insert") { L.push_front(num); continue; } if (s == "delete") { for (list<int>::iterator it = L.begin(); it != L.end(); it++) { if (*it == num) { L.erase(it); break; } } } } for (list<int>::iterator it = L.begin(); it != L.end();) { cout << *it; if (++it != L.end()) { cout << " "; } else { cout << endl; } } return 0; }
#include <iostream> #include <list> #include <string> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); list<int> L; int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "deleteFirst") { L.pop_front(); continue; } if (s == "deleteLast") { L.pop_back(); continue; } int num; cin >> num; if (s == "insert") { L.push_front(num); continue; } if (s == "delete") { for (list<int>::iterator it = L.begin(); it != L.end(); it++) { if (*it == num) { L.erase(it); break; } } } } for (list<int>::iterator it = L.begin(); it != L.end();) { cout << *it; if (++it != L.end()) { cout << " "; } else { cout << endl; } } return 0; }
insert
6
6
6
8
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> using namespace std; class list { public: int val; list *prev; list *next; }; list *head = NULL; list *tail = NULL; void insert_(int val) { list *element; element = new list; element->val = val; element->prev = NULL; if (head == NULL) { element->next = NULL; tail = element; } else { head->prev = element; element->next = head; } head = element; } void delete_(int val) { list *p = head; while (p->val != val) { if (p == tail) { return; } p = p->next; } if (p == head) { head = p->next; } else { p->prev->next = p->next; } if (p == tail) { tail = p->prev; } else { p->next->prev = p->prev; } delete p; } void deleteFirst() { list *p = head; head = p->next; if (head != NULL) { head->prev = NULL; } else { tail = NULL; } delete p; } void deleteLast() { list *p = tail; tail = p->prev; if (tail != NULL) { tail->next = NULL; } else { head = NULL; } delete p; } void trace() { for (list *p = head; p != NULL; p = p->next) { cout << p->val; if (p->next != NULL) { cout << " "; } else { cout << "\n"; } } } int main() { int n; cin >> n; list *head; for (int i = 0; i < n; i++) { string command; cin >> command; if (command == "deleteFirst") { deleteFirst(); continue; } if (command == "deleteLast") { deleteLast(); continue; } int val; cin >> val; if (command == "insert") { insert_(val); } else { delete_(val); } } trace(); }
#include <iostream> using namespace std; class list { public: int val; list *prev; list *next; }; list *head = NULL; list *tail = NULL; void insert_(int val) { list *element; element = new list; element->val = val; element->prev = NULL; if (head == NULL) { element->next = NULL; tail = element; } else { head->prev = element; element->next = head; } head = element; } void delete_(int val) { list *p = head; while (p->val != val) { if (p == tail) { return; } p = p->next; } if (p == head) { head = p->next; } else { p->prev->next = p->next; } if (p == tail) { tail = p->prev; } else { p->next->prev = p->prev; } delete p; } void deleteFirst() { list *p = head; head = p->next; if (head != NULL) { head->prev = NULL; } else { tail = NULL; } delete p; } void deleteLast() { list *p = tail; tail = p->prev; if (tail != NULL) { tail->next = NULL; } else { head = NULL; } delete p; } void trace() { for (list *p = head; p != NULL; p = p->next) { cout << p->val; if (p->next != NULL) { cout << " "; } else { cout << "\n"; } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; list *head; for (int i = 0; i < n; i++) { string command; cin >> command; if (command == "deleteFirst") { deleteFirst(); continue; } if (command == "deleteLast") { deleteLast(); continue; } int val; cin >> val; if (command == "insert") { insert_(val); } else { delete_(val); } } trace(); }
insert
82
82
82
84
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) int main() { int n; string st; int x; cin >> n; list<int> L; list<int>::iterator it, endit; while (n--) { cin >> st; if (st == "insert") { cin >> x; L.push_front(x); } else if (st == "delete") { cin >> x; for (it = L.begin(), endit = L.end(); it != endit; ++it) { if (*it == x) { L.erase(it); break; } } } else if (st == "deleteFirst") { L.pop_front(); } else { // deleteLast L.pop_back(); } } for (it = L.begin(), endit = --L.end(); it != endit; ++it) { cout << *it << ' '; } cout << *it << endl; return 0; }
#include <iostream> #include <list> #include <string> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; string st; int x; cin >> n; list<int> L; list<int>::iterator it, endit; while (n--) { cin >> st; if (st == "insert") { cin >> x; L.push_front(x); } else if (st == "delete") { cin >> x; for (it = L.begin(), endit = L.end(); it != endit; ++it) { if (*it == x) { L.erase(it); break; } } } else if (st == "deleteFirst") { L.pop_front(); } else { // deleteLast L.pop_back(); } } for (it = L.begin(), endit = --L.end(); it != endit; ++it) { cout << *it << ' '; } cout << *it << endl; return 0; }
insert
8
8
8
11
TLE
p02265
C++
Time Limit Exceeded
#include <assert.h> #include <iostream> using namespace std; struct Node; typedef Node *Node_ptr; struct Node { int key; Node_ptr prev, next; Node(int key_ = -1, Node_ptr prev_ = NULL, Node_ptr next_ = NULL) : key(key_), prev(prev_), next(next_) {} }; class LinkedList { private: Node_ptr head, tail; public: LinkedList() { head = new Node(); tail = new Node(); head->next = tail; tail->prev = head; } ~LinkedList() { Node_ptr d = head->next; for (Node_ptr iter = head->next->next; iter != tail; iter = iter->next) { delete d; d = iter; } delete d; delete head; delete tail; } bool empty() { return head->next == tail; } void insert(int x) { Node_ptr nd = new Node(x, head, head->next); nd->next->prev = nd; head->next = nd; } void erase(int x) { Node_ptr tprev, tnext; bool deleted = false; for (Node_ptr iter = head->next; iter != tail; iter = iter->next) { if (iter->key == x) { tprev = iter->prev, tnext = iter->next; delete iter; deleted = true; break; } } if (!deleted) return; tprev->next = tnext; tnext->prev = tprev; } void erase_first() { if (empty()) return; Node_ptr tnext = head->next->next; delete head->next; head->next = tnext; tnext->prev = head; } void erase_last() { if (empty()) return; Node_ptr tprev = tail->prev->prev; delete tail->prev; tprev->next = tail; tail->prev = tprev; } void print() { for (Node_ptr iter = head->next; iter != tail; iter = iter->next) { if (iter != head->next) cout << " "; cout << iter->key; } cout << endl; } }; int main() { int Q_num; cin >> Q_num; LinkedList list; for (int q = 0; q < Q_num; q++) { string s; cin >> s; if (s == "deleteFirst") list.erase_first(); else if (s == "deleteLast") list.erase_last(); else { int x; cin >> x; if (s == "insert") list.insert(x); if (s == "delete") list.erase(x); } } list.print(); return 0; }
#include <assert.h> #include <iostream> using namespace std; struct Node; typedef Node *Node_ptr; struct Node { int key; Node_ptr prev, next; Node(int key_ = -1, Node_ptr prev_ = NULL, Node_ptr next_ = NULL) : key(key_), prev(prev_), next(next_) {} }; class LinkedList { private: Node_ptr head, tail; public: LinkedList() { head = new Node(); tail = new Node(); head->next = tail; tail->prev = head; } ~LinkedList() { Node_ptr d = head->next; for (Node_ptr iter = head->next->next; iter != tail; iter = iter->next) { delete d; d = iter; } delete d; delete head; delete tail; } bool empty() { return head->next == tail; } void insert(int x) { Node_ptr nd = new Node(x, head, head->next); nd->next->prev = nd; head->next = nd; } void erase(int x) { Node_ptr tprev, tnext; bool deleted = false; for (Node_ptr iter = head->next; iter != tail; iter = iter->next) { if (iter->key == x) { tprev = iter->prev, tnext = iter->next; delete iter; deleted = true; break; } } if (!deleted) return; tprev->next = tnext; tnext->prev = tprev; } void erase_first() { if (empty()) return; Node_ptr tnext = head->next->next; delete head->next; head->next = tnext; tnext->prev = head; } void erase_last() { if (empty()) return; Node_ptr tprev = tail->prev->prev; delete tail->prev; tprev->next = tail; tail->prev = tprev; } void print() { for (Node_ptr iter = head->next; iter != tail; iter = iter->next) { if (iter != head->next) cout << " "; cout << iter->key; } cout << endl; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int Q_num; cin >> Q_num; LinkedList list; for (int q = 0; q < Q_num; q++) { string s; cin >> s; if (s == "deleteFirst") list.erase_first(); else if (s == "deleteLast") list.erase_last(); else { int x; cin >> x; if (s == "insert") list.insert(x); if (s == "delete") list.erase(x); } } list.print(); return 0; }
insert
92
92
92
95
TLE
p02265
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; typedef int data_t; typedef struct tag_node { tag_node *before_; data_t data_; tag_node *next_; } node_t; class List { node_t *head_; node_t *FindNodeFromBefore(data_t) const; node_t *FindLastNode(void) const; public: List(void); void Insert(data_t); int Delete(data_t); int DeleteFirst(void); int DeleteLast(void); int Print(void) const; }; List::List(void) { head_ = new node_t; head_->before_ = head_; head_->next_ = head_; } node_t *List::FindNodeFromBefore(data_t findData) const { node_t *tmp; for (tmp = head_->next_; tmp != head_; tmp = tmp->next_) { if (tmp->data_ == findData) return tmp; } return NULL; } node_t *List::FindLastNode(void) const { node_t *tmp; for (tmp = head_->next_; tmp->next_ != NULL; tmp = tmp->next_) ; return tmp; } void List::Insert(data_t data) { node_t *node = new node_t; node->data_ = data; node->next_ = head_->next_; head_->next_->before_ = node; head_->next_ = node; node->before_ = head_; } int List::Delete(data_t data) { node_t *target = FindNodeFromBefore(data); if (target == head_) return 1; target->before_->next_ = target->next_; target->next_->before_ = target->before_; delete target; return 0; } int List::DeleteFirst(void) { node_t *target = head_->next_; if (target == head_) return 1; target->before_->next_ = target->next_; target->next_->before_ = target->before_; delete target; } int List::DeleteLast(void) { node_t *target = head_->before_; if (target == head_) return 1; target->before_->next_ = target->next_; target->next_->before_ = target->before_; delete target; } int List::Print(void) const { if (head_->next_ == head_) return 1; node_t *tmp; for (tmp = head_->next_; tmp != head_; tmp = tmp->next_) { if (tmp != head_->next_) cout << " "; cout << tmp->data_; } cout << endl; return 0; } int main(void) { int n; data_t value; char command[12]; List list; scanf("%d%*c", &n); for (int i = 0; i < n; i++) { scanf("%s%*c", command); if (command[0] == 'i') { scanf("%d%*c", &value); list.Insert(value); continue; } switch (command[6]) { case 'F': list.DeleteFirst(); continue; case 'L': list.DeleteLast(); continue; default: scanf("%d%*c", &value); list.Delete(value); continue; } } list.Print(); return 0; }
#include <cstdio> #include <iostream> using namespace std; typedef int data_t; typedef struct tag_node { tag_node *before_; data_t data_; tag_node *next_; } node_t; class List { node_t *head_; node_t *FindNodeFromBefore(data_t) const; node_t *FindLastNode(void) const; public: List(void); void Insert(data_t); int Delete(data_t); int DeleteFirst(void); int DeleteLast(void); int Print(void) const; }; List::List(void) { head_ = new node_t; head_->before_ = head_; head_->next_ = head_; } node_t *List::FindNodeFromBefore(data_t findData) const { node_t *tmp; for (tmp = head_->next_; tmp != head_; tmp = tmp->next_) { if (tmp->data_ == findData) return tmp; } return tmp; } node_t *List::FindLastNode(void) const { node_t *tmp; for (tmp = head_->next_; tmp->next_ != NULL; tmp = tmp->next_) ; return tmp; } void List::Insert(data_t data) { node_t *node = new node_t; node->data_ = data; node->next_ = head_->next_; head_->next_->before_ = node; head_->next_ = node; node->before_ = head_; } int List::Delete(data_t data) { node_t *target = FindNodeFromBefore(data); if (target == head_) return 1; target->before_->next_ = target->next_; target->next_->before_ = target->before_; delete target; return 0; } int List::DeleteFirst(void) { node_t *target = head_->next_; if (target == head_) return 1; target->before_->next_ = target->next_; target->next_->before_ = target->before_; delete target; } int List::DeleteLast(void) { node_t *target = head_->before_; if (target == head_) return 1; target->before_->next_ = target->next_; target->next_->before_ = target->before_; delete target; } int List::Print(void) const { if (head_->next_ == head_) return 1; node_t *tmp; for (tmp = head_->next_; tmp != head_; tmp = tmp->next_) { if (tmp != head_->next_) cout << " "; cout << tmp->data_; } cout << endl; return 0; } int main(void) { int n; data_t value; char command[12]; List list; scanf("%d%*c", &n); for (int i = 0; i < n; i++) { scanf("%s%*c", command); if (command[0] == 'i') { scanf("%d%*c", &value); list.Insert(value); continue; } switch (command[6]) { case 'F': list.DeleteFirst(); continue; case 'L': list.DeleteLast(); continue; default: scanf("%d%*c", &value); list.Delete(value); continue; } } list.Print(); return 0; }
replace
40
41
40
41
0
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <stdio.h> #include <string> using namespace std; list<int> lst; int main() { int dataset; scanf("%d", &dataset); for (int i = 0; i < dataset; i++) { string order; cin >> order; if (order[0] == 'i') { int a; scanf("%d", &a); lst.push_front(a); } else if (order[6] == 'F') { lst.pop_front(); } else if (order[6] == 'L') { lst.pop_back(); } else { int order; cin >> order; auto itr = lst.begin(); int size = lst.size(); for (int i = 0; i < size; i++) { if (*itr == order) { lst.erase(itr); break; } itr++; } } } for (auto itr = lst.begin(); itr != lst.end(); itr++) { if (itr != lst.begin()) { printf(" "); } printf("%d", *itr); } printf("\n"); return 0; }
#include <iostream> #include <list> #include <stdio.h> #include <string> using namespace std; list<int> lst; int main() { int dataset; scanf("%d", &dataset); for (int i = 0; i < dataset; i++) { char order[20]; scanf("%s", order); if (order[0] == 'i') { int a; scanf("%d", &a); lst.push_front(a); } else if (order[6] == 'F') { lst.pop_front(); } else if (order[6] == 'L') { lst.pop_back(); } else { int order; cin >> order; auto itr = lst.begin(); int size = lst.size(); for (int i = 0; i < size; i++) { if (*itr == order) { lst.erase(itr); break; } itr++; } } } for (auto itr = lst.begin(); itr != lst.end(); itr++) { if (itr != lst.begin()) { printf(" "); } printf("%d", *itr); } printf("\n"); return 0; }
replace
12
14
12
14
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); string s; int n, i, a; list<int> li; list<int>::iterator p = li.begin(); cin >> n; for (i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> a; li.push_front(a); } else if (s == "delete") { cin >> a; p = find(li.begin(), li.end(), a); li.erase(p); } else if (s == "deleteFirst") { li.pop_front(); } else if (s == "deleteLast") { li.pop_back(); } } p = li.begin(); while (p != li.end()) { cout << *p; p++; if (p != li.end()) cout << " "; else cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); string s; int n, i, a; list<int> li; list<int>::iterator p = li.begin(); cin >> n; for (i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> a; li.push_front(a); } else if (s == "delete") { cin >> a; p = find(li.begin(), li.end(), a); if (p != li.end()) li.erase(p); } else if (s == "deleteFirst") { li.pop_front(); } else if (s == "deleteLast") { li.pop_back(); } } p = li.begin(); while (p != li.end()) { cout << *p; p++; if (p != li.end()) cout << " "; else cout << endl; } return 0; }
replace
23
24
23
25
0
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { string s; int n, i, a; list<int> li; list<int>::iterator p = li.begin(); cin >> n; for (i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> a; li.push_front(a); } else if (s == "delete") { cin >> a; p = find(li.begin(), li.end(), a); if (p != li.end()) li.erase(p); } else if (s == "deleteFirst") { li.pop_front(); } else if (s == "deleteLast") { li.pop_back(); } } p = li.begin(); while (p != li.end()) { cout << *p; p++; if (p != li.end()) cout << " "; else cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); string s; int n, i, a; list<int> li; list<int>::iterator p = li.begin(); cin >> n; for (i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> a; li.push_front(a); } else if (s == "delete") { cin >> a; p = find(li.begin(), li.end(), a); if (p != li.end()) li.erase(p); } else if (s == "deleteFirst") { li.pop_front(); } else if (s == "deleteLast") { li.pop_back(); } } p = li.begin(); while (p != li.end()) { cout << *p; p++; if (p != li.end()) cout << " "; else cout << endl; } return 0; }
insert
7
7
7
9
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; void print_list(list<int> &V) { for (list<int>::iterator it = V.begin(); it != V.end(); it++) { if (it == V.begin()) { cout << *it; } else { cout << " " << *it; } } cout << endl; } int main(void) { list<int> V; int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; int x; if (s == "insert") { cin >> x; // V.(V.begin(), x); V.push_front(x); } else if (s == "delete") { cin >> x; for (list<int>::iterator it = V.begin(); it != V.end(); it++) { if (*it == x) { V.erase(it); break; } } } else if (s == "deleteFirst") { V.pop_front(); } else { V.pop_back(); } } print_list(V); return 0; }
#include <iostream> #include <list> #include <string> using namespace std; void print_list(list<int> &V) { for (list<int>::iterator it = V.begin(); it != V.end(); it++) { if (it == V.begin()) { cout << *it; } else { cout << " " << *it; } } cout << endl; } int main(void) { ios_base::sync_with_stdio(false); list<int> V; int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; int x; if (s == "insert") { cin >> x; // V.(V.begin(), x); V.push_front(x); } else if (s == "delete") { cin >> x; for (list<int>::iterator it = V.begin(); it != V.end(); it++) { if (*it == x) { V.erase(it); break; } } } else if (s == "deleteFirst") { V.pop_front(); } else { V.pop_back(); } } print_list(V); return 0; }
insert
17
17
17
19
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *x = (Node *)malloc(sizeof(Node)); x->key = key; x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } Node *listSearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) { cur = cur->next; } return cur; } 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)); } int main() { int n, key; char com[20]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s %d", com, &key); if (com[0] == 'i') { insert(key); } else if (com[0] == 'd') { if (strlen(com) > 6) { if (com[6] == 'F') deleteFirst(); else if (com[6] == 'L') deleteLast(); } else { deleteKey(key); } } } 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"); }
#include <algorithm> #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *x = (Node *)malloc(sizeof(Node)); x->key = key; x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } Node *listSearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) { cur = cur->next; } return cur; } 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)); } int main() { int n, key; char com[20]; scanf("%d", &n); init(); for (int i = 0; i < n; i++) { scanf("%s %d", com, &key); if (com[0] == 'i') { insert(key); } else if (com[0] == 'd') { if (strlen(com) > 6) { if (com[6] == 'F') deleteFirst(); else if (com[6] == 'L') deleteLast(); } else { deleteKey(key); } } } 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"); }
insert
56
56
56
57
-11
p02265
C++
Time Limit Exceeded
#include <iostream> #include <stdlib.h> using namespace std; struct Node { int key; Node *next; Node *prev; }; struct List { Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; nil->key = 0; } Node *find(int key) { for (Node *i = nil->next; i != nil; i = i->next) { if (i->key == key) return i; } return nil; } void insert(Node *x) { x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } void add(int x) { Node *p = (Node *)malloc(sizeof(Node)); p->key = x; insert(p); } void delete_x(int key) { Node *x = find(key); if (x == nil) return; x->prev->next = x->next; x->next->prev = x->prev; free(x); } void deleteFirst() { Node *x = nil->next; x->prev->next = x->next; x->next->prev = x->prev; free(x); } void deleteLast() { Node *x = nil->prev; x->prev->next = x->next; x->next->prev = x->prev; free(x); } void output() { int flg = 0; for (Node *i = nil->next; i != nil; i = i->next) { if (flg) cout << ' '; cout << i->key; flg = 1; } cout << endl; } }; int main() { List list; list.init(); int n, x; string str; cin >> n; for (int i = 0; i < n; i++) { cin >> str; if (str == "insert") { cin >> x; list.add(x); } else if (str == "delete") { cin >> x; list.delete_x(x); } else if (str == "deleteFirst") { list.deleteFirst(); } else { list.deleteLast(); } } // cout<<'-'<<endl; list.output(); }
#include <iostream> #include <stdlib.h> using namespace std; struct Node { int key; Node *next; Node *prev; }; struct List { Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; nil->key = 0; } Node *find(int key) { for (Node *i = nil->next; i != nil; i = i->next) { if (i->key == key) return i; } return nil; } void insert(Node *x) { x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } void add(int x) { Node *p = (Node *)malloc(sizeof(Node)); p->key = x; insert(p); } void delete_x(int key) { Node *x = find(key); if (x == nil) return; x->prev->next = x->next; x->next->prev = x->prev; free(x); } void deleteFirst() { Node *x = nil->next; x->prev->next = x->next; x->next->prev = x->prev; free(x); } void deleteLast() { Node *x = nil->prev; x->prev->next = x->next; x->next->prev = x->prev; free(x); } void output() { int flg = 0; for (Node *i = nil->next; i != nil; i = i->next) { if (flg) cout << ' '; cout << i->key; flg = 1; } cout << endl; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); List list; list.init(); int n, x; string str; cin >> n; for (int i = 0; i < n; i++) { cin >> str; if (str == "insert") { cin >> x; list.add(x); } else if (str == "delete") { cin >> x; list.delete_x(x); } else if (str == "deleteFirst") { list.deleteFirst(); } else { list.deleteLast(); } } // cout<<'-'<<endl; list.output(); }
insert
78
78
78
81
TLE
p02265
C++
Runtime Error
// -*- mode:c++; coding:utf-8; indent-tabs-mode:nil; -*- // ALDS1_3-C: Doubly Linked List #include <cassert> #include <cstdio> template <typename T> class doubly_linked_list { struct node { node *prev_; node *next_; T x_; }; node sentinel_; public: doubly_linked_list() { sentinel_.prev_ = sentinel_.next_ = &sentinel_; } void insert_elem_to_first(const T &x) { node *np = new node(); np->x_ = x; np->prev_ = &sentinel_; np->next_ = sentinel_.next_; np->prev_->next_ = np; np->next_->prev_ = np; } void delete_first_elem(const T &x) { delete_node(search_first_elem(x)); } void delete_first_node() { delete_node(sentinel_.next_); } void delete_last_node() { delete_node(sentinel_.prev_); } private: void delete_node(node *np) { np->next_->prev_ = np->prev_; np->prev_->next_ = np->next_; delete np; } node *search_first_elem(const T &x) const { node *np = sentinel_.next_; for (; np != &sentinel_; np = np->next_) { if (np->x_ == x) break; } return np; } public: void print(const char *sep = " ") const { const char *out_sep = ""; for (const node *np = sentinel_.next_; np != &sentinel_; np = np->next_) { printf("%s%d", out_sep, np->x_); out_sep = sep; } puts(""); } }; int main(void) { unsigned int n; std::scanf("%d", &n); doubly_linked_list<int> list; for (auto i = 0U; i < n; ++i) { char buf[11 + 1]; // max_length == "deleteFirst".size()+1('\0') int x; std::scanf("%11s", buf); if (buf[0] == 'i') { // insert x std::scanf("%d", &x); list.insert_elem_to_first(x); } else if (buf[6] == 'F') { // deleteFirst list.delete_first_node(); } else if (buf[6] == 'L') { // deleteLast list.delete_last_node(); } else { // (buf[6] == '\0') // delete x std::scanf("%d", &x); list.delete_first_elem(x); } } list.print(); return 0; } // end
// -*- mode:c++; coding:utf-8; indent-tabs-mode:nil; -*- // ALDS1_3-C: Doubly Linked List #include <cassert> #include <cstdio> template <typename T> class doubly_linked_list { struct node { node *prev_; node *next_; T x_; }; node sentinel_; public: doubly_linked_list() { sentinel_.prev_ = sentinel_.next_ = &sentinel_; } void insert_elem_to_first(const T &x) { node *np = new node(); np->x_ = x; np->prev_ = &sentinel_; np->next_ = sentinel_.next_; np->prev_->next_ = np; np->next_->prev_ = np; } void delete_first_elem(const T &x) { delete_node(search_first_elem(x)); } void delete_first_node() { delete_node(sentinel_.next_); } void delete_last_node() { delete_node(sentinel_.prev_); } private: void delete_node(node *np) { if (np == &sentinel_) { return; } np->next_->prev_ = np->prev_; np->prev_->next_ = np->next_; delete np; } node *search_first_elem(const T &x) const { node *np = sentinel_.next_; for (; np != &sentinel_; np = np->next_) { if (np->x_ == x) break; } return np; } public: void print(const char *sep = " ") const { const char *out_sep = ""; for (const node *np = sentinel_.next_; np != &sentinel_; np = np->next_) { printf("%s%d", out_sep, np->x_); out_sep = sep; } puts(""); } }; int main(void) { unsigned int n; std::scanf("%d", &n); doubly_linked_list<int> list; for (auto i = 0U; i < n; ++i) { char buf[11 + 1]; // max_length == "deleteFirst".size()+1('\0') int x; std::scanf("%11s", buf); if (buf[0] == 'i') { // insert x std::scanf("%d", &x); list.insert_elem_to_first(x); } else if (buf[6] == 'F') { // deleteFirst list.delete_first_node(); } else if (buf[6] == 'L') { // deleteLast list.delete_last_node(); } else { // (buf[6] == '\0') // delete x std::scanf("%d", &x); list.delete_first_elem(x); } } list.print(); return 0; } // end
insert
30
30
30
33
0
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <cctype> #include <cmath> #include <iostream> #include <list> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #define LEN 100005 using namespace std; // list実装 int main() { list<int> l; int n; cin >> n; for (int i = 0; i < n; i++) { string c; cin >> c; if (c[0] == 'i') { int a; scanf("%d", &a); l.push_front(a); } else { if (c[6] == 'F') { l.pop_front(); } else if (c[6] == 'L') { l.pop_back(); } else { int a; scanf("%d", &a); for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) { if (*itr == a) { l.erase(itr); break; } } } } } int i = 0; for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) { if (i++) cout << " "; printf("%d", *itr); } cout << endl; return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <iostream> #include <list> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #define LEN 100005 using namespace std; // list実装 int main() { list<int> l; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { string c; cin >> c; if (c[0] == 'i') { int a; scanf("%d", &a); l.push_front(a); } else { if (c[6] == 'F') { l.pop_front(); } else if (c[6] == 'L') { l.pop_back(); } else { int a; scanf("%d", &a); for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) { if (*itr == a) { l.erase(itr); break; } } } } } int i = 0; for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) { if (i++) cout << " "; printf("%d", *itr); } cout << endl; return 0; }
replace
16
17
16
17
TLE
p02265
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; typedef struct _list_ { struct _list_ *prev; int value; struct _list_ *next; } list; int main(void) { int max, count; vector<list> x(100000); string a; x[0].value = -1; x[1].value = -1; x[0].next = &x[1]; x[1].prev = &x[0]; count = 1; cin >> max; for (int i = 0; i < max; i++) { cin >> a; if (a == "insert") { count++; cin >> x[count].value; x[count].prev = &x[0]; x[count].next = x[0].next; x[0].next->prev = &x[count]; x[0].next = &x[count]; } else if (a == "delete") { list *nextl = x[0].next; int b; cin >> b; for (;;) { if (nextl->value == b) { nextl->prev->next = nextl->next; nextl->next->prev = nextl->prev; break; } if (nextl->value == -1) break; nextl = nextl->next; } } else if (a == "deleteFirst") { x[0].next->next->prev = &x[0]; x[0].next = x[0].next->next; } else if (a == "deleteLast") { x[1].prev->prev->next = &x[1]; x[1].prev = x[1].prev->prev; } } list *nextl = x[0].next; if (nextl->value != -1) { cout << nextl->value; nextl = nextl->next; } for (;;) { if (nextl->value == -1) break; cout << " " << nextl->value; nextl = nextl->next; } cout << endl; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; typedef struct _list_ { struct _list_ *prev; int value; struct _list_ *next; } list; int main(void) { int max, count; vector<list> x(2000000); string a; x[0].value = -1; x[1].value = -1; x[0].next = &x[1]; x[1].prev = &x[0]; count = 1; cin >> max; for (int i = 0; i < max; i++) { cin >> a; if (a == "insert") { count++; cin >> x[count].value; x[count].prev = &x[0]; x[count].next = x[0].next; x[0].next->prev = &x[count]; x[0].next = &x[count]; } else if (a == "delete") { list *nextl = x[0].next; int b; cin >> b; for (;;) { if (nextl->value == b) { nextl->prev->next = nextl->next; nextl->next->prev = nextl->prev; break; } if (nextl->value == -1) break; nextl = nextl->next; } } else if (a == "deleteFirst") { x[0].next->next->prev = &x[0]; x[0].next = x[0].next->next; } else if (a == "deleteLast") { x[1].prev->prev->next = &x[1]; x[1].prev = x[1].prev->prev; } } list *nextl = x[0].next; if (nextl->value != -1) { cout << nextl->value; nextl = nextl->next; } for (;;) { if (nextl->value == -1) break; cout << " " << nextl->value; nextl = nextl->next; } cout << endl; return 0; }
replace
14
15
14
15
0
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main(void) { list<int> l; int n; cin >> n; for (int i = 0; i < n; i++) { string s; int k; cin >> s; if (s == "insert") { cin >> k; l.emplace_front(k); } else if (s == "delete") { cin >> k; auto it = find(l.begin(), l.end(), k); if (it == l.end()) continue; l.erase(it); } else if (s == "deleteFirst") { l.pop_front(); } else { l.pop_back(); } } for (auto it = l.begin(); it != l.end(); it++) { cout << *it; if (it != --l.end()) { cout << " "; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); list<int> l; int n; cin >> n; for (int i = 0; i < n; i++) { string s; int k; cin >> s; if (s == "insert") { cin >> k; l.emplace_front(k); } else if (s == "delete") { cin >> k; auto it = find(l.begin(), l.end(), k); if (it == l.end()) continue; l.erase(it); } else if (s == "deleteFirst") { l.pop_front(); } else { l.pop_back(); } } for (auto it = l.begin(); it != l.end(); it++) { cout << *it; if (it != --l.end()) { cout << " "; } } cout << endl; return 0; }
insert
4
4
4
6
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <sstream> using namespace std; #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) int main() { list<int> data; list<int>::iterator it; int n; cin >> n; int x; string str; for (int i = 0; i < n; i++) { cin >> str; if (str == "insert") { cin >> x; data.push_front(x); } else if (str == "delete") { cin >> x; for (it = data.begin(); it != data.end(); it++) { if (*it == x) { data.erase(it); break; } } } else if (str == "deleteFirst") data.pop_front(); else data.pop_back(); } it = data.begin(); while (it != data.end()) { cout << *it; it++; (it == data.end()) ? cout << endl : cout << " "; } return 0; }
#include <algorithm> #include <cassert> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <sstream> using namespace std; #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); list<int> data; list<int>::iterator it; int n; cin >> n; int x; string str; for (int i = 0; i < n; i++) { cin >> str; if (str == "insert") { cin >> x; data.push_front(x); } else if (str == "delete") { cin >> x; for (it = data.begin(); it != data.end(); it++) { if (*it == x) { data.erase(it); break; } } } else if (str == "deleteFirst") data.pop_front(); else data.pop_back(); } it = data.begin(); while (it != data.end()) { cout << *it; it++; (it == data.end()) ? cout << endl : cout << " "; } return 0; }
insert
16
16
16
18
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; void Command(list<int> &X, string &c) { if (c == "insert") { int d; cin >> d; X.push_front(d); } else if (c == "delete") { int d; cin >> d; list<int>::iterator itr = X.begin(); while (*itr != d && itr != X.end()) { itr++; } if (itr != X.end()) { X.erase(itr); } } else if (c == "deleteFirst") { X.pop_front(); } else if (c == "deleteLast") { X.pop_back(); } } int main() { int n; cin >> n; list<int> X; for (int i = 0; i < n; i++) { string c; cin >> c; Command(X, c); } list<int>::iterator it = X.begin(); list<int>::iterator end = X.end(); end--; while (it != end) { cout << *it << ' '; it++; } cout << *end << endl; return 0; }
#include <iostream> #include <list> #include <string> using namespace std; void Command(list<int> &X, string &c) { if (c == "insert") { int d; cin >> d; X.push_front(d); } else if (c == "delete") { int d; cin >> d; list<int>::iterator itr = X.begin(); while (*itr != d && itr != X.end()) { itr++; } if (itr != X.end()) { X.erase(itr); } } else if (c == "deleteFirst") { X.pop_front(); } else if (c == "deleteLast") { X.pop_back(); } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; list<int> X; for (int i = 0; i < n; i++) { string c; cin >> c; Command(X, c); } list<int>::iterator it = X.begin(); list<int>::iterator end = X.end(); end--; while (it != end) { cout << *it << ' '; it++; } cout << *end << endl; return 0; }
insert
28
28
28
30
TLE
p02265
C++
Runtime Error
#include <cassert> #include <iostream> #include <string> class list { private: struct TElement { int Value; TElement *PrevAd; TElement *NextAd; }; TElement *FStartElement; TElement *FLastElement; public: class iterator { friend class list; public: iterator(TElement *AElement); bool operator!=(const iterator &itr) const; void operator++(int); void operator--(int); int &operator*(); private: TElement *FElement; }; list(); ~list(); void push_front(int Value); void pop_front(); void pop_back(); void erase(iterator itr); iterator begin(); iterator end(); }; list::list() { TElement *DummyPoint = new TElement; *DummyPoint = {0, NULL, NULL}; FStartElement = DummyPoint; FLastElement = DummyPoint; } list::~list() { while (FStartElement) { TElement *NextPoint = FStartElement->NextAd; delete FStartElement; FStartElement = NextPoint; } } void list::push_front(int Value) { TElement *AddPoint = new TElement; *AddPoint = {Value, NULL, FStartElement}; if (FStartElement == FLastElement) { FStartElement = AddPoint; FLastElement = FStartElement->NextAd; FLastElement->PrevAd = FStartElement; } else { FStartElement->PrevAd = AddPoint; FStartElement = AddPoint; } } void list::pop_front() { assert(FStartElement != FLastElement); TElement *DeleteNextPoint = FStartElement->NextAd; DeleteNextPoint->PrevAd = NULL; delete FStartElement; FStartElement = DeleteNextPoint; } void list::pop_back() { assert(FStartElement != FLastElement); TElement *DeletePoint = FLastElement->PrevAd; FLastElement->PrevAd = DeletePoint->PrevAd; DeletePoint->PrevAd->NextAd = FLastElement; delete DeletePoint; } void list::erase(iterator itr) { if (itr.FElement->PrevAd != NULL) { (itr.FElement->PrevAd)->NextAd = itr.FElement->NextAd; (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd; } else { (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd; FStartElement = itr.FElement->NextAd; } delete itr.FElement; } list::iterator::iterator(TElement *AElement) { FElement = AElement; } list::iterator list::begin() { iterator itr = {FStartElement}; return itr; } list::iterator list::end() { iterator itr = {FLastElement}; return itr; } bool list::iterator::operator!=(const iterator &itr) const { return itr.FElement != FElement; } void list::iterator::operator++(int) { FElement = FElement->NextAd; } void list::iterator::operator--(int) { FElement = FElement->PrevAd; } int &list::iterator::operator*() { return FElement->Value; } void Command(list &X, std::string &c) { if (c == "insert") { int d; std::cin >> d; X.push_front(d); } else if (c == "delete") { int d; std::cin >> d; list::iterator itr = X.begin(); while (*itr != d && itr != X.end()) { itr++; } if (itr != X.end()) { X.erase(itr); } } else if (c == "deleteFirst") { X.pop_front(); } else if (c == "deleteLast") { X.pop_back(); } } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int n; std::cin >> n; list X; for (int i = 0; i < n; i++) { std::string c; std::cin >> c; Command(X, c); } list::iterator it = X.begin(); list::iterator end = X.end(); end--; while (it != end) { std::cout << *it << ' '; it++; } std::cout << *end << std::endl; return 0; }
#include <cassert> #include <iostream> #include <string> class list { private: struct TElement { int Value; TElement *PrevAd; TElement *NextAd; }; TElement *FStartElement; TElement *FLastElement; public: class iterator { friend class list; public: iterator(TElement *AElement); bool operator!=(const iterator &itr) const; void operator++(int); void operator--(int); int &operator*(); private: TElement *FElement; }; list(); ~list(); void push_front(int Value); void pop_front(); void pop_back(); void erase(iterator itr); iterator begin(); iterator end(); }; list::list() { TElement *DummyPoint = new TElement; *DummyPoint = {0, NULL, NULL}; FStartElement = DummyPoint; FLastElement = DummyPoint; } list::~list() { while (FStartElement) { TElement *NextPoint = FStartElement->NextAd; delete FStartElement; FStartElement = NextPoint; } } void list::push_front(int Value) { TElement *AddPoint = new TElement; *AddPoint = {Value, NULL, FStartElement}; if (FStartElement == FLastElement) { FStartElement = AddPoint; FLastElement = FStartElement->NextAd; FLastElement->PrevAd = FStartElement; } else { FStartElement->PrevAd = AddPoint; FStartElement = AddPoint; } } void list::pop_front() { assert(FStartElement != FLastElement); TElement *DeleteNextPoint = FStartElement->NextAd; DeleteNextPoint->PrevAd = NULL; delete FStartElement; FStartElement = DeleteNextPoint; } void list::pop_back() { assert(FStartElement != FLastElement); TElement *DeletePoint = FLastElement->PrevAd; FLastElement->PrevAd = DeletePoint->PrevAd; if (FLastElement->PrevAd != NULL) { (FLastElement->PrevAd)->NextAd = FLastElement; } else { FStartElement = FLastElement; } delete DeletePoint; } void list::erase(iterator itr) { if (itr.FElement->PrevAd != NULL) { (itr.FElement->PrevAd)->NextAd = itr.FElement->NextAd; (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd; } else { (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd; FStartElement = itr.FElement->NextAd; } delete itr.FElement; } list::iterator::iterator(TElement *AElement) { FElement = AElement; } list::iterator list::begin() { iterator itr = {FStartElement}; return itr; } list::iterator list::end() { iterator itr = {FLastElement}; return itr; } bool list::iterator::operator!=(const iterator &itr) const { return itr.FElement != FElement; } void list::iterator::operator++(int) { FElement = FElement->NextAd; } void list::iterator::operator--(int) { FElement = FElement->PrevAd; } int &list::iterator::operator*() { return FElement->Value; } void Command(list &X, std::string &c) { if (c == "insert") { int d; std::cin >> d; X.push_front(d); } else if (c == "delete") { int d; std::cin >> d; list::iterator itr = X.begin(); while (*itr != d && itr != X.end()) { itr++; } if (itr != X.end()) { X.erase(itr); } } else if (c == "deleteFirst") { X.pop_front(); } else if (c == "deleteLast") { X.pop_back(); } } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int n; std::cin >> n; list X; for (int i = 0; i < n; i++) { std::string c; std::cin >> c; Command(X, c); } list::iterator it = X.begin(); list::iterator end = X.end(); end--; while (it != end) { std::cout << *it << ' '; it++; } std::cout << *end << std::endl; return 0; }
replace
78
79
78
83
0
p02265
C++
Time Limit Exceeded
#include <cstdlib> #include <iostream> #include <string> using namespace std; int N; struct List { struct ListI { ListI *next; ListI *prev; int value; ListI(ListI *n, ListI *p, int v) : next(n), prev(p), value(v) {} void *operator new(size_t, void *p) { return p; } static ListI *create(ListI *n, ListI *p, int v) { return new (malloc(sizeof(ListI))) ListI(n, p, v); } }; ListI *first; ListI *last; List() : first(0), last(0) {} void insert(int x) { if (first == 0) { first = last = ListI::create(0, 0, x); } else { ListI *of = first; ListI *nf = ListI::create(of, 0, x); of->prev = nf; first = nf; } } void remove(int x) { if (first == 0) { return; } if (first == last && first->value == x) { free(first); first = last = 0; return; } if (first->value == x) { deleteFirst(); return; } if (first->next != 0) { for (ListI *i = first->next; i != last; i = i->next) { if (i->value == x) { ListI *p = i->prev; ListI *n = i->next; p->next = n; n->prev = p; free(i); return; } } } if (last->value == x) { deleteLast(); } } void deleteFirst() { if (first == 0) { return; } if (first == last) { free(first); first = last = 0; return; } ListI *f = first; first = first->next; first->prev = 0; free(f); } void deleteLast() { if (last == 0) { return; } if (first == last) { free(last); first = last = 0; return; } ListI *l = last; last = last->prev; last->next = 0; free(l); } void print() { if (first == 0) { cout << endl; return; } for (ListI *i = first; i != last; i = i->next) { cout << i->value << " "; } cout << last->value << endl; } }; int main() { cin >> N; List lis; for (int i = 0; i < N; i++) { string mne; cin >> mne; if (mne == "insert") { int val; cin >> val; lis.insert(val); } else if (mne == "delete") { int val; cin >> val; lis.remove(val); } else if (mne == "deleteFirst") { lis.deleteFirst(); } else if (mne == "deleteLast") { lis.deleteLast(); } } lis.print(); return 0; }
#include <cstdlib> #include <iostream> #include <string> using namespace std; int N; struct List { struct ListI { ListI *next; ListI *prev; int value; ListI(ListI *n, ListI *p, int v) : next(n), prev(p), value(v) {} void *operator new(size_t, void *p) { return p; } static ListI *create(ListI *n, ListI *p, int v) { return new (malloc(sizeof(ListI))) ListI(n, p, v); } }; ListI *first; ListI *last; List() : first(0), last(0) {} void insert(int x) { if (first == 0) { first = last = ListI::create(0, 0, x); } else { ListI *of = first; ListI *nf = ListI::create(of, 0, x); of->prev = nf; first = nf; } } void remove(int x) { if (first == 0) { return; } if (first == last && first->value == x) { free(first); first = last = 0; return; } if (first->value == x) { deleteFirst(); return; } if (first->next != 0) { for (ListI *i = first->next; i != last; i = i->next) { if (i->value == x) { ListI *p = i->prev; ListI *n = i->next; p->next = n; n->prev = p; free(i); return; } } } if (last->value == x) { deleteLast(); } } void deleteFirst() { if (first == 0) { return; } if (first == last) { free(first); first = last = 0; return; } ListI *f = first; first = first->next; first->prev = 0; free(f); } void deleteLast() { if (last == 0) { return; } if (first == last) { free(last); first = last = 0; return; } ListI *l = last; last = last->prev; last->next = 0; free(l); } void print() { if (first == 0) { cout << endl; return; } for (ListI *i = first; i != last; i = i->next) { cout << i->value << " "; } cout << last->value << endl; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; List lis; for (int i = 0; i < N; i++) { string mne; cin >> mne; if (mne == "insert") { int val; cin >> val; lis.insert(val); } else if (mne == "delete") { int val; cin >> val; lis.remove(val); } else if (mne == "deleteFirst") { lis.deleteFirst(); } else if (mne == "deleteLast") { lis.deleteLast(); } } lis.print(); return 0; }
insert
117
117
117
120
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int len = 1000050; inline int prev(int pos) { return (pos == 0) ? len - 1 : pos - 1; } inline int next(int pos) { return (pos == len - 1) ? 0 : pos + 1; } int main() { int n; cin >> n; vector<int> list(len); int first = 0, last = 0; string op; int key; for (int i = 0; i < n; i++) { cin >> op; if (op == "insert") { cin >> key; first = prev(first); list[first] = key; } else if (op == "deleteFirst") { while (list[first] == -1) { first = next(first); } first = next(first); } else if (op == "deleteLast") { while (list[prev(last)] == -1) { last = prev(last); } last = prev(last); } else { cin >> key; for (int j = first; j != last; j = next(j)) { if (list[j] == key) { list[j] = -1; break; } } } } bool nospace = true; for (int i = first; i != last; i = next(i)) { if (list[i] == -1) continue; if (nospace) { cout << list[i]; nospace = false; } else { cout << " " << list[i]; } } cout << endl; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int len = 1000050; inline int prev(int pos) { return (pos == 0) ? len - 1 : pos - 1; } inline int next(int pos) { return (pos == len - 1) ? 0 : pos + 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> list(len); int first = 0, last = 0; string op; int key; for (int i = 0; i < n; i++) { cin >> op; if (op == "insert") { cin >> key; first = prev(first); list[first] = key; } else if (op == "deleteFirst") { while (list[first] == -1) { first = next(first); } first = next(first); } else if (op == "deleteLast") { while (list[prev(last)] == -1) { last = prev(last); } last = prev(last); } else { cin >> key; for (int j = first; j != last; j = next(j)) { if (list[j] == key) { list[j] = -1; break; } } } } bool nospace = true; for (int i = first; i != last; i = next(i)) { if (list[i] == -1) continue; if (nospace) { cout << list[i]; nospace = false; } else { cout << " " << list[i]; } } cout << endl; }
insert
13
13
13
15
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); string str; list<int> data; list<int>::iterator it, l; int n, x; cin >> n; while (n--) { cin >> str; if (str == "insert") { cin >> x; data.push_front(x); } else if (str == "delete") { cin >> x; data.erase(find(data.begin(), data.end(), x)); } else if (str == "deleteFirst") data.pop_front(); else data.pop_back(); } for (it = data.begin(), l = --data.end(); it != l; ++it) { cout << *it << " "; } cout << *it << endl; }
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); string str; list<int> data; list<int>::iterator it, l; int n, x; cin >> n; while (n--) { cin >> str; if (str == "insert") { cin >> x; data.push_front(x); } else if (str == "delete") { cin >> x; it = find(data.begin(), data.end(), x); if (it != data.end()) data.erase(it); } else if (str == "deleteFirst") data.pop_front(); else data.pop_back(); } for (it = data.begin(), l = --data.end(); it != l; ++it) { cout << *it << " "; } cout << *it << endl; }
replace
21
22
21
24
0
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <cstring> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *x = (Node *)malloc(sizeof(Node)); x->key = key; x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } Node *listsearch(int key) { Node *cur = nil->next; while (cur != NULL && cur->key != key) { cur = cur->next; } return cur; } void deleteNode(Node *t) { if (t == nil) { return; } t->next->prev = t->prev; t->prev->next = t->next; free(t); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } void deleteKey(int key) { deleteNode(listsearch(key)); } void print() { int j = 0; Node *cur = nil->next; while (1) { if (cur == nil) { break; } if (j++ > 0) { printf(" "); } printf("%d", cur->key); cur = cur->next; } putchar('\n'); } int main() { char com[20]; int n, i, key; init(); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s %d", com, &key); if (com[0] == 'i') { insert(key); } else { if (com[6] == 'L') { deleteLast(); } else if (com[6] == 'F') { deleteFirst(); } else { deleteKey(key); } } } print(); return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *x = (Node *)malloc(sizeof(Node)); x->key = key; x->next = nil->next; nil->next->prev = x; nil->next = x; x->prev = nil; } Node *listsearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) { cur = cur->next; } return cur; } void deleteNode(Node *t) { if (t == nil) { return; } t->next->prev = t->prev; t->prev->next = t->next; free(t); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } void deleteKey(int key) { deleteNode(listsearch(key)); } void print() { int j = 0; Node *cur = nil->next; while (1) { if (cur == nil) { break; } if (j++ > 0) { printf(" "); } printf("%d", cur->key); cur = cur->next; } putchar('\n'); } int main() { char com[20]; int n, i, key; init(); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s %d", com, &key); if (com[0] == 'i') { insert(key); } else { if (com[6] == 'L') { deleteLast(); } else if (com[6] == 'F') { deleteFirst(); } else { deleteKey(key); } } } print(); return 0; }
replace
30
31
30
31
TLE
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <cstring> struct Node { int key; Node *next, *prev; }; Node *nil; 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"); } Node *listSearch(int key) { Node *cur = nil->next; while (cur->key != key) cur = cur->next; return cur; } 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 *x = (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; char com[20]; scanf("%d", &n); init(); for (i = 0; i < n; i++) { scanf("%s%d", com, &key); if (com[0] == 'i') insert(key); else if (strlen(com) == 6) deleteKey(key); else if (com[6] == 'F') deleteFirst(); else deleteLast(); } printList(); return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> struct Node { int key; Node *next, *prev; }; Node *nil; 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"); } Node *listSearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) cur = cur->next; return cur; } 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 *x = (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; char com[20]; scanf("%d", &n); init(); for (i = 0; i < n; i++) { scanf("%s%d", com, &key); if (com[0] == 'i') insert(key); else if (strlen(com) == 6) deleteKey(key); else if (com[6] == 'F') deleteFirst(); else deleteLast(); } printList(); return 0; }
replace
32
33
32
33
TLE
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; signed main() { cin.tie(0); int n; cin >> n; list<int> l; for (int i = 0; i < n; i++) { string s; int x; cin >> s; if (s == "insert" || s == "delete") cin >> x; if (s == "deleteFirst") { l.pop_front(); continue; } if (s == "deleteLast") { l.pop_back(); continue; } if (s == "insert") { l.push_front(x); continue; } if (s == "delete") { auto it = find(l.begin(), l.end(), x); if (it == l.end()) continue; l.erase(it); continue; } } bool f = 0; for (auto it = l.begin(); it != l.end(); ++it) { if (f) cout << " "; cout << *it; f = 1; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; list<int> l; for (int i = 0; i < n; i++) { string s; int x; cin >> s; if (s == "insert" || s == "delete") cin >> x; if (s == "deleteFirst") { l.pop_front(); continue; } if (s == "deleteLast") { l.pop_back(); continue; } if (s == "insert") { l.push_front(x); continue; } if (s == "delete") { auto it = find(l.begin(), l.end(), x); if (it == l.end()) continue; l.erase(it); continue; } } bool f = 0; for (auto it = l.begin(); it != l.end(); ++it) { if (f) cout << " "; cout << *it; f = 1; } cout << endl; return 0; }
insert
3
3
3
4
TLE
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <cstring> #include <iostream> struct list { int val; list *next; list *prev; }; list *head; void init() { head = new list; head->next = head; head->prev = head; } int main() { using namespace std; int len, key; cin >> len; char str[12]; init(); for (int i = 0; i < len; i++) { scanf("%s", str); if (str[0] == 'i') { list *add = new list; scanf("%d", &key); add->val = key; add->next = head->next; head->next->prev = add; head->next = add; add->prev = head; } else if (strlen(str) > 6 && str[6] == 'F') { if (head->next != head) { head->next->next->prev = head; list *minus = head->next; head->next = head->next->next; delete minus; } } else if (strlen(str) > 6 && str[6] == 'L') { if (head->prev != head) { list *minus = head->prev; minus->prev->next = minus->next; minus->next->prev = minus->prev; delete minus; } } else { scanf("%d", &key); list *search = head->next; while (search != head && search->val != key) { search = search->next; } search->prev->next = search->next; search->next->prev = search->prev; delete search; } } list *show = head->next; int isf = 0; while (1) { if (show == head) break; if (isf++ > 0) printf(" "); printf("%d", show->val); show = show->next; delete show->prev; } printf("\n"); delete head; return 0; }
#include <cstdio> #include <cstring> #include <iostream> struct list { int val; list *next; list *prev; }; list *head; void init() { head = new list; head->next = head; head->prev = head; } int main() { using namespace std; int len, key; cin >> len; char str[12]; init(); for (int i = 0; i < len; i++) { scanf("%s", str); if (str[0] == 'i') { list *add = new list; scanf("%d", &key); add->val = key; add->next = head->next; head->next->prev = add; head->next = add; add->prev = head; } else if (strlen(str) > 6 && str[6] == 'F') { if (head->next != head) { head->next->next->prev = head; list *minus = head->next; head->next = head->next->next; delete minus; } } else if (strlen(str) > 6 && str[6] == 'L') { if (head->prev != head) { list *minus = head->prev; minus->prev->next = minus->next; minus->next->prev = minus->prev; delete minus; } } else { scanf("%d", &key); list *search = head->next; while (search != head && search->val != key) { search = search->next; } if (search == head) continue; search->prev->next = search->next; search->next->prev = search->prev; delete search; } } list *show = head->next; int isf = 0; while (1) { if (show == head) break; if (isf++ > 0) printf(" "); printf("%d", show->val); show = show->next; delete show->prev; } printf("\n"); delete head; return 0; }
insert
50
50
50
52
TLE
p02265
C++
Time Limit Exceeded
#include <cstdlib> #include <cstring> #include <iostream> #include <stdio.h> using namespace std; struct Node { int key; Node *pre; Node *next; }; Node *nil; void init() { nil = new Node; nil->pre = nil; nil->next = nil; } void insert(int x) { Node *add = new Node; add->key = x; add->next = nil->next; nil->next->pre = add; nil->next = add; add->pre = nil; add->next; } void deleteNode(Node *d) { if (d == nil) return; Node *p = d->pre; Node *n = d->next; p->next = n; n->pre = p; free(d); } void deleteOne(int x) { Node *i = nil->next; while (i->key != x) i = i->next; deleteNode(i); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->pre); } int main() { init(); int len; cin >> len; for (int i = 0; i < len; i++) { char op[20]; int x; scanf("%s", op); if (op[0] == 'i') { scanf("%d", &x); insert(x); } else if (op[0] == 'd') { if (strlen(op) <= 6) { scanf("%d", &x); deleteOne(x); } else if (op[6] == 'F') deleteFirst(); else deleteLast(); } } Node *item = nil->next; while (nil->pre != item) { printf("%d ", item->key); item = item->next; } printf("%d\n", item->key); return 0; }
#include <cstdlib> #include <cstring> #include <iostream> #include <stdio.h> using namespace std; struct Node { int key; Node *pre; Node *next; }; Node *nil; void init() { nil = new Node; nil->pre = nil; nil->next = nil; } void insert(int x) { Node *add = new Node; add->key = x; add->next = nil->next; nil->next->pre = add; nil->next = add; add->pre = nil; add->next; } void deleteNode(Node *d) { if (d == nil) return; Node *p = d->pre; Node *n = d->next; p->next = n; n->pre = p; free(d); } void deleteOne(int x) { Node *i = nil->next; while (i != nil && i->key != x) i = i->next; deleteNode(i); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->pre); } int main() { init(); int len; cin >> len; for (int i = 0; i < len; i++) { char op[20]; int x; scanf("%s", op); if (op[0] == 'i') { scanf("%d", &x); insert(x); } else if (op[0] == 'd') { if (strlen(op) <= 6) { scanf("%d", &x); deleteOne(x); } else if (op[6] == 'F') deleteFirst(); else deleteLast(); } } Node *item = nil->next; while (nil->pre != item) { printf("%d ", item->key); item = item->next; } printf("%d\n", item->key); return 0; }
replace
36
37
36
37
TLE
p02265
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { list<int> l; int n; string cmd; int x; cin >> n; for (int i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> x; l.push_front(x); } else if (cmd == "delete") { cin >> x; l.erase(find(begin(l), end(l), x)); } else if (cmd == "deleteFirst") { l.pop_front(); } else { l.pop_back(); } } for (auto it = l.begin(); it != l.end();) { cout << *it; it++; if (it == l.end()) { cout << endl; } else { cout << ' '; } } }
#include <bits/stdc++.h> using namespace std; int main() { list<int> l; int n; string cmd; int x; cin >> n; for (int i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> x; l.push_front(x); } else if (cmd == "delete") { cin >> x; auto it = find(begin(l), end(l), x); if (it != l.end()) { l.erase(it); } } else if (cmd == "deleteFirst") { l.pop_front(); } else { l.pop_back(); } } for (auto it = l.begin(); it != l.end();) { cout << *it; it++; if (it == l.end()) { cout << endl; } else { cout << ' '; } } }
replace
17
18
17
21
0
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; int main() { int n; cin >> n; list<int> l; for (int i = 0; i < n; i++) { string command; cin >> command; if (command == "insert") { int x; cin >> x; l.push_front(x); } else if (command == "delete") { int x; cin >> x; for (list<int>::iterator i = l.begin(); i != l.end(); i++) { if (*i == x) { l.erase(i); break; } } } else if (command == "deleteFirst") { l.pop_front(); } else if (command == "deleteLast") { l.pop_back(); } } list<int>::iterator i; for (i = l.begin(); i != --l.end(); i++) { cout << *i << " "; } cout << *i << endl; return 0; }
#include <iostream> #include <list> #include <string> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; list<int> l; for (int i = 0; i < n; i++) { string command; cin >> command; if (command == "insert") { int x; cin >> x; l.push_front(x); } else if (command == "delete") { int x; cin >> x; for (list<int>::iterator i = l.begin(); i != l.end(); i++) { if (*i == x) { l.erase(i); break; } } } else if (command == "deleteFirst") { l.pop_front(); } else if (command == "deleteLast") { l.pop_back(); } } list<int>::iterator i; for (i = l.begin(); i != --l.end(); i++) { cout << *i << " "; } cout << *i << endl; return 0; }
insert
7
7
7
10
TLE
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <deque> #include <iostream> #include <string> using namespace std; int main() { int n, i, x; string s; deque<int> intlist; cin >> n; for (i = 0; i <= n - 1; i++) { cin >> s; if (s == "insert") { cin >> x; intlist.push_front(x); } else if (s == "delete") { cin >> x; deque<int>::iterator it = intlist.begin(); while (1) { if (*it == x) { it = intlist.erase(it); break; } else if (it == intlist.end()) { break; } it++; } } else if (s == "deleteFirst") { intlist.pop_front(); } else if (s == "deleteLast") { intlist.pop_back(); } } deque<int>::iterator it = intlist.begin(); cout << *it; it++; while (1) { if (it == intlist.end()) { cout << "\n"; break; } cout << " " << *it; it++; } return 0; }
#include <cstdio> #include <deque> #include <iostream> #include <string> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, i, x; string s; deque<int> intlist; cin >> n; for (i = 0; i <= n - 1; i++) { cin >> s; if (s == "insert") { cin >> x; intlist.push_front(x); } else if (s == "delete") { cin >> x; deque<int>::iterator it = intlist.begin(); while (1) { if (*it == x) { it = intlist.erase(it); break; } else if (it == intlist.end()) { break; } it++; } } else if (s == "deleteFirst") { intlist.pop_front(); } else if (s == "deleteLast") { intlist.pop_back(); } } deque<int>::iterator it = intlist.begin(); cout << *it; it++; while (1) { if (it == intlist.end()) { cout << "\n"; break; } cout << " " << *it; it++; } return 0; }
insert
7
7
7
9
TLE
p02265
C++
Runtime Error
/** * ???????????£???????????? ??\?????????????????????????????????????????£???????????????????£?????????????????????? insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶??????? delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´???????????????????????? deleteFirst: ?????????????????????????´??????????????????? deleteLast: ????????????????°????????´??????????????????? ??\??? ??\????????\????????¢?????§????????????????????? n command1 command2 ... commandn ???????????????????????° n ?????????????????????????¶???? n ??????????????????????????????????????????????¨?4?????????????????????????????§?????????????????´??°??¨???????????? ?????? ??¨??????????????????????????????????????£?????????????????????????????????????????????????????????????????£?¶???????????????????????????????????????§????????£?????????????????????????????? ??¶?´? ????????°??? 2,000,000 ????¶????????????? delete ??????????????°??? 20 ????¶????????????? 0 ??? ???????????? ??? 10^9??? ?????????????¨???§????????????????´???°??? 106????¶????????????? delete, deleteFirst, ????????? deleteLast ??????????????????????????¨?????????????????????????????\??????????´?????????¨????????? ??\?????? 1 7 insert 5 insert 2 insert 3 insert 1 delete 3 insert 6 delete 5 ????????? 1 6 1 2 ??\?????? 2 9 insert 5 insert 2 insert 3 insert 1 delete 3 insert 6 delete 5 deleteFirst deleteLast ????????? 2 1 */ #include <algorithm> #include <iostream> #include <iterator> #include <list> #include <sstream> void printList(const std::list<unsigned int> &l) { std::ostringstream oss; std::copy(l.begin(), --l.end(), std::ostream_iterator<unsigned int>(oss, " ")); oss << l.back(); std::cout << oss.str() << std::endl; } int main(int argc, char const *argv[]) { std::string Param; std::istringstream iss; std::list<unsigned int> List; unsigned int n; std::string Command; unsigned int x; getline(std::cin, Param); iss.str(Param); iss >> n; iss.clear(); for (size_t i = 0; i < n; i++) { getline(std::cin, Param); iss.str(Param); iss >> Command >> x; iss.clear(); if (Command == "insert") { List.push_front(x); } else if (Command == "delete") { auto itr = std::find(List.begin(), List.end(), x); List.erase(itr); } else if (Command == "deleteFirst") { List.pop_front(); } else if (Command == "deleteLast") { List.pop_back(); } } printList(List); return 0; }
/** * ???????????£???????????? ??\?????????????????????????????????????????£???????????????????£?????????????????????? insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶??????? delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´???????????????????????? deleteFirst: ?????????????????????????´??????????????????? deleteLast: ????????????????°????????´??????????????????? ??\??? ??\????????\????????¢?????§????????????????????? n command1 command2 ... commandn ???????????????????????° n ?????????????????????????¶???? n ??????????????????????????????????????????????¨?4?????????????????????????????§?????????????????´??°??¨???????????? ?????? ??¨??????????????????????????????????????£?????????????????????????????????????????????????????????????????£?¶???????????????????????????????????????§????????£?????????????????????????????? ??¶?´? ????????°??? 2,000,000 ????¶????????????? delete ??????????????°??? 20 ????¶????????????? 0 ??? ???????????? ??? 10^9??? ?????????????¨???§????????????????´???°??? 106????¶????????????? delete, deleteFirst, ????????? deleteLast ??????????????????????????¨?????????????????????????????\??????????´?????????¨????????? ??\?????? 1 7 insert 5 insert 2 insert 3 insert 1 delete 3 insert 6 delete 5 ????????? 1 6 1 2 ??\?????? 2 9 insert 5 insert 2 insert 3 insert 1 delete 3 insert 6 delete 5 deleteFirst deleteLast ????????? 2 1 */ #include <algorithm> #include <iostream> #include <iterator> #include <list> #include <sstream> void printList(const std::list<unsigned int> &l) { std::ostringstream oss; std::copy(l.begin(), --l.end(), std::ostream_iterator<unsigned int>(oss, " ")); oss << l.back(); std::cout << oss.str() << std::endl; } int main(int argc, char const *argv[]) { std::string Param; std::istringstream iss; std::list<unsigned int> List; unsigned int n; std::string Command; unsigned int x; getline(std::cin, Param); iss.str(Param); iss >> n; iss.clear(); for (size_t i = 0; i < n; i++) { getline(std::cin, Param); iss.str(Param); iss >> Command >> x; iss.clear(); if (Command == "insert") { List.push_front(x); } else if (Command == "delete") { auto itr = std::find(List.begin(), List.end(), x); if (itr != List.end()) { List.erase(itr); } } else if (Command == "deleteFirst") { List.pop_front(); } else if (Command == "deleteLast") { List.pop_back(); } } printList(List); return 0; }
replace
91
92
91
94
0
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { int n; cin >> n; list<int> l; string s; int x; for (int i = 0; i < n; ++i) { cin >> s; if (s == "insert") { cin >> x; l.push_front(x); } else if (s == "delete") { cin >> x; list<int>::iterator it = find(l.begin(), l.end(), x); if (it != l.end()) { l.erase(it); } } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } } for (list<int>::iterator it = l.begin(); it != l.end(); ++it) { if (it != l.begin()) cout << ' '; cout << *it; } cout << endl; }
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; list<int> l; string s; int x; for (int i = 0; i < n; ++i) { cin >> s; if (s == "insert") { cin >> x; l.push_front(x); } else if (s == "delete") { cin >> x; list<int>::iterator it = find(l.begin(), l.end(), x); if (it != l.end()) { l.erase(it); } } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } } for (list<int>::iterator it = l.begin(); it != l.end(); ++it) { if (it != l.begin()) cout << ' '; cout << *it; } cout << endl; }
insert
7
7
7
8
TLE
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; struct lis { int d; lis *pr; lis *ne; }; lis he; int n; void ins(); void del(); void delf(); void dell(); int main() { cin >> n; string ord; lis bu = {-1, &he, &he}; he = bu; for (int i = 0; i < n; i++) { cin >> ord; if (ord == "insert") ins(); else if (ord == "delete") del(); else if (ord == "deleteFirst") delf(); else if (ord == "deleteLast") dell(); } for (lis *i = he.ne; i != &he; i = i->ne) { cout << i->d; if (i->ne != &he) { cout << " "; } } cout << endl; return 0; } void ins() { int x; cin >> x; lis *p; p = new lis; p->d = x; p->ne = he.ne; p->pr = &he; he.ne->pr = p; he.ne = p; } void del() { int x; cin >> x; for (lis *i = he.ne; i != &he; i = i->ne) { if (i->d == x) { i->pr->ne = i->ne; i->ne->pr = i->pr; free(i); return; } } } void dell() { for (lis *i = he.ne; i != &he; i = i->ne) { if (i->ne == &he) { i->pr->ne = i->ne; i->ne->pr = i->pr; free(i); } } } void delf() { lis *i = he.ne; i->pr->ne = i->ne; i->ne->pr = i->pr; free(i); }
#include <bits/stdc++.h> using namespace std; struct lis { int d; lis *pr; lis *ne; }; lis he; int n; void ins(); void del(); void delf(); void dell(); int main() { cin >> n; string ord; lis bu = {-1, &he, &he}; he = bu; for (int i = 0; i < n; i++) { cin >> ord; if (ord == "insert") ins(); else if (ord == "delete") del(); else if (ord == "deleteFirst") delf(); else if (ord == "deleteLast") dell(); } for (lis *i = he.ne; i != &he; i = i->ne) { cout << i->d; if (i->ne != &he) { cout << " "; } } cout << endl; return 0; } void ins() { int x; cin >> x; lis *p; p = new lis; p->d = x; p->ne = he.ne; p->pr = &he; he.ne->pr = p; he.ne = p; } void del() { int x; cin >> x; for (lis *i = he.ne; i != &he; i = i->ne) { if (i->d == x) { i->pr->ne = i->ne; i->ne->pr = i->pr; free(i); return; } } } void dell() { lis *i = he.pr; i->pr->ne = i->ne; i->ne->pr = i->pr; free(i); } void delf() { lis *i = he.ne; i->pr->ne = i->ne; i->ne->pr = i->pr; free(i); }
replace
64
71
64
68
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; int main() { int n, x; string str; list<int> lst; list<int>::iterator it; cin >> n; while (n--) { cin >> str; if (str == "insert") { cin >> x; lst.push_front(x); } else if (str == "deleteFirst") lst.pop_front(); else if (str == "deleteLast") lst.pop_back(); else { cin >> x; for (it = lst.begin(); it != lst.end(); ++it) { if (*it == x) { lst.erase(it); break; } } } } for (it = lst.begin(); it != --lst.end(); ++it) { cout << *it << " "; } cout << *it << endl; return 0; }
#include <iostream> #include <list> #include <string> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, x; string str; list<int> lst; list<int>::iterator it; cin >> n; while (n--) { cin >> str; if (str == "insert") { cin >> x; lst.push_front(x); } else if (str == "deleteFirst") lst.pop_front(); else if (str == "deleteLast") lst.pop_back(); else { cin >> x; for (it = lst.begin(); it != lst.end(); ++it) { if (*it == x) { lst.erase(it); break; } } } } for (it = lst.begin(); it != --lst.end(); ++it) { cout << *it << " "; } cout << *it << endl; return 0; }
insert
5
5
5
7
TLE
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct DLL { ll x; DLL *next; DLL *prev; }; int main(void) { int n; cin >> n; DLL *head; head = (DLL *)malloc(sizeof(DLL)); head->x = -1; head->next = head; head->prev = head; for (int i = 0; i < n; i++) { string comm; ll v; cin >> comm; if (comm == "insert") { cin >> v; DLL *x; x = (DLL *)malloc(sizeof(DLL)); x->x = v; x->next = head->next; head->next->prev = x; head->next = x; x->prev = head; } if (comm == "delete") { cin >> v; DLL *key; key = head->next; while (1) { if (key == head) break; if (key->x == v) { key->prev->next = key->next; key->next->prev = key->prev; free(key); break; } key = key->next; } } if (comm == "deleteFirst") { DLL *key = head->next; key->prev->next = key->next; key->next->prev = key->prev; free(key); } if (comm == "deleteLast") { DLL *key = head->prev; key->prev->next = key->next; key->next->prev = key->prev; free(key); } } DLL *a; a = head->next; while (a != head) { if (a->next == head) cout << a->x << endl; else cout << a->x << " "; a = a->next; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct DLL { ll x; DLL *next; DLL *prev; }; int main(void) { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; DLL *head; head = (DLL *)malloc(sizeof(DLL)); head->x = -1; head->next = head; head->prev = head; for (int i = 0; i < n; i++) { string comm; ll v; cin >> comm; if (comm == "insert") { cin >> v; DLL *x; x = (DLL *)malloc(sizeof(DLL)); x->x = v; x->next = head->next; head->next->prev = x; head->next = x; x->prev = head; } if (comm == "delete") { cin >> v; DLL *key; key = head->next; while (1) { if (key == head) break; if (key->x == v) { key->prev->next = key->next; key->next->prev = key->prev; free(key); break; } key = key->next; } } if (comm == "deleteFirst") { DLL *key = head->next; key->prev->next = key->next; key->next->prev = key->prev; free(key); } if (comm == "deleteLast") { DLL *key = head->prev; key->prev->next = key->next; key->next->prev = key->prev; free(key); } } DLL *a; a = head->next; while (a != head) { if (a->next == head) cout << a->x << endl; else cout << a->x << " "; a = a->next; } return 0; }
insert
11
11
11
13
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <string> using namespace std; typedef struct n { int Key; n *prev, *next; } Node; Node *nil; const string COMMAND[4] = {"insert", "deleteFirst", "deleteLast", "delete"}; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void intsert(int Key) { Node *x; x = (Node *)malloc(sizeof(Node)); x->Key = Key; x->next = nil->next; nil->next->prev = x; x->prev = nil; nil->next = x; } void deleteNode(Node *t) { t->next->prev = t->prev; t->prev->next = t->next; free(t); } void deletefirst() { deleteNode(nil->next); } void deletelast() { deleteNode(nil->prev); } Node *SearchNode(int Key) { Node *ser; ser = nil->next; while (ser != nil && ser->Key != Key) { ser = ser->next; } return ser; } void delete_X(int Key) { deleteNode(SearchNode(Key)); } void ReadCommand() { char comm[16]; scanf("%s", comm); if (comm == COMMAND[0]) { int Key; scanf("%d", &Key); intsert(Key); } else if (comm == COMMAND[1]) { deletefirst(); } else if (comm == COMMAND[2]) { deletelast(); } else { int Key; cin >> Key; deleteNode(SearchNode(Key)); } } void printNode() { Node *k; k = nil->next; while (k != nil) { if (k->next != nil) { printf("%d ", k->Key); } else { printf("%d\n", k->Key); } k = k->next; } } int main() { init(); int N; cin >> N; for (int i = 0; i < N; i++) ReadCommand(); printNode(); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <string> using namespace std; typedef struct n { int Key; n *prev, *next; } Node; Node *nil; const string COMMAND[4] = {"insert", "deleteFirst", "deleteLast", "delete"}; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void intsert(int Key) { Node *x; x = (Node *)malloc(sizeof(Node)); x->Key = Key; x->next = nil->next; nil->next->prev = x; x->prev = nil; nil->next = x; } void deleteNode(Node *t) { if (t == nil) return; t->next->prev = t->prev; t->prev->next = t->next; free(t); } void deletefirst() { deleteNode(nil->next); } void deletelast() { deleteNode(nil->prev); } Node *SearchNode(int Key) { Node *ser; ser = nil->next; while (ser != nil && ser->Key != Key) { ser = ser->next; } return ser; } void delete_X(int Key) { deleteNode(SearchNode(Key)); } void ReadCommand() { char comm[16]; scanf("%s", comm); if (comm == COMMAND[0]) { int Key; scanf("%d", &Key); intsert(Key); } else if (comm == COMMAND[1]) { deletefirst(); } else if (comm == COMMAND[2]) { deletelast(); } else { int Key; cin >> Key; deleteNode(SearchNode(Key)); } } void printNode() { Node *k; k = nil->next; while (k != nil) { if (k->next != nil) { printf("%d ", k->Key); } else { printf("%d\n", k->Key); } k = k->next; } } int main() { init(); int N; cin >> N; for (int i = 0; i < N; i++) ReadCommand(); printNode(); return 0; }
insert
32
32
32
34
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <iostream> #include <list> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; int n; list<int> x; int main() { ios_base::sync_with_stdio(false); cin >> n; rep(i, n) { string a; cin >> a; if (a == "deleteFirst") { x.pop_front(); } else if (a == "deleteLast") { x.pop_back(); } else { int b; cin >> b; if (a == "insert") { x.push_front(b); } else { x.erase(find(x.begin(), x.end(), b)); } } } for (list<int>::iterator it = x.begin(); it != x.end(); ++it) { if (it != x.begin()) { cout << ' '; } cout << *it; } cout << endl; return 0; }
#include <algorithm> #include <iostream> #include <list> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; int n; list<int> x; int main() { ios_base::sync_with_stdio(false); cin >> n; rep(i, n) { string a; cin >> a; if (a == "deleteFirst") { x.pop_front(); } else if (a == "deleteLast") { x.pop_back(); } else { int b; cin >> b; if (a == "insert") { x.push_front(b); } else { list<int>::iterator it = find(x.begin(), x.end(), b); if (it != x.end()) { x.erase(it); } } } } for (list<int>::iterator it = x.begin(); it != x.end(); ++it) { if (it != x.begin()) { cout << ' '; } cout << *it; } cout << endl; return 0; }
replace
27
28
27
31
0
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> #include <string> using namespace std; void display(list<int> &lst) { if (!lst.empty()) { cout << lst.front(); lst.pop_front(); } for (auto element : lst) { cout << ' ' << element; } cout << endl; } int main() { list<int> lst; int n = 0; cin >> n; for (int i = 0; i != n; ++i) { string command; cin >> command; if (command == "insert") { int x = 0; cin >> x; lst.push_front(x); } else if (command == "delete") { int x = 0; cin >> x; for (auto it = lst.begin(); it != lst.end(); ++it) { if (*it == x) { lst.erase(it); break; } } } else if (command == "deleteFirst") { lst.pop_front(); } else if (command == "deleteLast") { lst.pop_back(); } } display(lst); return 0; }
#include <iostream> #include <list> #include <string> using namespace std; void display(list<int> &lst) { if (!lst.empty()) { cout << lst.front(); lst.pop_front(); } for (auto element : lst) { cout << ' ' << element; } cout << endl; } int main() { ios::sync_with_stdio(false); list<int> lst; int n = 0; cin >> n; for (int i = 0; i != n; ++i) { string command; cin >> command; if (command == "insert") { int x = 0; cin >> x; lst.push_front(x); } else if (command == "delete") { int x = 0; cin >> x; for (auto it = lst.begin(); it != lst.end(); ++it) { if (*it == x) { lst.erase(it); break; } } } else if (command == "deleteFirst") { lst.pop_front(); } else if (command == "deleteLast") { lst.pop_back(); } } display(lst); return 0; }
insert
17
17
17
18
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <list> using namespace std; int main(void) { list<int> l; int n; string s; int d; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> d; l.push_front(d); } else if (s == "delete") { cin >> d; list<int>::iterator it = l.begin(); while (it != l.end()) { if (*it == d) { l.erase(it); break; } ++it; } } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } } list<int>::iterator it = l.begin(); while (it != l.end()) { if (it != l.begin()) cout << " "; cout << *it; ++it; } cout << endl; return 0; }
#include <iostream> #include <list> using namespace std; int main(void) { std::ios_base::sync_with_stdio(false); list<int> l; int n; string s; int d; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> d; l.push_front(d); } else if (s == "delete") { cin >> d; list<int>::iterator it = l.begin(); while (it != l.end()) { if (*it == d) { l.erase(it); break; } ++it; } } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } } list<int>::iterator it = l.begin(); while (it != l.end()) { if (it != l.begin()) cout << " "; cout << *it; ++it; } cout << endl; return 0; }
insert
5
5
5
6
TLE
p02265
C++
Time Limit Exceeded
#include <iomanip> #include <iostream> #include <list> #include <string> #define REP(i, n) for (int i = 0; i < n; ++i) using namespace std; int main() { int n; list<int> l; cin >> n; REP(i, n) { string s; cin >> s; if (s[0] == 'i') { int x; cin >> x; l.push_front(x); } else { if (s[6] == 'F') { l.pop_front(); } else if (s[6] == 'L') { l.pop_back(); } else { int x; cin >> x; for (auto itr = l.begin(); itr != l.end(); ++itr) { if (*itr == x) { l.erase(itr); break; } } } } } int i = 0; for (auto itr = l.begin(); itr != l.end(); ++itr) { if (i++) cout << " "; cout << *itr; } cout << endl; return 0; }
#include <iomanip> #include <iostream> #include <list> #include <string> #define REP(i, n) for (int i = 0; i < n; ++i) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; list<int> l; cin >> n; REP(i, n) { string s; cin >> s; if (s[0] == 'i') { int x; cin >> x; l.push_front(x); } else { if (s[6] == 'F') { l.pop_front(); } else if (s[6] == 'L') { l.pop_back(); } else { int x; cin >> x; for (auto itr = l.begin(); itr != l.end(); ++itr) { if (*itr == x) { l.erase(itr); break; } } } } } int i = 0; for (auto itr = l.begin(); itr != l.end(); ++itr) { if (i++) cout << " "; cout << *itr; } cout << endl; return 0; }
insert
10
10
10
12
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <cctype> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> #define int long long using namespace std; struct node { int key; node *prev; node *next; }; class doubly_linked { private: public: node *head; doubly_linked() { head = new node(); head->next = head; head->prev = head; } void insert(int x) { node *now = new node(); now->next = head->next; head->next->prev = now; head->next = now; now->prev = head; now->key = x; }; void delete2(int x) { node *now = head; while ((now->key != x) || (now = head)) now = now->next; if (now != head) { now->prev->next = now->next; now->next->prev = now->prev; } } void deleteFirst() { head->next = head->next->next; head->next->prev = head; } void deleteLast() { head->prev = head->prev->prev; head->prev->next = head; } void print() { node *now = head->next; bool fst = false; while (now != head) { if (fst) cout << " "; else fst = true; cout << now->key; now = now->next; } cout << endl; } }; signed main() { doubly_linked d; int n, v; string s; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> v; d.insert(v); } if (s == "delete") { cin >> v; d.delete2(v); } if (s == "deleteFirst") d.deleteFirst(); if (s == "deleteLast") d.deleteLast(); } d.print(); }
#include <algorithm> #include <cctype> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> #define int long long using namespace std; struct node { int key; node *prev; node *next; }; class doubly_linked { private: public: node *head; doubly_linked() { head = new node(); head->next = head; head->prev = head; } void insert(int x) { node *now = new node(); now->next = head->next; head->next->prev = now; head->next = now; now->prev = head; now->key = x; }; void delete2(int x) { node *now = head->next; while ((now->key != x) && (now != head)) now = now->next; if (now != head) { now->prev->next = now->next; now->next->prev = now->prev; } } void deleteFirst() { head->next = head->next->next; head->next->prev = head; } void deleteLast() { head->prev = head->prev->prev; head->prev->next = head; } void print() { node *now = head->next; bool fst = false; while (now != head) { if (fst) cout << " "; else fst = true; cout << now->key; now = now->next; } cout << endl; } }; signed main() { doubly_linked d; int n, v; string s; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> v; d.insert(v); } if (s == "delete") { cin >> v; d.delete2(v); } if (s == "deleteFirst") d.deleteFirst(); if (s == "deleteLast") d.deleteLast(); } d.print(); }
replace
38
40
38
40
TLE
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> using namespace std; typedef struct Node { int key; Node *prev, *next; } Node; Node *nil; void insert_cell(int key) { Node *x; x = new Node; x->key = key; x->prev = nil; x->next = nil->next; x->next->prev = x; nil->next = x; } void deleteNode(Node *t) { if (t != nil) { t->prev->next = t->next; t->next->prev = t->prev; t->prev = NULL; t->next = NULL; delete t; } } void delete_cell(int x) { Node *cell; cell = new Node; cell = nil->next; while (cell->key != x) cell = cell->next; deleteNode(cell); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } int main() { nil = new Node; nil->prev = nil; nil->next = nil; int n, k; scanf("%d", &n); for (int i = 0; i < n; i++) { char s[20], c; scanf("%s%d", s, &k); if (s[0] == 'i') { insert_cell(k); } else { if (s[6] == 'F') deleteFirst(); else if (s[6] == 'L') deleteLast(); else delete_cell(k); } } Node *p; p = new Node; p = nil->next; for (int i = 0; p != nil; i++, p = p->next) if (i) cout << " " << p->key; else cout << p->key; delete p; cout << endl; }
#include <cstdio> #include <iostream> using namespace std; typedef struct Node { int key; Node *prev, *next; } Node; Node *nil; void insert_cell(int key) { Node *x; x = new Node; x->key = key; x->prev = nil; x->next = nil->next; x->next->prev = x; nil->next = x; } void deleteNode(Node *t) { if (t != nil) { t->prev->next = t->next; t->next->prev = t->prev; t->prev = NULL; t->next = NULL; delete t; } } void delete_cell(int x) { Node *cell; cell = new Node; cell = nil->next; while (cell->key != x && cell != nil) cell = cell->next; deleteNode(cell); } void deleteFirst() { deleteNode(nil->next); } void deleteLast() { deleteNode(nil->prev); } int main() { nil = new Node; nil->prev = nil; nil->next = nil; int n, k; scanf("%d", &n); for (int i = 0; i < n; i++) { char s[20], c; scanf("%s%d", s, &k); if (s[0] == 'i') { insert_cell(k); } else { if (s[6] == 'F') deleteFirst(); else if (s[6] == 'L') deleteLast(); else delete_cell(k); } } Node *p; p = new Node; p = nil->next; for (int i = 0; p != nil; i++, p = p->next) if (i) cout << " " << p->key; else cout << p->key; delete p; cout << endl; }
replace
35
36
35
36
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { int n; cin >> n; list<int> l; string s; int x; while (n--) { cin >> s; if (s == "deleteFirst") { l.pop_front(); continue; } else if (s == "deleteLast") { l.pop_back(); continue; } cin >> x; if (s == "insert") l.push_front(x); else if (s == "delete") l.erase(find(l.begin(), l.end(), x)); } cout << l.front(), l.pop_front(); for (auto it = l.begin(); it != l.end(); ++it) cout << " " << *it; cout << endl; return 0; }
#include <algorithm> #include <iostream> #include <list> #include <string> using namespace std; int main() { int n; cin >> n; list<int> l; string s; int x; while (n--) { cin >> s; if (s == "deleteFirst") { l.pop_front(); continue; } else if (s == "deleteLast") { l.pop_back(); continue; } cin >> x; if (s == "insert") l.push_front(x); else if (s == "delete") { list<int>::iterator it = find(l.begin(), l.end(), x); if (it != l.end()) l.erase(it); } } cout << l.front(), l.pop_front(); for (auto it = l.begin(); it != l.end(); ++it) cout << " " << *it; cout << endl; return 0; }
replace
27
29
27
32
0
p02265
C++
Time Limit Exceeded
#include <cassert> #include <iostream> #include <list> #include <string> typedef std::list<int> IList; void input(IList &ilist); int output(const IList &vec); // IList calc(const PList& vec); int main() { IList result; input(result); // IList ovec = calc(ivec); output(result); return 0; } ///-------------------------------------------- void input(IList &ilist) { int n; std::cin >> n; // list.resize(n); for (int i = 0; i < n; i++) { std::string meirei; std::cin >> meirei; if (meirei == "deleteFirst") { ilist.pop_front(); // ilist.erase(ilist.begin()); } else if (meirei == "deleteLast") { ilist.pop_back(); } else { int value; std::cin >> value; if (meirei == "insert") { //??????????????? ilist.insert(ilist.begin(), value); // ilist.push_back(value); } else { //?????? for (auto itr = ilist.begin(); itr != ilist.end(); ++itr) { if (*itr == value) { ilist.erase(itr); break; } } } } } // output(ilist); } ///-------------------------------------------- int output(const IList &intlist) { for (auto itr = intlist.begin(); itr != intlist.end(); ++itr) { if (itr != (intlist.begin())) { std::cout << " "; } std::cout << *itr; // itr ????????????????´????????????????????????§ 1 ?????¨??? } std::cout << std::endl; return 0; } ///-------------------------------------------- ///@func calc ///@brif ??¨???? ///-------------------------------------------- /* IList calc(const PList& ilist) { IList ovec; // std::cout << "IList ovec;"<< std::endl; for(auto itr = ilist.begin(); itr != ilist.end(); ++itr){ if(itr->meirei == "insert"){ // std::cout << "ovec.insert"<< std::endl; ovec.insert(ovec.begin(), itr->value); }else if(itr->meirei == "delete"){ auto otr = ovec.begin(); for(; otr != ovec.end(); ++otr){ if(*otr == itr->value){ break; } } if(otr != ovec.end()){ ovec.erase(otr); // std::cout << "ovec.erase(otr)"<< std::endl; } }else if(itr->meirei == "deleteFirst"){ ovec.erase(ovec.begin()); // std::cout << "deleteFirst" << std::endl; }else if(itr->meirei == "deleteLast"){ ovec.pop_back(); // std::cout << "deleteLast" << std::endl; }else { assert(true); } // output(ovec); } return ovec; } */
#include <cassert> #include <iostream> #include <list> #include <string> typedef std::list<int> IList; void input(IList &ilist); int output(const IList &vec); // IList calc(const PList& vec); int main() { std::cin.tie(0); //??\?????????????????? std::ios::sync_with_stdio(false); IList result; input(result); // IList ovec = calc(ivec); output(result); return 0; } ///-------------------------------------------- void input(IList &ilist) { int n; std::cin >> n; // list.resize(n); for (int i = 0; i < n; i++) { std::string meirei; std::cin >> meirei; if (meirei == "deleteFirst") { ilist.pop_front(); // ilist.erase(ilist.begin()); } else if (meirei == "deleteLast") { ilist.pop_back(); } else { int value; std::cin >> value; if (meirei == "insert") { //??????????????? ilist.insert(ilist.begin(), value); // ilist.push_back(value); } else { //?????? for (auto itr = ilist.begin(); itr != ilist.end(); ++itr) { if (*itr == value) { ilist.erase(itr); break; } } } } } // output(ilist); } ///-------------------------------------------- int output(const IList &intlist) { for (auto itr = intlist.begin(); itr != intlist.end(); ++itr) { if (itr != (intlist.begin())) { std::cout << " "; } std::cout << *itr; // itr ????????????????´????????????????????????§ 1 ?????¨??? } std::cout << std::endl; return 0; } ///-------------------------------------------- ///@func calc ///@brif ??¨???? ///-------------------------------------------- /* IList calc(const PList& ilist) { IList ovec; // std::cout << "IList ovec;"<< std::endl; for(auto itr = ilist.begin(); itr != ilist.end(); ++itr){ if(itr->meirei == "insert"){ // std::cout << "ovec.insert"<< std::endl; ovec.insert(ovec.begin(), itr->value); }else if(itr->meirei == "delete"){ auto otr = ovec.begin(); for(; otr != ovec.end(); ++otr){ if(*otr == itr->value){ break; } } if(otr != ovec.end()){ ovec.erase(otr); // std::cout << "ovec.erase(otr)"<< std::endl; } }else if(itr->meirei == "deleteFirst"){ ovec.erase(ovec.begin()); // std::cout << "deleteFirst" << std::endl; }else if(itr->meirei == "deleteLast"){ ovec.pop_back(); // std::cout << "deleteLast" << std::endl; }else { assert(true); } // output(ovec); } return ovec; } */
insert
12
12
12
14
TLE
p02265
C++
Time Limit Exceeded
#include "string.h" #include <iostream> using namespace std; class Node { public: Node *prev; Node *next; int data; Node() { prev = 0; next = 0; data = 0; } }; class List { private: Node head; public: List() { head.prev = &head; head.next = &head; } int insertx(int x); int deletex(int x); int deleteFirst(); int deleteLast(); int print(); }; // ここに問題がある!! int List::insertx(int x) { Node *p = new Node(); if (p == 0) return -1; Node *q = head.next; Node *r = &head; p->prev = r; p->next = q; p->data = x; r->next = p; q->prev = p; return 0; } int List::deletex(int x) { for (Node *p = head.next; p != &head; p = p->next) { if (p->data == x) { Node *q = p->prev; Node *r = p->next; q->next = r; r->prev = q; delete p; return 0; } } return -2; } int List::deleteFirst() { Node *p = head.next; Node *q = &head; Node *r = p->next; if (p == q) return -3; // 何も入っていない q->next = r; p->prev = q; delete p; return 0; } int List::deleteLast() { Node *p = head.prev; Node *q = &head; Node *r = p->prev; if (p == q) return -3; // 何も入っていない q->prev = r; r->next = q; delete p; return 0; } int List::print() { for (Node *p = head.next; p != &head; p = p->next) { if (p != head.prev) cout << p->data << ' '; else cout << p->data << endl; } } int main() { List L; int n, x; char a[12]; cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (a[0] == 'i') { cin >> x; L.insertx(x); } else if (a[6] == 'F') { L.deleteFirst(); } else if (a[6] == 'L') { L.deleteLast(); } else { cin >> x; L.deletex(x); } } L.print(); return 0; }
#include "string.h" #include <iostream> using namespace std; class Node { public: Node *prev; Node *next; int data; Node() { prev = 0; next = 0; data = 0; } }; class List { private: Node head; public: List() { head.prev = &head; head.next = &head; } int insertx(int x); int deletex(int x); int deleteFirst(); int deleteLast(); int print(); }; // ここに問題がある!! int List::insertx(int x) { Node *p = new Node(); if (p == 0) return -1; Node *q = head.next; Node *r = &head; p->prev = r; p->next = q; p->data = x; r->next = p; q->prev = p; return 0; } int List::deletex(int x) { for (Node *p = head.next; p != &head; p = p->next) { if (p->data == x) { Node *q = p->prev; Node *r = p->next; q->next = r; r->prev = q; delete p; return 0; } } return -2; } int List::deleteFirst() { Node *p = head.next; Node *q = &head; Node *r = p->next; if (p == q) return -3; // 何も入っていない q->next = r; r->prev = q; delete p; return 0; } int List::deleteLast() { Node *p = head.prev; Node *q = &head; Node *r = p->prev; if (p == q) return -3; // 何も入っていない q->prev = r; r->next = q; delete p; return 0; } int List::print() { for (Node *p = head.next; p != &head; p = p->next) { if (p != head.prev) cout << p->data << ' '; else cout << p->data << endl; } } int main() { List L; int n, x; char a[12]; cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (a[0] == 'i') { cin >> x; L.insertx(x); } else if (a[6] == 'F') { L.deleteFirst(); } else if (a[6] == 'L') { L.deleteLast(); } else { cin >> x; L.deletex(x); } } L.print(); return 0; }
replace
76
77
76
77
TLE
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct Node { int key; Node *next; Node *pre; }; struct List { Node *first; void init() { first = (Node *)malloc(sizeof(Node)); first->key = 0; first->next = first; first->pre = first; } void insert(int x) { Node *nextnode = (Node *)malloc(sizeof(Node)); nextnode->key = x; nextnode->next = first->next; first->next->pre = nextnode; first->next = nextnode; nextnode->pre = first; } void delete_x(int x) { for (Node *p = first->next; p != first; p = p->next) { if (p->key == x) { p->next->pre = p->pre; p->pre->next = p->next; free(p); return; } } } void deleteFirst() { Node *p = first->next; p->next->pre = p->pre; first->next = p->next; free(p); } void deleteLast() { Node *p; for (p = first; p->next != first; p = p->next) ; p->pre->next = first; } void print_list() { bool flg = false; for (Node *p = first->next; p != first; p = p->next) { if (flg) cout << " "; cout << p->key; flg = true; } cout << endl; } }; int main() { ios::sync_with_stdio(false); int n; cin >> n; List list; list.init(); string s; int k; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> k; list.insert(k); } else if (s == "delete") { cin >> k; list.delete_x(k); } else if (s == "deleteFirst") { list.deleteFirst(); } else { list.deleteLast(); } } list.print_list(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct Node { int key; Node *next; Node *pre; }; struct List { Node *first; void init() { first = (Node *)malloc(sizeof(Node)); first->key = 0; first->next = first; first->pre = first; } void insert(int x) { Node *nextnode = (Node *)malloc(sizeof(Node)); nextnode->key = x; nextnode->next = first->next; first->next->pre = nextnode; first->next = nextnode; nextnode->pre = first; } void delete_x(int x) { for (Node *p = first->next; p != first; p = p->next) { if (p->key == x) { p->next->pre = p->pre; p->pre->next = p->next; free(p); return; } } } void deleteFirst() { Node *p = first->next; p->next->pre = p->pre; first->next = p->next; free(p); } void deleteLast() { Node *p = first->pre; // for(p = first; p->next != first; p=p->next); p->pre->next = p->next; p->next->pre = p->pre; } void print_list() { bool flg = false; for (Node *p = first->next; p != first; p = p->next) { if (flg) cout << " "; cout << p->key; flg = true; } cout << endl; } }; int main() { ios::sync_with_stdio(false); int n; cin >> n; List list; list.init(); string s; int k; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> k; list.insert(k); } else if (s == "delete") { cin >> k; list.delete_x(k); } else if (s == "deleteFirst") { list.deleteFirst(); } else { list.deleteLast(); } } list.print_list(); }
replace
49
53
49
53
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { int key; Node *prev, *next; }; Node *NIL; void init() { NIL = new Node(); NIL->prev = NIL; NIL->next = NIL; } void insert(int key) { Node *x = new Node(); x->key = key; x->next = NIL->next; NIL->next->prev = x; NIL->next = x; x->prev = NIL; } Node *listSearch(int key) { Node *cur = NIL->next; while (cur->key != key && cur != NIL) { cur = cur->next; } return cur; } void deleteNode(Node *tmp) { tmp->next->prev = tmp->prev; tmp->prev->next = tmp->next; delete tmp; } void deleteFirst() { deleteNode(NIL->next); } void deleteLast() { deleteNode(NIL->prev); } void deleteKey(int key) { deleteNode(listSearch(key)); } void print() { Node *cur = NIL->next; while (cur != NIL) { if (cur != NIL->next) printf(" "); printf("%d", cur->key); cur = cur->next; } printf("\n"); } int main() { int n; char S[20]; int key; init(); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s %d", S, &key); if (S[0] == 'i') insert(key); if (S[0] == 'd') { if (S[6] == 'F') deleteFirst(); else if (S[6] == 'L') deleteLast(); else deleteKey(key); } } print(); return 0; }
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { int key; Node *prev, *next; }; Node *NIL; void init() { NIL = new Node(); NIL->prev = NIL; NIL->next = NIL; } void insert(int key) { Node *x = new Node(); x->key = key; x->next = NIL->next; NIL->next->prev = x; NIL->next = x; x->prev = NIL; } Node *listSearch(int key) { Node *cur = NIL->next; while (cur->key != key && cur != NIL) { cur = cur->next; } return cur; } void deleteNode(Node *tmp) { if (tmp == NIL) return; tmp->next->prev = tmp->prev; tmp->prev->next = tmp->next; delete tmp; } void deleteFirst() { deleteNode(NIL->next); } void deleteLast() { deleteNode(NIL->prev); } void deleteKey(int key) { deleteNode(listSearch(key)); } void print() { Node *cur = NIL->next; while (cur != NIL) { if (cur != NIL->next) printf(" "); printf("%d", cur->key); cur = cur->next; } printf("\n"); } int main() { int n; char S[20]; int key; init(); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s %d", S, &key); if (S[0] == 'i') insert(key); if (S[0] == 'd') { if (S[6] == 'F') deleteFirst(); else if (S[6] == 'L') deleteLast(); else deleteKey(key); } } print(); return 0; }
insert
36
36
36
38
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <functional> #include <iostream> #include <list> #include <string> #include <unordered_map> int main() { std::list<unsigned> l; std::unordered_map<std::string, std::function<void(void)>> funcs{ {"insert", [&l]() { unsigned i; std::cin >> i; l.push_front(i); }}, {"delete", [&l]() { unsigned i; std::cin >> i; auto it = std::find(l.begin(), l.end(), i); if (it != l.end()) l.erase(it); }}, {"deleteFirst", [&l]() { l.erase(l.begin()); }}, {"deleteLast", [&l]() { l.erase(--(l.end())); }}}; unsigned n; std::string buf; std::cin >> n; for (; n; n--) { std::cin >> buf; funcs[buf](); } std::cout << *(l.begin()); for (auto it = ++l.begin(); it != l.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n'; }
#include <algorithm> #include <functional> #include <iostream> #include <list> #include <string> #include <unordered_map> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::list<unsigned> l; std::unordered_map<std::string, std::function<void(void)>> funcs{ {"insert", [&l]() { unsigned i; std::cin >> i; l.push_front(i); }}, {"delete", [&l]() { unsigned i; std::cin >> i; auto it = std::find(l.begin(), l.end(), i); if (it != l.end()) l.erase(it); }}, {"deleteFirst", [&l]() { l.erase(l.begin()); }}, {"deleteLast", [&l]() { l.erase(--(l.end())); }}}; unsigned n; std::string buf; std::cin >> n; for (; n; n--) { std::cin >> buf; funcs[buf](); } std::cout << *(l.begin()); for (auto it = ++l.begin(); it != l.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n'; }
insert
8
8
8
10
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <functional> #include <iostream> #include <list> #include <string> #include <unordered_map> int main() { std::list<unsigned> l; unsigned i; std::unordered_map<std::string, std::function<void(void)>> funcs{ {"insert", [&l, &i]() { std::cin >> i; l.push_front(i); }}, {"delete", [&l, &i]() { std::cin >> i; auto it = std::find(l.begin(), l.end(), i); if (it != l.end()) l.erase(it); }}, {"deleteFirst", [&l]() { l.erase(l.begin()); }}, {"deleteLast", [&l]() { l.erase(--(l.end())); }}}; unsigned n; std::string buf; std::cin >> n; for (; n; n--) { std::cin >> buf; funcs[buf](); } std::cout << *(l.begin()); for (auto it = ++l.begin(); it != l.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n'; }
#include <algorithm> #include <functional> #include <iostream> #include <list> #include <string> #include <unordered_map> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::list<unsigned> l; unsigned i; std::unordered_map<std::string, std::function<void(void)>> funcs{ {"insert", [&l, &i]() { std::cin >> i; l.push_front(i); }}, {"delete", [&l, &i]() { std::cin >> i; auto it = std::find(l.begin(), l.end(), i); if (it != l.end()) l.erase(it); }}, {"deleteFirst", [&l]() { l.erase(l.begin()); }}, {"deleteLast", [&l]() { l.erase(--(l.end())); }}}; unsigned n; std::string buf; std::cin >> n; for (; n; n--) { std::cin >> buf; funcs[buf](); } std::cout << *(l.begin()); for (auto it = ++l.begin(); it != l.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n'; }
insert
8
8
8
10
TLE
p02265
C++
Time Limit Exceeded
//============================================================================ // Name : LinkedList.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <cstdlib> #include <iostream> #include <sstream> #include <stdio.h> #include <string.h> using namespace std; #define BUF_SIZE 1024 struct Node { int data; Node *prev; Node *next; }; class ConnectedList { public: ConnectedList() { motherNode = new Node(); motherNode->data = -1; motherNode->prev = motherNode; motherNode->next = motherNode; }; ~ConnectedList() { Node *tmpNode = motherNode->next; Node *nextNode; while (tmpNode != motherNode) { nextNode = tmpNode->next; delete tmpNode; tmpNode = nextNode; } delete motherNode; } void insertNode(int x) { Node *tmpNode = motherNode->next; motherNode->next = new Node(); motherNode->next->data = x; motherNode->next->next = tmpNode; motherNode->next->prev = motherNode; tmpNode->prev = motherNode->next; }; void deleteNode(int x) { Node *tmpNode = motherNode->next; while (tmpNode != motherNode && tmpNode->data != x) { tmpNode = tmpNode->next; } if (tmpNode != motherNode) { tmpNode->prev->next = tmpNode->next; tmpNode->next->prev = tmpNode->prev; delete tmpNode; } } void deleteFirst() { Node *tmpNode = motherNode->next; if (tmpNode != motherNode) { motherNode->next = tmpNode->next; tmpNode->next->prev = motherNode; delete tmpNode; } } void deleteLast() { Node *tmpNode = motherNode->prev; if (tmpNode != motherNode) { tmpNode->prev->next = motherNode; motherNode->prev = tmpNode->prev; } delete tmpNode; } void showList() { Node *tmpNode = motherNode->next; if (tmpNode != motherNode) { cout << tmpNode->data; tmpNode = tmpNode->next; } while (tmpNode != motherNode) { cout << " " << tmpNode->data; tmpNode = tmpNode->next; } cout << "\n"; } private: Node *motherNode; }; int main() { ConnectedList list; string line; getline(cin, line); int n_commands = stoi(line); for (int i = 0; i < n_commands; i++) { string command; int data; cin >> command; if (command == "insert") { cin >> data; list.insertNode(data); } else if (command == "delete") { cin >> data; list.deleteNode(data); } else if (command == "deleteFirst") { list.deleteFirst(); } else if (command == "deleteLast") { list.deleteLast(); } } list.showList(); return 0; }
//============================================================================ // Name : LinkedList.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <cstdlib> #include <iostream> #include <sstream> #include <stdio.h> #include <string.h> using namespace std; #define BUF_SIZE 1024 struct Node { int data; Node *prev; Node *next; }; class ConnectedList { public: ConnectedList() { motherNode = new Node(); motherNode->data = -1; motherNode->prev = motherNode; motherNode->next = motherNode; }; ~ConnectedList() { Node *tmpNode = motherNode->next; Node *nextNode; while (tmpNode != motherNode) { nextNode = tmpNode->next; delete tmpNode; tmpNode = nextNode; } delete motherNode; } void insertNode(int x) { Node *tmpNode = motherNode->next; motherNode->next = new Node(); motherNode->next->data = x; motherNode->next->next = tmpNode; motherNode->next->prev = motherNode; tmpNode->prev = motherNode->next; }; void deleteNode(int x) { Node *tmpNode = motherNode->next; while (tmpNode != motherNode && tmpNode->data != x) { tmpNode = tmpNode->next; } if (tmpNode != motherNode) { tmpNode->prev->next = tmpNode->next; tmpNode->next->prev = tmpNode->prev; delete tmpNode; } } void deleteFirst() { Node *tmpNode = motherNode->next; if (tmpNode != motherNode) { motherNode->next = tmpNode->next; tmpNode->next->prev = motherNode; delete tmpNode; } } void deleteLast() { Node *tmpNode = motherNode->prev; if (tmpNode != motherNode) { tmpNode->prev->next = motherNode; motherNode->prev = tmpNode->prev; } delete tmpNode; } void showList() { Node *tmpNode = motherNode->next; if (tmpNode != motherNode) { cout << tmpNode->data; tmpNode = tmpNode->next; } while (tmpNode != motherNode) { cout << " " << tmpNode->data; tmpNode = tmpNode->next; } cout << "\n"; } private: Node *motherNode; }; int main() { cin.tie(0); ios::sync_with_stdio(false); ConnectedList list; string line; getline(cin, line); int n_commands = stoi(line); for (int i = 0; i < n_commands; i++) { string command; int data; cin >> command; if (command == "insert") { cin >> data; list.insertNode(data); } else if (command == "delete") { cin >> data; list.deleteNode(data); } else if (command == "deleteFirst") { list.deleteFirst(); } else if (command == "deleteLast") { list.deleteLast(); } } list.showList(); return 0; }
insert
101
101
101
103
TLE
p02265
C++
Time Limit Exceeded
#include <cassert> #include <iostream> #include <list> #include <string> class TDllist { std::list<int> FList; public: void Insert(int X) { FList.push_front(X); } void Delete(int X) { for (std::list<int>::iterator It = FList.begin(); It != FList.end(); ++It) { if (*It == X) { FList.erase(It); return; } } } void DeleteFirst() { FList.pop_front(); } void DeleteLast() { FList.pop_back(); } void Show() const { std::list<int>::const_iterator It = FList.begin(); std::cout << *It; for (++It; It != FList.end(); ++It) { std::cout << " " << *It; } std::cout << std::endl; } }; int main() { std::cin.tie(0); TDllist List; int n, X; std::cin >> n; std::string Command; for (int i = 0; i < n; ++i) { // std::string Command; std::cin >> Command; if (Command == "insert") { // int X; std::cin >> X; List.Insert(X); } else if (Command == "delete") { // int X; std::cin >> X; List.Delete(X); } else if (Command == "deleteFirst") { List.DeleteFirst(); } else if (Command == "deleteLast") { List.DeleteLast(); } else { assert(false); } } List.Show(); return 0; }
#include <cassert> #include <iostream> #include <list> #include <string> class TDllist { std::list<int> FList; public: void Insert(int X) { FList.push_front(X); } void Delete(int X) { for (std::list<int>::iterator It = FList.begin(); It != FList.end(); ++It) { if (*It == X) { FList.erase(It); return; } } } void DeleteFirst() { FList.pop_front(); } void DeleteLast() { FList.pop_back(); } void Show() const { std::list<int>::const_iterator It = FList.begin(); std::cout << *It; for (++It; It != FList.end(); ++It) { std::cout << " " << *It; } std::cout << std::endl; } }; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); TDllist List; int n, X; std::cin >> n; std::string Command; for (int i = 0; i < n; ++i) { // std::string Command; std::cin >> Command; if (Command == "insert") { // int X; std::cin >> X; List.Insert(X); } else if (Command == "delete") { // int X; std::cin >> X; List.Delete(X); } else if (Command == "deleteFirst") { List.DeleteFirst(); } else if (Command == "deleteLast") { List.DeleteLast(); } else { assert(false); } } List.Show(); return 0; }
insert
32
32
32
33
TLE
p02265
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> using namespace std; int main() { deque<int> deq; int N; cin >> N; while (N--) { string cmd; cin >> cmd; if (cmd == "insert") { int x; cin >> x; deq.push_front(x); } else if (cmd == "delete") { int x; cin >> x; auto it = find(deq.begin(), deq.end(), x); if (it != deq.end()) deq.erase(it); } if (cmd == "deleteFirst") deq.pop_front(); if (cmd == "deleteLast") deq.pop_back(); } if (!deq.empty()) { cout << deq.front(); for (auto it = deq.begin() + 1; it != deq.end(); ++it) cout << " " << *it; cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <queue> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(false); deque<int> deq; int N; cin >> N; while (N--) { string cmd; cin >> cmd; if (cmd == "insert") { int x; cin >> x; deq.push_front(x); } else if (cmd == "delete") { int x; cin >> x; auto it = find(deq.begin(), deq.end(), x); if (it != deq.end()) deq.erase(it); } if (cmd == "deleteFirst") deq.pop_front(); if (cmd == "deleteLast") deq.pop_back(); } if (!deq.empty()) { cout << deq.front(); for (auto it = deq.begin() + 1; it != deq.end(); ++it) cout << " " << *it; cout << endl; } return 0; }
insert
7
7
7
10
TLE
p02265
C++
Time Limit Exceeded
#include <iostream> #include <string> using namespace std; class DLinkList { private: struct ListNode { ListNode *prior, *next; int val; ListNode(int v) : prior(NULL), next(NULL), val(v){}; }; private: ListNode *head; int len; public: DLinkList() { head = new ListNode(-1); head->prior = head; head->next = head; len = 0; } ListNode *search(int v) { ListNode *p = head->next; while (p != head && p->val != v) p = p->next; return p; } void del_helper(ListNode *p) { if (p == head) return; p->prior->next = p->next; p->next->prior = p->prior; delete p; len--; } void del_first() { del_helper(head->next); } void del_last() { del_helper(head->prior); } void del_x(int v) { del_helper(search(v)); } void insert(int val) { ListNode *node = new ListNode(val); node->next = head->next; head->next->prior = node; head->next = node; node->prior = head; len++; } void print() { ListNode *p = head->next; int count = 0; while (count < len - 1) { cout << p->val << " "; p = p->next; } if (len - 1 >= 0) cout << p->val << endl; } }; int main() { ios_base::sync_with_stdio(false); DLinkList l; string str; int val; cin >> str; while (cin >> str) { if (str == "insert") { cin >> val; l.insert(val); } else if (str == "delete") { cin >> val; l.del_x(val); } else if (str == "deleteFirst") l.del_first(); else if (str == "deleteLast") l.del_last(); } l.print(); }
#include <iostream> #include <string> using namespace std; class DLinkList { private: struct ListNode { ListNode *prior, *next; int val; ListNode(int v) : prior(NULL), next(NULL), val(v){}; }; private: ListNode *head; int len; public: DLinkList() { head = new ListNode(-1); head->prior = head; head->next = head; len = 0; } ListNode *search(int v) { ListNode *p = head->next; while (p != head && p->val != v) p = p->next; return p; } void del_helper(ListNode *p) { if (p == head) return; p->prior->next = p->next; p->next->prior = p->prior; delete p; len--; } void del_first() { del_helper(head->next); } void del_last() { del_helper(head->prior); } void del_x(int v) { del_helper(search(v)); } void insert(int val) { ListNode *node = new ListNode(val); node->next = head->next; head->next->prior = node; head->next = node; node->prior = head; len++; } void print() { ListNode *p = head->next; int count = 0; while (count++ < len - 1) { cout << p->val << " "; p = p->next; } if (len - 1 >= 0) cout << p->val << endl; } }; int main() { ios_base::sync_with_stdio(false); DLinkList l; string str; int val; cin >> str; while (cin >> str) { if (str == "insert") { cin >> val; l.insert(val); } else if (str == "delete") { cin >> val; l.del_x(val); } else if (str == "deleteFirst") l.del_first(); else if (str == "deleteLast") l.del_last(); } l.print(); }
replace
63
64
63
64
TLE
p02265
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); list<int> lis; int cnt; cin >> cnt; string tmp; int tmpI; for (int i = 0; i < cnt; i++) { cin >> tmp; if (tmp[0] == 'i') { // insert x cin >> tmpI; lis.push_front(tmpI); } else if (tmp == "delete") { // delete x cin >> tmpI; for (list<int>::iterator it = lis.begin(); it != lis.end(); i++) { if (*it == tmpI) { lis.erase(it); break; } } } else if (tmp[6] == 'F') { // deleteF lis.pop_front(); } else { // deleteL lis.pop_back(); } } for (list<int>::iterator it = lis.begin(); it != lis.end(); it++) { if (it != lis.begin()) { cout << " "; } cout << *it; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); list<int> lis; int cnt; cin >> cnt; string tmp; int tmpI; for (int i = 0; i < cnt; i++) { cin >> tmp; if (tmp[0] == 'i') { // insert x cin >> tmpI; lis.push_front(tmpI); } else if (tmp == "delete") { // delete x cin >> tmpI; for (list<int>::iterator it = lis.begin(); it != lis.end(); it++) { if (*it == tmpI) { lis.erase(it); break; } } } else if (tmp[6] == 'F') { // deleteF lis.pop_front(); } else { // deleteL lis.pop_back(); } } for (list<int>::iterator it = lis.begin(); it != lis.end(); it++) { if (it != lis.begin()) { cout << " "; } cout << *it; } cout << endl; return 0; }
replace
17
18
17
18
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <iostream> #include <list> using namespace std; int main(void) { std::ios_base::sync_with_stdio(false); list<int> l; string s; int d; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> d; l.push_front(d); } else if (s == "delete") { cin >> d; list<int>::iterator pos; pos = find(l.begin(), l.end(), d); l.erase(pos); } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } } list<int>::iterator pos; for (pos = l.begin(); pos != l.end(); pos++) { if (pos != l.begin()) cout << " "; cout << *pos; } cout << endl; return 0; }
#include <algorithm> #include <iostream> #include <list> using namespace std; int main(void) { std::ios_base::sync_with_stdio(false); list<int> l; string s; int d; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> d; l.push_front(d); } else if (s == "delete") { cin >> d; list<int>::iterator pos; pos = find(l.begin(), l.end(), d); if (pos != l.end()) l.erase(pos); } else if (s == "deleteFirst") { l.pop_front(); } else if (s == "deleteLast") { l.pop_back(); } } list<int>::iterator pos; for (pos = l.begin(); pos != l.end(); pos++) { if (pos != l.begin()) cout << " "; cout << *pos; } cout << endl; return 0; }
replace
22
23
22
24
0
p02265
C++
Time Limit Exceeded
#include <iostream> #include <iterator> #include <list> using namespace std; int main() { int n, x; string cmd; list<int> alist; cin >> n; for (int i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> x; alist.push_front(x); } else if (cmd == "delete") { cin >> x; list<int>::iterator ait = alist.begin(); while (1) { if (*ait == x) { alist.erase(ait); break; } else ait++; } } else if (cmd == "deleteFirst") alist.pop_front(); else alist.pop_back(); } n = alist.size(); list<int>::iterator ait = alist.begin(); for (int i = 0; i < n - 1; i++) cout << *ait++ << ' '; cout << *ait << endl; return 0; }
#include <iostream> #include <iterator> #include <list> using namespace std; int main() { int n, x; string cmd; list<int> alist; cin >> n; for (int i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> x; alist.push_front(x); } else if (cmd == "delete") { cin >> x; list<int>::iterator ait = alist.begin(); while (ait != alist.end()) { if (*ait == x) { alist.erase(ait); break; } else ait++; } } else if (cmd == "deleteFirst") alist.pop_front(); else alist.pop_back(); } n = alist.size(); list<int>::iterator ait = alist.begin(); for (int i = 0; i < n - 1; i++) cout << *ait++ << ' '; cout << *ait << endl; return 0; }
replace
18
19
18
19
TLE
p02265
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; typedef struct _List { _List *front; _List *rear; int data; } List; int main(void) { std::ios_base::sync_with_stdio(false); int n; string s; int d; cin >> n; List *top; List *end; List a; a.front = NULL; a.rear = NULL; a.data = 0; top = &a; end = &a; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> d; List *temp = new List; temp->data = d; temp->rear = top; temp->front = NULL; top->front = temp; top = temp; } else if (s == "delete") { cin >> d; List *t = top; while (t != NULL && t->rear != NULL) { if (t->data == d) { if (t->front != NULL) (t->front)->rear = (t->rear); else top = t->rear; if (t->rear != NULL) (t->rear)->front = (t->front); break; } t = t->rear; } } else if (s == "deleteFirst") { if (top->rear != NULL) (top->rear)->front = NULL; top = top->rear; } else if (s == "deleteLast") { List *t = end; t = t->front; t = t->front; t->rear = end; end->front = t; } } List *t = top; while (t->rear != NULL) { if (t != top) cout << " "; cout << t->data; t = t->rear; } cout << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; typedef struct _List { _List *front; _List *rear; int data; } List; int main(void) { std::ios_base::sync_with_stdio(false); int n; string s; int d; cin >> n; List *top; List *end; List a; a.front = NULL; a.rear = NULL; a.data = 0; top = &a; end = &a; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> d; List *temp = new List; temp->data = d; temp->rear = top; temp->front = NULL; top->front = temp; top = temp; } else if (s == "delete") { cin >> d; List *t = top; while (t != NULL && t->rear != NULL) { if (t->data == d) { if (t->front != NULL) (t->front)->rear = (t->rear); else top = t->rear; if (t->rear != NULL) (t->rear)->front = (t->front); break; } t = t->rear; } } else if (s == "deleteFirst") { if (top->rear != NULL) (top->rear)->front = NULL; top = top->rear; } else if (s == "deleteLast") { List *t = end; t = t->front; t = t->front; if (t != NULL) t->rear = end; else top = end; end->front = t; } } List *t = top; while (t->rear != NULL) { if (t != top) cout << " "; cout << t->data; t = t->rear; } cout << endl; return 0; }
replace
61
62
61
65
0
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; void initiation() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int x) { Node *a = (Node *)malloc(sizeof(Node)); a->key = x; a->next = nil->next; nil->next->prev = a; a->prev = nil; nil->next = a; } void delete1(Node *cur) { cur->prev->next = cur->next; cur->next->prev = cur->prev; free(cur); } Node *listserch(int x) { Node *cur = nil->next; while (cur != nil && cur->key != x) { cur = cur->next; } return cur; } int main() { int size; cin >> size; // char *inst = new char[100]; string inst; int x; initiation(); for (int i = 0; i < size; i++) { cin >> inst; if (inst == "insert") { cin >> x; insert(x); } else if (inst == "delete") { cin >> x; delete1(listserch(x)); } else if (inst == "deleteFirst") { delete1(nil->next); } else if (inst == "deleteLast") { delete1(nil->prev); } /*Node *cur = nil->next; while(cur != nil){ if(cur == nil->next){ cout << cur->key; } else{ cout << ' ' << cur->key; } cur = cur->next; } cout << endl;*/ } Node *cur = nil->next; while (cur != nil) { if (cur == nil->next) { cout << cur->key; } else { cout << ' ' << cur->key; } cur = cur->next; } cout << endl; return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; struct Node { int key; Node *prev, *next; }; Node *nil; void initiation() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int x) { Node *a = (Node *)malloc(sizeof(Node)); a->key = x; a->next = nil->next; nil->next->prev = a; a->prev = nil; nil->next = a; } void delete1(Node *cur) { if (cur == nil) { return; } cur->prev->next = cur->next; cur->next->prev = cur->prev; free(cur); } Node *listserch(int x) { Node *cur = nil->next; while (cur != nil && cur->key != x) { cur = cur->next; } return cur; } int main() { int size; cin >> size; // char *inst = new char[100]; string inst; int x; initiation(); for (int i = 0; i < size; i++) { cin >> inst; if (inst == "insert") { cin >> x; insert(x); } else if (inst == "delete") { cin >> x; delete1(listserch(x)); } else if (inst == "deleteFirst") { delete1(nil->next); } else if (inst == "deleteLast") { delete1(nil->prev); } /*Node *cur = nil->next; while(cur != nil){ if(cur == nil->next){ cout << cur->key; } else{ cout << ' ' << cur->key; } cur = cur->next; } cout << endl;*/ } Node *cur = nil->next; while (cur != nil) { if (cur == nil->next) { cout << cur->key; } else { cout << ' ' << cur->key; } cur = cur->next; } cout << endl; return 0; }
insert
29
29
29
32
TLE
p02265
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <string> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define rep(i, n) FOR(i, 0, n) #define DEBUG(x) cout << #x << ": " << x << endl #define vint vector<int> #define vdouble vector<double> #define vstring vector<string> using namespace std; #include <map> #include <queue> #include <set> typedef long long ll; typedef unsigned long long ull; #include <list> // size, push_back, pop_back, insert, erase const int MAX_N = 3000000; char cmd[MAX_N]; int arg[MAX_N]; int main() { int N; cin >> N; rep(i, N) { // string command; // cin >> command; char command[20]; scanf("%s", command); cerr << "X"; if (command[0] == 'i') { cmd[i] = 'I'; cin >> arg[i]; } else if (command[6] == 'F') { cmd[i] = 'F'; } else if (command[6] == 'L') { cmd[i] = 'L'; } else { cmd[i] = 'D'; cin >> arg[i]; } } list<int> lst; rep(i, N) { if (cmd[i] == 'I') { lst.push_front(arg[i]); } if (cmd[i] == 'D') { for (list<int>::iterator it = lst.begin(); it != lst.end(); it++) { if (*it == arg[i]) { lst.erase(it); break; } } } if (cmd[i] == 'F') { lst.pop_front(); } if (cmd[i] == 'L') { lst.pop_back(); } } for (list<int>::iterator it = lst.begin(); it != lst.end(); it++) { if (it != lst.begin()) { cout << ' '; } cout << *it; } cout << endl; }
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <string> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define rep(i, n) FOR(i, 0, n) #define DEBUG(x) cout << #x << ": " << x << endl #define vint vector<int> #define vdouble vector<double> #define vstring vector<string> using namespace std; #include <map> #include <queue> #include <set> typedef long long ll; typedef unsigned long long ull; #include <list> // size, push_back, pop_back, insert, erase const int MAX_N = 3000000; char cmd[MAX_N]; int arg[MAX_N]; int main() { int N; cin >> N; rep(i, N) { // string command; // cin >> command; char command[20]; scanf("%s", command); // cerr << "X"; if (command[0] == 'i') { cmd[i] = 'I'; cin >> arg[i]; } else if (command[6] == 'F') { cmd[i] = 'F'; } else if (command[6] == 'L') { cmd[i] = 'L'; } else { cmd[i] = 'D'; cin >> arg[i]; } } list<int> lst; rep(i, N) { if (cmd[i] == 'I') { lst.push_front(arg[i]); } if (cmd[i] == 'D') { for (list<int>::iterator it = lst.begin(); it != lst.end(); it++) { if (*it == arg[i]) { lst.erase(it); break; } } } if (cmd[i] == 'F') { lst.pop_front(); } if (cmd[i] == 'L') { lst.pop_back(); } } for (list<int>::iterator it = lst.begin(); it != lst.end(); it++) { if (it != lst.begin()) { cout << ' '; } cout << *it; } cout << endl; }
replace
42
43
42
43
0
XXXXXXX
p02265
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <cstring> struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *x = (Node *)malloc(sizeof(Node)); x->key = key; x->next = nil->next; nil->next->prev = x->next; nil->next = x; x->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"); } Node *listSearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) { cur = cur->next; } return cur; } 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)); } 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; }
#include <cstdio> #include <cstdlib> #include <cstring> struct Node { int key; Node *prev, *next; }; Node *nil; void init() { nil = (Node *)malloc(sizeof(Node)); nil->next = nil; nil->prev = nil; } void insert(int key) { Node *x = (Node *)malloc(sizeof(Node)); x->key = key; x->next = nil->next; nil->next->prev = x; nil->next = x; x->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"); } Node *listSearch(int key) { Node *cur = nil->next; while (cur != nil && cur->key != key) { cur = cur->next; } return cur; } 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)); } 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; }
replace
22
23
22
23
TLE
p02265
C++
Runtime Error
#include <iostream> #include <string> using namespace std; class Node { public: int key; Node *prev; Node *next; Node(int _key, Node *_prev, Node *_next) { key = _key; prev = _prev; next = _next; } }; class List { public: Node *head; Node *last; List() { head = NULL; last = NULL; } void insert(int key) { auto node = head; head = new Node(key, NULL, node); if (node) node->prev = head; else last = head; } void deleteNode(Node *node) { if (node == head) head = node->next; else node->prev->next = node->next; if (node == last) last = node->prev; else node->next->prev = node->prev; delete node; } Node *find(int key) { Node *current = head; while (current) { if (current->key == key) { break; } else { current = current->next; } } return current; } void deleteFirst() { deleteNode(head); } void deleteLast() { deleteNode(last); } }; int main() { int n; cin >> n; auto list = List(); string name; int key; for (int i = 0; i < n; i++) { cin >> name; if (name == "insert") { cin >> key; list.insert(key); } else if (name == "delete") { cin >> key; list.deleteNode(list.find(key)); } else if (name == "deleteFirst") { list.deleteFirst(); } else if (name == "deleteLast") { list.deleteLast(); } } auto current = list.head; while (current) { if (current != list.head) cout << ' '; cout << current->key; current = current->next; } cout << endl; }
#include <iostream> #include <string> using namespace std; class Node { public: int key; Node *prev; Node *next; Node(int _key, Node *_prev, Node *_next) { key = _key; prev = _prev; next = _next; } }; class List { public: Node *head; Node *last; List() { head = NULL; last = NULL; } void insert(int key) { auto node = head; head = new Node(key, NULL, node); if (node) node->prev = head; else last = head; } void deleteNode(Node *node) { if (!node) return; if (node == head) head = node->next; else node->prev->next = node->next; if (node == last) last = node->prev; else node->next->prev = node->prev; delete node; } Node *find(int key) { Node *current = head; while (current) { if (current->key == key) { break; } else { current = current->next; } } return current; } void deleteFirst() { deleteNode(head); } void deleteLast() { deleteNode(last); } }; int main() { int n; cin >> n; auto list = List(); string name; int key; for (int i = 0; i < n; i++) { cin >> name; if (name == "insert") { cin >> key; list.insert(key); } else if (name == "delete") { cin >> key; list.deleteNode(list.find(key)); } else if (name == "deleteFirst") { list.deleteFirst(); } else if (name == "deleteLast") { list.deleteLast(); } } auto current = list.head; while (current) { if (current != list.head) cout << ' '; cout << current->key; current = current->next; } cout << endl; }
insert
34
34
34
37
0