推荐系统(java80分 c++100分)

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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值