Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.0 kB
2
Indexable
Never
package NewFees;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;

class UserSolution {
	class Article {
		int id;
		int Section;
		int read_count;
		int score;

		public Article(int id, int section, int read_count, int score) {
			super();
			this.id = id;
			Section = section;
			this.read_count = read_count;
			this.score = score;
		}

	}

	List<PriorityQueue<Article>> listq = new ArrayList<PriorityQueue<Article>>();
	HashMap<Integer, Article> list_article = new HashMap<Integer, UserSolution.Article>();
	HashMap<Integer, Integer> inter = new HashMap<Integer, Integer>();

	void init() {
		listq.clear();
		list_article.clear();
		inter.clear();
		for (int i = 0; i <= 9; i++) {
			PriorityQueue<Article> q = new PriorityQueue<UserSolution.Article>(
					new Comparator<Article>() {
						public int compare(Article a1, Article a2) {
							if (a1.read_count == a2.read_count)
								return a2.id - a1.id;
							else
								return a2.read_count - a1.read_count;
						}

					});
			listq.add(q);
		}
		
	}

	void addNews(int mSection, int mNewsId) {
		Article a = new Article(mNewsId, mSection, 0, 0);
		listq.get(mSection).add(a);
		list_article.put(mNewsId, a);
	}

	void eraseNews(int mNewsId) {
		Article a = list_article.get(mNewsId);
		listq.get(a.Section).remove(a);
		list_article.remove(mNewsId);
	}

	void readNews(int mUserId, int mNewsId) {
		Article a = list_article.get(mNewsId);
		listq.get(a.Section).remove(a);
		a.read_count++;
		listq.get(a.Section).add(a);
		if (inter.get(mUserId) != null) {
			if (a.Section != inter.get(mUserId)) {
				inter.remove(mUserId);
				inter.put(mUserId, a.Section);
			}
		} else
			inter.put(mUserId, a.Section);
	}

	void changeSection(int mNewsId, int mSection) {
		Article a = list_article.get(mNewsId);
		listq.get(a.Section).remove(a);
		a.Section = mSection;
		listq.get(a.Section).add(a);
	}

	private boolean check() {
		int count = 0;
		for (int i = 0; i <= 9; i++) {
			if (!listq.get(i).isEmpty())
				count++;
		}
		if (count == 0)
			return false;
		return true;
	}

	int getList(int mUserId, int mList[]) {
		Integer s = inter.get(mUserId);
		int ans = 0;
		while (ans < 10 && check()) {
			int Max = 0;
			int Max_section = 0;
			int Maxid = 0;
			for (int i = 0; i <= 9; i++) {
				if (!listq.get(i).isEmpty()) {
					Article a = listq.get(i).peek();
					if (s != null && i == s) {
						a.score = a.read_count + 10;
					} else {
						a.score = a.read_count;
					}
					if (a.score > Max || (a.score == Max && a.id > Maxid)) {
						Max = a.score;
						Maxid = a.id;
						Max_section = i;
					}
				}
			}
			Article b = listq.get(Max_section).poll();
			mList[ans] = b.id;
			ans++;
		}
		for (int i = 0; i < ans; i++) {
			Article a = list_article.get(mList[i]);
			listq.get(a.Section).add(a);
		}
		return ans;
	}

}