80分代码,原本打算熟悉一下java,结果写吐了,又臭又长,明天试试c++
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
static TreeSet<Node> set = new TreeSet<Node>();
static HashMap<Pair, Integer> map = new HashMap<Pair, Integer>();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) {
int m, n;
m = InputReader.nextInt();
n = InputReader.nextInt();
for (int i = 0; i < n; i++) {
int id, score;
id = InputReader.nextInt();
score = InputReader.nextInt();
for (int j = 0; j < m; j++) {
set.add(new Node(j, id, score));
map.put(new Pair(j, id), score);
}
}
int ops = InputReader.nextInt();
for (int i = 0; i < ops; i++) {
int idx = InputReader.nextInt();
if (idx == 1) {
int type = InputReader.nextInt();
int com = InputReader.nextInt();
int score = InputReader.nextInt();
set.add(new Node(type, com, score));
map.put(new Pair(type, com), score);
} else if (idx == 2) {
int type = InputReader.nextInt();
int com = InputReader.nextInt();
Pair p = new Pair(type, com);
Node nd = new Node(type, com, map.get(p));
set.remove(nd);
map.remove(p);
} else {
int[] arr = new int[m + 1];
arr[m] = InputReader.nextInt();
for (int j = 0; j < m; j++)
arr[j] = InputReader.nextInt();
ArrayList[] res = new ArrayList[m];
for (int j = 0; j < m; j++)
res[j] = new ArrayList<Integer>();
for (Node nd : set) {
if (arr[m] > 0 && arr[nd.a] > 0) {
arr[m]--;
arr[nd.a]--;
res[nd.a].add(nd.b);
}
}
for (int j = 0; j < m; j++) {
if (res[j].size() == 0)
out.println(-1);
else {
for (int x = 0; x < res[j].size(); x++)
out.printf(res[j].get(x) + " ");
out.println();
}
}
}
}
out.flush();
out.close();
}
static class Node implements Comparable<Node> {
int a, b, c;// 类,编号,价值
public Node() {
}
public Node(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public int compareTo(Node nd) {
if (c != nd.c)
return nd.c - c;
if (a != nd.a)
return a - nd.a;
return b - nd.b;
}
}
static class Pair {
int x, y;
public Pair(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
static class InputReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = null;
static String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
static int nextInt() {
return Integer.parseInt(next());
}
}
}
c++100分出来了,跟java思路一样,还没优化输入输出就可以通过了,代码还短,不过map中不能存放pair有点不爽,
#include<iostream>
#include<set>
#include<unordered_map>
#include<vector>
using namespace std;
const int M = 55;
int arr[M];
struct nd {
int a, b, c;
bool operator <(const nd nd1)const {
if (c != nd1.c)return nd1.c < c;
if (a != nd1.a)return a < nd1.a;
return b < nd1.b;
}
};
set<nd>st;
//unordered_map<pair<int, int>, int> mp;
unordered_map<int, int>mp[M];
int main() {
int m, n, op;
cin >> m >> n;
while (n--) {
int id, score;
cin >> id >> score;
for (int i = 0; i < m; i++) {
st.insert({ i,id,score });
mp[i][id]=score;
}
}
cin >> op;
while (op--) {
int t;
cin >> t;
int type, com, score;
switch (t) {
case 1:
cin >> type >> com >> score;
st.insert({ type,com,score });
mp[type][com] = score;
break;
case 2:
cin >> type >> com;
st.erase({ type,com,mp[type][com] });
break;
case 3:
cin >> arr[m];
for (int i = 0; i < m; i++)cin >> arr[i];
vector<int> res[M];
for (auto t : st) {
if (arr[m] == 0)break;
if (!arr[t.a])continue;
arr[t.a]--;
arr[m]--;
res[t.a].push_back(t.b);
}
for (int i = 0; i < m; i++) {
if (res[i].empty())cout << -1 << endl;
else {
for (int x : res[i])cout << x << ' ';
cout << endl;
}
}
break;
}
}
}