这是一道牛客的dd爱框框的题
题目解析: 就是求大于x的最短子序列
我的思路:是滑动窗口
java
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int x = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
//双指针实现
int dest = 0,cur = 0,min = Integer.MAX_VALUE,temp = 0,resultIndex1 = 0,resultIndex2 = 0;
while (cur < arr.length) {
temp += arr[cur++];//进窗口
while (temp >= x){
if (min > cur - dest + 1) {
min = cur - dest + 1;
resultIndex1 = dest;
resultIndex2 = cur;
}
temp -= arr[dest++];//出窗口,并循环判断
}
}
System.out.println(resultIndex1 + 1 + " " + resultIndex2);
}
}