小明这些天一直在思考这样一个奇怪而有趣的问题:
在 1∼N 的某个排列中有多少个连号区间呢?
这里所说的连号区间的定义是:
如果区间 [L,R] 里的所有元素(即此排列的第 L 个到第 R 个元素)递增排序后能得到一个长度为 R−L+1 的“连续”数列,则称这个区间连号区间。
当 N 很小的时候,小明可以很快地算出答案,但是当 N 变大的时候,问题就不是那么简单了,现在小明需要你的帮助。
输入格式
第一行是一个正整数 N,表示排列的规模。
第二行是 N 个不同的数字 Pi,表示这 N 个数字的某一排列。
输出格式
输出一个整数,表示不同连号区间的数目。
数据范围
1≤N≤10000,
1≤Pi≤N
输入样例1:
4
3 2 4 1
输出样例1:
7
输入样例2:
5
3 4 2 5 1
输出样例2:
9
样例解释
第一个用例中,有 7 个连号区间分别是:[1,1],[1,2],[1,3],[1,4],[2,2],[3,3],[4,4]
第二个用例中,有 9 个连号区间分别是:[1,1],[1,2],[1,3],[1,4],[1,5],[2,2],[3,3],[4,4],[5,5]
ps : 第二行是 N 个不同的数字 Pi,表示这 N 个数字的某一排列。 和 1∼N 的某个排列中,代表此中的数字不会相同,故最大值和最小值如果等于数字的个数即必定是连续的;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = sc.nextInt();
int[] a = new int[10005];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int ans = 0;
for(int i = 0; i < n; i++) {
int min = n + 1,max = 0;
for(int j = i; j < n; j++) {
max = Math.max(max, a[j]);
min = Math.min(min, a[j]);
if(j - i == max - min)ans++;
}
}
System.out.println(ans);
}
}
给定三个整数数组
A=[A1,A2,…AN],
B=[B1,B2,…BN],
C=[C1,C2,…CN],
请你统计有多少个三元组 (i,j,k) 满足:
1≤i,j,k≤N
Ai
第一行包含一个整数 N。
第二行包含 N 个整数 A1,A2,…AN。
第三行包含 N 个整数 B1,B2,…BN。
第四行包含 N 个整数 C1,C2,…CN。
输出格式
一个整数表示答案。
数据范围
1≤N≤105,
0≤Ai,Bi,Ci≤105
输入样例:
3
1 1 1
2 2 2
3 3 3
输出样例:
27
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(new BufferedInputStream(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int n = sc.nextInt();
int[] a = new int[100005];int[] b = new int[100005];int[] c = new int[100005];
int[] cnt = new int[100005];int[] s = new int[100005];
int[] as = new int[100005];int[] cs = new int[100005];
for(int i = 0; i < n; i++)a[i] = sc.nextInt() + 1;
for(int i = 0; i < n; i++)b[i] = sc.nextInt() + 1;
for(int i = 0; i < n; i++)c[i] = sc.nextInt() + 1;
//cnt[i] 代表 值等于 i 的个数,也就是说 cnt里面记录的是 a[i]大小的个数
for(int i = 0; i < n; i++)cnt[a[i]] ++;
//s[i]代表 0-i的个数,也就是说 a[i]中小于 i的 个数
for(int i = 1; i < 100005; i++)s[i] = s[i - 1] + cnt[i];
for(int i = 0; i < n; i++)as[i] = s[b[i]-1];
Arrays.fill(cnt, 0);Arrays.fill(s, 0);
//c同理
for(int i = 0; i < n; i++)cnt[c[i]] ++;
for(int i = 1; i < 100005; i++)s[i] = s[i - 1] + cnt[i];
for(int i = 0; i < n; i++)cs[i] = s[100004] - s[b[i]];
long ans = 0;
//对于每一个b[i] 只需知道比b[i]小的 a[i]有多少个 和 比 b[i]大的 c[i]有多少个相乘,即为答案;
for(int i = 0; i < n; i++)ans += (long)as[i] * (long)cs[i];
out.write(ans + "\n");
out.flush();
}
}
二分:
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(new BufferedInputStream(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int n = sc.nextInt();
int[] a = new int[100005];int[] b = new int[100005];int[] c = new int[100005];
int[] cnt = new int[100005];int[] s = new int[100005];
int[] as = new int[100005];int[] cs = new int[100005];
for(int i = 0; i < n; i++)a[i] = sc.nextInt();
for(int i = 0; i < n; i++)b[i] = sc.nextInt();
for(int i = 0; i < n; i++)c[i] = sc.nextInt();
Arrays.sort(a,0,n);
Arrays.sort(c,0,n);
long ans = 0;
for(int i = 0; i < n; i++) {
long amin = minB(a,0,n-1,b[i]);
long cmax = maxB(c,0,n-1,b[i]);
if(a[(int)amin] >= b[i] || c[(int)cmax] <= b[i])continue;
ans += (amin+1) * (n - cmax);
}
out.write(ans + "\n");
out.flush();
}
static int minB(int[] a,int l,int r, int key) {
while(l < r) {
int mid = l + r + 1>> 1;
if(key > a[mid])l = mid;
else r = mid - 1;
}
return l;
}
static int maxB(int[] a,int l,int r, int key) {
while(l < r) {
int mid = l + r >> 1;
if(key < a[mid])r = mid;
else l = mid + 1;
}
return r;
}
}
在日常生活中,通过年、月、日这三个要素可以表示出一个唯一确定的日期。
牛牛习惯用 8 位数字表示一个日期,其中,前 4 位代表年份,接下来 2 位代表月份,最后 2 位代表日期。
显然:一个日期只有一种表示方法,而两个不同的日期的表示方法不会相同。
牛牛认为,一个日期是回文的,当且仅当表示这个日期的8位数字是回文的。
现在,牛牛想知道:在他指定的两个日期之间(包含这两个日期本身),有多少个真实存在的日期是回文的。
一个 8 位数字是回文的,当且仅当对于所有的 i(1≤i≤8) 从左向右数的第i个数字和第 9−i 个数字(即从右向左数的第 i 个数字)是相同的。
例如:
•对于2016年11月19日,用 8 位数字 20161119 表示,它不是回文的。
•对于2010年1月2日,用 8 位数字 20100102 表示,它是回文的。
•对于2010年10月2日,用 8 位数字 20101002 表示,它不是回文的。
输入格式
输入包括两行,每行包括一个8位数字。
第一行表示牛牛指定的起始日期date1,第二行表示牛牛指定的终止日期date2。保证date1和date2都是真实存在的日期,且年份部分一定为4位数字,且首位数字不为0。
保证date1一定不晚于date2。
输出格式
输出共一行,包含一个整数,表示在date1和date2之间,有多少个日期是回文的。
输入样例:
20110101
20111231
输出样例:
1
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
static int[] a = {
0,31,28,31,30,31,30,31,31,30,31,30,31};
public static void main(String[] args) {
String d1 = sc.next();
String d2 = sc.next();
int date1 = Integer.valueOf(d1.substring(0,4));
int date2 = Integer.valueOf(d2.substring(0,4));
int ans = 0;
for(int i = date1; i <= date2; i++) {
if(i != date2) {
if(check(i))ans++;
}
else {
int temp = i;
if(!check(temp))continue;
int k = 0;
while(i > 0) {
k = k*10 + i % 10;
i /= 10;
}
temp = temp * 10000 + k;
if(Integer.valueOf(d1) <= temp && temp <= Integer.valueOf(d2))ans++;
break;
}
}
System.out.println(ans);
}
private static boolean check(int k) {
boolean flag = false;
if(k % 400 == 0 || (k % 4 == 0 && k % 100 != 0))flag = true;
int temp = k;
k = 0;
while(temp > 0) {
k = k*10 + temp % 10;
temp /= 10;
}
if(flag && k / 100 == 2) {
if(k % 100 <= 29)return true;
}
if(k / 100 <= 12 && k % 100 <= a[k / 100])return true;
return false;
}
}
X星球居民小区的楼房全是一样的,并且按矩阵样式排列。
其楼房的编号为 1,2,3…1,2,3…
当排满一行时,从下一行相邻的楼往反方向排号。
比如:当小区排号宽度为 66 时,开始情形如下:
1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 .....
我们的问题是:已知了两个楼号 mm 和 nn,需要求出它们之间的最短移动距离(不能斜线方向移动)。
输入共一行,包含三个整数 w,m,nw,m,n,ww 为排号宽度,m,nm,n 为待计算的楼号。
输出一个整数,表示 m,nm,n 两楼间最短移动距离。
1≤w,m,n≤100001≤w,m,n≤10000,
6 8 2
4
欧几里得距离: 平方和开根
曼哈顿距离 : 差相加
利用规律 计算出行和列,求出曼哈顿距离。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Stack;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
String[] input = bf.readLine().split(" ");
int w = Integer.valueOf(input[0]);
int m = Integer.valueOf(input[1]);
int n = Integer.valueOf(input[2]);
int n_r,n_c,m_r,m_c;
n_r = (int)Math.ceil(n*1.0/w);
if(n_r % 2 == 0) {
n_c = w * n_r - n + 1;
}else n_c = n - w * (n_r - 1);
m_r = (int)Math.ceil(m*1.0/w);
// 列为奇数,要倒置
if(m_r % 2 == 0) {
m_c = w * m_r - m + 1;
}else m_c = m - w * (m_r - 1);
int ans = Math.abs(n_r - m_r) + Math.abs(n_c - m_c);
System.out.println(ans);
}
}
小明正在整理一批历史文献。这些历史文献中出现了很多日期。
小明知道这些日期都在1960年1月1日至2059年12月31日。
令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。
更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。
给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?
一个日期,格式是”AA/BB/CC”。
即每个’/’隔开的部分由两个 0-9 之间的数字(不一定相同)组成。
输出若干个不相同的日期,每个日期一行,格式是”yyyy-MM-dd”。
多个日期按从早到晚排列。
0≤A,B,C≤90≤A,B,C≤9
02/03/04
2002-03-04
2004-02-03
2004-03-02
关于日历问题,记得定义月份的数组,和平年闰年的检查,用整数int 枚举,输出,直接用printf格式化即可。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Stack;
public class Main {
static int[] days = {
0,31,28,31,30,31,30,31,31,30,31,30,31};
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
String[] input = bf.readLine().split("/");
int a = Integer.valueOf(input[0]),b = Integer.valueOf(input[1]), c = Integer.valueOf(input[2]);
StringBuilder sb = new StringBuilder();
for(int date = 19600101; date <= 20591231; date++) {
int year = date / 10000, month = date % 10000 / 100, day = date % 100;
if(check_date(year,month,day)) {
if(year % 100 == a && month == b && day == c ||
year % 100 == c && month == b && day == a ||
year % 100 == c && month == a && day == b)
System.out.printf("%d-%02d-%02d\n",year,month,day);
}
}
}
private static boolean check_date(int year, int month,int day) {
if(month == 0 || month > 12)return false;
if(day == 0)return false;
if(month != 2) {
if(days[month] < day)return false;
}else if(month == 2){
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
if(day > days[2] + 1)return false;
}else if(day > days[2]) return false;
}
return true;
}
}
小 hh 前往美国参加了蓝桥杯国际赛。
小 hh 的女朋友发现小 hh 上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”。
小 hh 对超音速飞行感到十分恐惧。
仔细观察后发现飞机的起降时间都是当地时间。
由于北京和美国东部有 1212 小时时差,故飞机总共需要 1414 小时的飞行时间。
不久后小 hh 的女朋友去中东交换。
小 hh 并不知道中东与北京的时差。
但是小 hh 得到了女朋友来回航班的起降时间。
小 hh 想知道女朋友的航班飞行时间是多少。
对于一个可能跨时区的航班,给定来回程的起降时间。
假设飞机来回飞行时间相同,求飞机的飞行时间。
一个输入包含多组数据。
输入第一行为一个正整数 TT,表示输入数据组数。
每组数据包含两行,第一行为去程的起降时间,第二行为回程的起降时间。
起降时间的格式如下:
第一种格式表示该航班在当地时间h1时m1分s1秒起飞,在当地时间当日h2时m2分s2秒降落。
第二种格式表示该航班在当地时间h1时m1分s1秒起飞,在当地时间次日h2时m2分s2秒降落。
第三种格式表示该航班在当地时间h1时m1分s1秒起飞,在当地时间第三日h2时m2分s2秒降落。
对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。
注意,当时间为一位数时,要补齐前导零,如三小时四分五秒应写为03:04:05。
保证输入时间合法(0≤h≤23,0≤m,s≤590≤h≤23,0≤m,s≤59),飞行时间不超过24小时。
3
17:48:19 21:57:24
11:05:18 15:14:23
17:21:07 00:31:46 (+1)
23:02:41 16:13:20 (+1)
10:19:19 20:41:24
22:19:04 16:41:09 (+1)
04:09:05
12:10:39
14:22:05
时间问题:转换为毫秒去算,并且用printf 格式化输出。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Stack;
public class Main {
static int[] days = {
0,31,28,31,30,31,30,31,31,30,31,30,31};
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
int T = Integer.valueOf(bf.readLine());
while(T-- > 0) {
String[] take1 = bf.readLine().split(" ");
String[] take2 = bf.readLine().split(" ");
int time = (sub(take1) + sub(take2)) / 2;
int h = time / 3600, m = time % 3600 / 60, s = time % 60;
// StringBuilder sb = new StringBuilder();
// if(h < 10)sb.append("0" + h + ":");
// else sb.append(h + ":");
// if(m < 10)sb.append("0" + m + ":");
// else sb.append(m + ":");
// if(s < 10)sb.append("0" + s);
// else sb.append(s);
System.out.printf("%02d:%02d:%02d\n",h,m,s);
}
}
private static int sub(String[] input) {
int h_b = Integer.valueOf(input[1].substring(0,2));
int m_b = Integer.valueOf(input[1].substring(3,5));
int s_b = Integer.valueOf(input[1].substring(6,8));
int h_c = Integer.valueOf(input[0].substring(0,2));
int m_c = Integer.valueOf(input[0].substring(3,5));
int s_c = Integer.valueOf(input[0].substring(6,8));
if(input.length > 2)
return get_second(h_b, m_b, s_b) - get_second(h_c, m_c, s_c) + Integer.valueOf(input[2].substring(2,3)) * 24 * 3600;
else return get_second(h_b, m_b, s_b) - get_second(h_c, m_c, s_c);
}
private static int get_second(int h, int m, int s) {
return h * 3600 + m * 60 + s;
}
}
“饱了么”外卖系统中维护着 NN 家外卖店,编号 1∼N1∼N。
每家外卖店都有一个优先级,初始时 (00 时刻) 优先级都为 00。
每经过 11 个时间单位,如果外卖店没有订单,则优先级会减少 11,最低减到 00;而如果外卖店有订单,则优先级不减反加,每有一单优先级加 22。
如果某家外卖店某时刻优先级大于 55,则会被系统加入优先缓存中;如果优先级小于等于 33,则会被清除出优先缓存。
给定 TT 时刻以内的 MM 条订单信息,请你计算 TT 时刻时有多少外卖店在优先缓存中。
第一行包含 33 个整数 N,M,TN,M,T。
以下 MM 行每行包含两个整数 tsts 和 idid,表示 tsts 时刻编号 idid 的外卖店收到一个订单。
输出一个整数代表答案。
1≤N,M,T≤1051≤N,M,T≤105,
1≤ts≤T1≤ts≤T,
1≤id≤N1≤id≤N
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
1
66 时刻时,11 号店优先级降到 33,被移除出优先缓存;22 号店优先级升到 66,加入优先缓存。
所以是有 11 家店 (22 号) 在优先缓存中。
由题得 数据在 10^5, 不能用O(n^2)会超时。
也就是说不能 店铺编号,嵌套时间的循环。
故要优化代码,优化 时间这个循环,一个店家,两个订单之间可能有很多时间单位是没有订单的,只需要记录有订单的时间,当到下一个订单时,减去上一个有订单的时间,则是中间空缺的时间。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static final int N = 100100;
static pill[] order = new pill[N];
static int[] score = new int[N];
static int[] last = new int[N];
static boolean[] st = new boolean[N];
public static void main(String[] args) throws IOException {
String[] input = bf.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);
int T = Integer.parseInt(input[2]);
for(int i = 0; i < m; i++) {
String[] str = bf.readLine().split(" ");
order[i] = new pill(Integer.parseInt(str[0]),Integer.parseInt(str[1]));
}
Arrays.sort(order,0,m);
// 循环订单数
for(int i = 0; i < m;) {
int j = i;
// 如果订单 时间 和店铺 是同一家,累加到一起处理
while(j < m && order[i].t == order[j].t && order[i].id == order[j].id)j++;
int id = order[i].id, t = order[i].t, cnt = j - i;
i = j;
if(last[id] < t) score[id] -= t - last[id] - 1;
if(score[id] < 0)score[id] = 0;
if(score[id] <= 3)st[id] = false;
score[id] += cnt * 2;
if(score[id] > 5)st[id] = true;
// 记录上一次 订单时间
last[id] = t;
}
// 循环每个店铺, 最后一次订单,到 最终时刻T 之间空闲的时间。
for(int i = 1; i <= n; i++) {
if(last[i] < T)score[i] -= T - last[i];
if(score[i] <= 3)st[i] = false;
}
int res = 0;
for(int i = 1; i <= n; i++)
if(st[i]) res ++;
System.out.println(res);
}
}
class pill implements Comparable<pill>{
int t;
int id;
pill(int t, int id){
this.t = t;
this.id = id;
}
@Override
public int compareTo(pill o) {
if(this.t != o.t)return this.t - o.t;
else return this.id - o.id;
}
}
给定一个长度为n的整数数列,请你计算数列中的逆序对的数量。
逆序对的定义如下:对于数列的第 i 个和第 j 个元素,如果满足 i < j 且 a[i] > a[j],则其为一个逆序对;否则不是。
第一行包含整数n,表示数列的长度。
第二行包含 n 个整数,表示整个数列。
输出一个整数,表示逆序对的个数。
1≤n≤1000001≤n≤100000
6
2 3 4 5 6 1
5
可以用冒泡,归并,和树状数组,这里用最好写的归并(主要是其它的我也不知道啊)。
说说归并排序求逆序对的思路:
- 归并两边(l,r) – > (l,mid), (mid + 1 , r);
- 将(l,mid), (mid + 1 , r); 有序的组合到一个数组中(利用双指针)
- 将第二步临时开辟的数组,回写到原数组
上面是归并的思路,而主要做手脚求逆序对就是在第二步上。
此时有(l,mid), (mid + 1 , r) 左右两边,当左边位置为i的数 大于 右边 位置为j的数, 则代表 i后面 的所有数 都比 位置j上的数大,故 res += mid - i + 1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static Scanner sc = new Scanner(System.in);
static final int N = (int)1e5 + 10;
static int[] a = new int[N];
static int[] tmp = new int[N];
static long res = 0;
public static void main(String[] args) {
int n = sc.nextInt();
for(int i = 0; i < n; i++)a[i] = sc.nextInt();
merge_sort(0, n - 1, a);
System.out.println(res);
}
private static void merge_sort(int l, int r,int[] q) {
if(l >= r)return ;
int mid = l + r >> 1;
merge_sort(l, mid,q);merge_sort(mid + 1, r,q);
int i = l, j = mid + 1, k = 0;
while(i <= mid && j <= r) {
if(q[i] <= q[j])tmp[k++] = q[i++];
else {
tmp[k++] = q[j++];
res += mid - i + 1;
}
}
while(i <= mid)tmp[k++] = q[i++];
while(j <= r) tmp[k++] = q[j++];
for(i = l, k = 0; i <= r;) q[i++] = tmp[k++];
}
}