为了避免断优化推荐效果,今日头条天天要存储和处理海量数据。假设有这样一种场景:咱们对用户按照它们的注册时间前后来标号,对于一类文章,每一个用户都有不一样的喜爱值,咱们会想知道某一段时间内注册的用户(标号相连的一批用户)中,有多少用户对这类文章喜爱值为k。由于一些特殊的缘由,不会出现一个查询的用户区间彻底覆盖另外一个查询的用户区间(不存在L1<=L2<=R2<=R1)。java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import
java.util.*;
public
class
Main {
public
static
void
main(String[] args) {
Scanner sc =
new
Scanner(System.in);
int
userTotal = sc.nextInt();
HashMap<Integer, List<Integer>> like =
new
HashMap<>();
for
(
int
i=
1
; i<=userTotal; i++) {
int
k = sc.nextInt();
if
(like.containsKey(k)) {
List<Integer> list = like.get(k);
list.add(i);
}
else
{
List<Integer> list =
new
ArrayList<>();
list.add(i);
like.put(k, list);
}
}
int
groupTotal = sc.nextInt();
List<Integer> result =
new
ArrayList<>();
for
(
int
i=
0
; i<groupTotal; i++) {
int
low = sc.nextInt();
int
high = sc.nextInt();
int
k = sc.nextInt();
int
total =
0
;
List<Integer> list = like.get(k);
if
(list !=
null
) {
for
(Integer integer : list) {
if
(integer >= low && integer <= high) total++;
}
}
result.add(total);
}
for
(Integer integer:result) {
System.out.println(integer);
}
}
}
|
做为一个手串艺人,有金主向你订购了一条包含n个杂色串珠的手串——每一个串珠要么无色,要么涂了若干种颜色。为了使手串的色彩看起来不那么单调,金主要求,手串上的任意一种颜色(不包含无色),在任意连续的m个串珠里至多出现一次(注意这里手串是一个环形)。手串上的颜色一共有c种。如今按顺时针序告诉你n个串珠的手串上,每一个串珠用所包含的颜色分别有哪些。请你判断该手串上有多少种颜色不符合要求。即询问有多少种颜色在任意连续m个串珠中出现了至少两次。优化
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int c=sc.nextInt();
int arr[][]= new int[n][];
int arrCount[]= new int[n];
HashMap<Integer, List<Integer>> ma=new HashMap<>();
for(int i=0;i<n;i++){
int iCount=sc.nextInt();
arrCount[i]=iCount;
if(iCount==0){
;
}else{
for(int j=0;j<iCount;j++){
int x=sc.nextInt();
if(ma.containsKey(x)){
List<Integer> li= ma.get(x);
// List<Integer> list = like.get(k);
li.add(i);
}else{
List<Integer> li=new ArrayList<>();
li.add(i);
ma.put(x,li);
}
}
}
}
int flag=0;
//遍历map中的值
for (List<Integer> li: ma.values()) {
Collections.sort(li);
int f=li.get(0);
for(int k=1;k<li.size();k++){
if((f+m)>li.get(k)){
flag++;
break;
}
f=li.get(k);
}
//最后一位
}
System.out.println(flag);
}
}spa
字符串S由小写字母构成,长度为n。定义一种操做,每次均可以挑选字符串中任意的两个相邻字母进行交换。询问在至多交换m次以后,字符串中最多有多少个连续的位置上的字母相同?code