C++版(1)
#include<iostream>
#include<algorithm>
#include<vector>
#define RETURN 0
#define GET 1
#define MAX 1005
using namespace std;
struct Node{
int w;
int t;
int type;
bool operator < (Node a) const{
if(t == a.t){
if(type == a.type)
return w < a.w;
else
return type < a.type;
}
return t < a.t;
}
};
vector<Node> arr;
int a[MAX];
int main()
{
Node node;
int n, k;
int w,s,c;
scanf("%d%d",&n,&k);
for(int i = 0; i < k; ++i){
scanf("%d%d%d",&w,&s,&c);
node.w = w;
node.t = s;
node.type = GET;
arr.push_back(node);
node.t = s+c;
node.type = RETURN;
arr.push_back(node);
}
sort(arr.begin(),arr.end());
for(int i = 1; i <= n; ++i){
a[i] = i;
}
for(int i = 0; i < arr.size(); ++i){
Node tmp = arr[i];
if(tmp.type == RETURN){
for(int j = 1; j <= n; ++j){
if(a[j] == 0){
a[j] = tmp.w;
break;
}
}
}
else if(tmp.type == GET){
for(int j = 1; j <= n; ++j){
if(a[j] == tmp.w){
a[j] = 0;
break;
}
}
}
}
for(int i = 1; i <= n; ++i){
cout << a[i] <<" ";
}
// for(int i = 0; i < arr.size(); ++i){
// cout << arr[i].t <<" ";
// }
return 0;
}
C++版(2)
开始没有满分 是因为数组开小了, 题目测试点在 10000+ 所以 尽可能的开大一点 防止极限测试点不过
#include<iostream>
#include<algorithm>
using namespace std;
const int INF = 65535;
const int MAX = 20000;
int main()
{
int i, j;
int a[MAX];
int b[MAX][3];
int c[MAX];
int N, K, x;
cin >> N >> K;
for(i = 0; i < K; i++ ){
for(j = 0; j < 3; j++ ){
cin >> x;
b[i][j] = x;
}
}
for(i = 1; i <= N; i++ ){
a[i] = i;
}
int time, cnt;
for(time = 1; time <= MAX; time++ ){
cnt = 0;
for(i = 0; i < N; i++ ){//每次time都要重新初始化
c[i] = INF;
}
for(i = 0; i < K; i++ ){
if(b[i][1] + b[i][2] == time){
c[cnt++] = b[i][0];
}
}
sort(c, c+N);
for(i = 0; i < cnt; i++ ){
for(j = 1; j <= N; j++ ){
if(a[j] == 0){
a[j] = c[i];
break;
}
}
}
for(i = 0; i < K; i++ ){
if(time == b[i][1]){
for(int k = 1; k <= N; k++ ){//这里 钥匙可能更换位置 需要找到其当前所在位置 然后取走(置0)
if(a[k] == b[i][0]){
a[k] = 0;
break;
}
}
}
}
}
for(int i = 1; i <= N; i++ ){
cout << a[i] << " ";
}
return 0;
}
//Java的另一种实现: 所有时刻按照题目规则排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
ArrayList<Node> list;
Scanner in = new Scanner(System.in);
int []a;
public static void main(String[] args) {
new Main().run();
}
public void run(){
list = new ArrayList<>();
int n, k;
n = in.nextInt();
k = in.nextInt();
a = new int[n+1];
for(int i = 1; i <= n; i++ ){
a[i] = i;
}
int key,time, lastTime;
for(int i = 0; i < k; i++ ){
key = in.nextInt();
time = in.nextInt();
lastTime = in.nextInt();
list.add(new Node(key, time, 1));
list.add(new Node(key, time+lastTime, 0));
}
Collections.sort(list);
for(int i = 0; i < list.size(); i++ ){
if(list.get(i).id == 0){
for(int j = 1; j <= n; j++ ){
if(a[j] == 0){
a[j] = list.get(i).key;
break;
}
}
}
else{
for(int m = 1; m <= n; m++ ){
if(a[m] == list.get(i).key){
a[m] = 0;
break;
}
}
}
}
for(int i = 1; i <= n; i++ ){
System.out.print(a[i] + " ");
}
// for(Node node : list){
// System.out.print(node.time+" " + node.key+" ");
// }
}
}
class Node implements Comparable<Node>{
int key, time;
int id;
public Node(int key, int time, int id){
this.key = key;
this.time = time;
this.id = id;
}
@Override
public int compareTo(Node o){
if(this.time > o.time )
return 1;
else if(this.time == o.time){
if(this.id > o.id)
return 1;
else if(this.id == o.id){
if(this.key > o.key)
return 1;
else if(this.key == o.key)
return 0;
else
return -1;
}
else
return -1;
}
else
return -1;
}
}