Untitled
unknown
plain_text
a year ago
7.6 kB
3
Indexable
[Problem Description] Users can give a rating to several apps on a scale ranging from 0 to 5 or delete the rating. If a particular user is considered illegal, the ratings the user gave will all be deleted. Write a program which manages an app ranking and app user reviews. Implement each required function by referring to the following API description. ※ The function signature below is based on C/C++. As for other languages, refer to the provided Main and User Code. The following is the description of API to be written in the User Code. void init(int N, const char mApp[][]) This function is called in the beginning of each test case. All existing ratings and information of illegal users are deleted. Given the number of apps to be used and their names in the test case. The names of all apps are different. The name of an app consists of uppercase English alphabet letters of minimum 5 but no larger than 15. This string ends with ‘\0.’ Parameters N : Number of apps (5 ≤ N ≤ 10,000) mApp[][] : App name (5 ≤ |mApp[i]| ≤ 15; 0 ≤ i ≤ N – 1; |a| represents the length of a string a.) void addRating(int mUser, const char mApp[], int mScore) mUser gives mApp a rating. If there is a rating that the user had given to the app, that rating is deleted. If mUser is an illegal user, such a request is ignored. Parameters mUser : User number (1 ≤ mUser ≤ 10,000) mApp[] : App name (5 ≤ |mApp| ≤ 15) mScore : Score (0 ≤ mScore ≤ 5) void deleteRating(int mUser, const char mApp[]) The rating given to mApp by mUser is deleted. It is guaranteed that mUser is not an illegal user and there is a rating that the user has given to the app. Parameters mUser : User number (1 ≤ mUser ≤ 10,000) mApp[] : App name (5 ≤ |mApp| ≤ 15) void banUser(int mUser) This function considers mUser an illegal user, thereby deleting all ratings given by the user. If mUser is an illegal user, such a request is ignored. Parameters mUser : User number (1 ≤ mUser ≤ 10,000) RESULT sortByScore() This function returns the top 5 apps in the order of their average ratings. The average rating is calculated by ignoring the second digit to the right of the decimal point (ex. 1.125 -> 1.1). If the average ratings are the same as each other, they are sorted in alphabetical order. If an app has no valid evaluations, its rating is 0. The name of an app to be returned consists of uppercase English alphabet letters of minimum 5, but no larger than 15. This string ends with ‘\0.’ Returns RESULT char mApp[][] : App name (5 ≤ |mApp[i]| ≤ 15; 0 ≤ i ≤ 4) RESULT sortByNumber() The top 5 apps are returned in the order of the number of users who have rated them except for illegal users. If they have the same ratings as each other, they are sorted in alphabetical order. Returns RESULT char mApp[][] : App name (5 ≤ |mApp[i]| ≤ 15; 0 ≤ i ≤ 4) [Constraints] 1. init() is called in the beginning of each test case. 2. For each test case, the sum of calls of the all functions is up to 100,000. [Input and Output] As the input and output are processed in the provided code in the Main, they are not processed separately in the User Code. The output result for the sample input is either “#TC number 100 or 0.” The TC is the correct answer if it is 100; the TC is the wrong answer if it is 0. package mook1; import java.util.*; class UserSolution { static final int MAXL = 16; public static class RESULT { char mApp[][] = new char[5][MAXL]; }; HashMap<String,app> map; TreeSet<app> topscore; TreeSet<app> topnumber; user user[]; void init(int N, char mApp[][]) { map = new HashMap<>(); topscore = new TreeSet<>(new byrate()); topnumber = new TreeSet<>(new bynumber()); for(int i=0;i<N;i++) { String appname = String.valueOf(mApp[i]); app a = new app(appname); topscore.add(a); topnumber.add(a); map.put(appname,a); } user = new user[10001]; for(int i=0;i<10001;i++) { user[i]= new user(); } } void addRating(int mUser, char mApp[], int mScore) { if(user[mUser].ban==0) { String s = String.valueOf(mApp); app a = map.get(s); topscore.remove(a); topnumber.remove(a); a.c++; if(user[mUser].usermap.containsKey(a)) { a.rating=a.rating-user[mUser].usermap.get(a); a.c--; } a.rating = (a.rating+mScore); user[mUser].usermap.put(a,mScore); topscore.add(a); topnumber.add(a); } } void deleteRating(int mUser, char mApp[]) { String s = String.valueOf(mApp); app a = map.get(s); topscore.remove(a); topnumber.remove(a); a.c--; a.rating=(a.rating-user[mUser].usermap.get(a)); user[mUser].usermap.remove(a); topscore.add(a); topnumber.add(a); } void banUser(int mUser) { user[mUser].ban=1; for(app a: user[mUser].usermap.keySet()) { topscore.remove(a); topnumber.remove(a); a.rating=a.rating-user[mUser].usermap.get(a); user[mUser].usermap.put(a, 0); a.c--; topscore.add(a); topnumber.add(a); } } RESULT sortByScore() { RESULT ret = new RESULT(); // System.out.println(topscore); int n=0; for(app a:topscore){ ret.mApp[n]= a.name.toCharArray(); n++; if(n==5) break; } //System.out.println(Arrays.deepToString(ret.mApp)+ " byscore"); return ret; } RESULT sortByNumber() { RESULT ret = new RESULT(); int n=0; for(app a:topnumber){ ret.mApp[n]= a.name.toCharArray(); n++; if(n==5) break; } //System.out.println(Arrays.deepToString(ret.mApp)+ " bynumber"); return ret; } } class app{ int c=0; int rating=0; String name; public app(String s) { this.name=s; } public int getscore() { if(c>0) { return (rating*10)/c; } return 0; } } class user{ int ban; HashMap<app,Integer> usermap = new HashMap<>(); } class byrate implements Comparator<app> { public int compare(app a1,app a2) { if(a1.getscore() == a2.getscore()) { return a1.name.compareTo(a2.name); } return a2.getscore() - a1.getscore(); } } class bynumber implements Comparator<app> { public int compare(app a1,app a2) { if(a1.c == a2.c) { return a1.name.compareTo(a2.name); } return a2.c - a1.c; }
Editor is loading...
Leave a Comment