ผลต่างระหว่างรุ่นของ "01204212/like"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย ': ''This is part of 01204212'' You want to keep track of the number of "likes" each status has. In the beginning, every status ha...') |
Jittat (คุย | มีส่วนร่วม) |
||
| (ไม่แสดง 6 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน) | |||
| แถว 1: | แถว 1: | ||
: ''This is part of [[01204212]]'' | : ''This is part of [[01204212]]'' | ||
| − | You want to keep track of the number of "likes" each status has. In the beginning, every status has 0 likes. | + | You want to keep track of the number of "likes" each status has. In the beginning, every status has 0 likes. You are given a sequence of like clicks (as a sequence of status IDs). |
| + | |||
| + | '''Input''' | ||
| + | |||
| + | * First line: an integer '''N''' (1<='''N'''<=100,000) | ||
| + | * The next '''N''' lines: each line contains an integer '''x''', the ID of the post clicked. | ||
| + | |||
| + | '''Output''' | ||
| + | |||
| + | There are '''N''' lines of output, each contains the current number of likes the post ID '''x''' has so far (including this one). | ||
| + | |||
| + | == Example == | ||
| + | |||
| + | '''Input''' | ||
| + | |||
| + | <pre> | ||
| + | 10 | ||
| + | 4 | ||
| + | 2 | ||
| + | 4 | ||
| + | 10 | ||
| + | 100 | ||
| + | 30 | ||
| + | 10 | ||
| + | 35 | ||
| + | 4 | ||
| + | 200 | ||
| + | </pre> | ||
| + | |||
| + | '''Output''' | ||
| + | |||
| + | <pre> | ||
| + | 1 | ||
| + | 1 | ||
| + | 2 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 2 | ||
| + | 1 | ||
| + | 3 | ||
| + | 1 | ||
| + | </pre> | ||
| + | |||
| + | == Test data == | ||
| + | |||
| + | Use the test data for [[01204212/id request|ID requests]] | ||
| + | |||
| + | Download at: [http://theory.cpe.ku.ac.th/~jittat/courses/01204212/tasks/id-requests/ here] | ||
| + | |||
| + | Solution to <tt>n10.in</tt> | ||
| + | |||
| + | <pre> | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 2 | ||
| + | 1 | ||
| + | 2 | ||
| + | </pre> | ||
| + | |||
| + | Solution to <tt>n1000.in</tt> (first 20 lines) | ||
| + | |||
| + | <pre> | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 2 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 1 | ||
| + | 2 | ||
| + | 1 | ||
| + | 4 | ||
| + | 2 | ||
| + | 1 | ||
| + | 1 | ||
| + | </pre> | ||
| + | |||
| + | == Code == | ||
| + | |||
| + | '''Collections:''' you will have to use [https://docs.oracle.com/javase/7/docs/api/java/util/Map.html the Map interface]. These are implementations: | ||
| + | |||
| + | * [https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html TreeMap] | ||
| + | * [http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html HashMap] | ||
| + | |||
| + | You can use the same Main class from task [[01204212/id request|ID request]] | ||
| + | |||
| + | == Code for your own implementation of binary search trees == | ||
| + | === TreeNode === | ||
| + | <syntaxhighlight lang="java"> | ||
| + | public class TreeNode { | ||
| + | public int key; | ||
| + | public int val; | ||
| + | public TreeNode leftChild; | ||
| + | public TreeNode rightChild; | ||
| + | |||
| + | public TreeNode(int k, int v) { | ||
| + | key = k; | ||
| + | val = v; | ||
| + | leftChild = rightChild = null; | ||
| + | } | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === An example of find === | ||
| + | <syntaxhighlight lang="java"> | ||
| + | private TreeNode find(TreeNode node, int key) { | ||
| + | if(node == null) { | ||
| + | return null; | ||
| + | } | ||
| + | if(node.key == key) { | ||
| + | return node; | ||
| + | } else if(node.key < key) { | ||
| + | return find(node.rightChild, key); | ||
| + | } else { | ||
| + | return find(node.leftChild, key); | ||
| + | } | ||
| + | } | ||
| + | </syntaxhighlight> | ||
รุ่นแก้ไขปัจจุบันเมื่อ 04:48, 6 ตุลาคม 2559
- This is part of 01204212
You want to keep track of the number of "likes" each status has. In the beginning, every status has 0 likes. You are given a sequence of like clicks (as a sequence of status IDs).
Input
- First line: an integer N (1<=N<=100,000)
- The next N lines: each line contains an integer x, the ID of the post clicked.
Output
There are N lines of output, each contains the current number of likes the post ID x has so far (including this one).
เนื้อหา
Example
Input
10 4 2 4 10 100 30 10 35 4 200
Output
1 1 2 1 1 1 2 1 3 1
Test data
Use the test data for ID requests
Download at: here
Solution to n10.in
1 1 1 1 1 1 1 2 1 2
Solution to n1000.in (first 20 lines)
1 1 1 1 1 2 1 1 1 1 1 2 3 1 2 1 4 2 1 1
Code
Collections: you will have to use the Map interface. These are implementations:
You can use the same Main class from task ID request
Code for your own implementation of binary search trees
TreeNode
public class TreeNode {
public int key;
public int val;
public TreeNode leftChild;
public TreeNode rightChild;
public TreeNode(int k, int v) {
key = k;
val = v;
leftChild = rightChild = null;
}
}
An example of find
private TreeNode find(TreeNode node, int key) {
if(node == null) {
return null;
}
if(node.key == key) {
return node;
} else if(node.key < key) {
return find(node.rightChild, key);
} else {
return find(node.leftChild, key);
}
}