poj3061
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(false)
#define DEBUG cout<<endl<<"DEBUG"<<endl;
using namespace std;
int n, arr[100010], a[100010], s, ans = INF;
int main() {
int t;
cin >> t;
while(t--) {
ans = INF;
cin >> n >> s;
for(int i = 1; i <= n; i++)
cin >> arr[i], a[i] = a[i - 1] + arr[i];
int l = 1;
for(int i = 1; i <= n; i++) {//右端
while(a[i] - a[l] > s && l < i)
l++;
if(a[i] - a[l - 1] >= s) {
ans = min(ans, i - l + 1);
}
}
if(ans == INF)
ans = 0;
cout << ans << endl;
}
return 0;
}
poj3320
这道题神了,用去掉输入同步的cin都不行,第一回见还。。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<cmath>
#define INF 0x3f3f3f3f
#define ll long long
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG cout<<endl<<"DEBUG"<<endl;
using namespace std;
typedef map<int, int>::iterator ITER;
int n, arr[1000010], ans = INF;
map<int, int> m;
set<int> t;
int main() {
IOS
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", arr + i), t.insert(arr[i]);
int num = t.size();
int r = 1, nm = 0, l = 1;
while(1) {
while(nm < num && r <= n) {未得到所有知识点
if(m[arr[r++]]++ == 0)//新知识点
nm++;
}
if(nm < num)
break;
ans = min(ans, r - l);
if(--m[arr[l++]] == 0)
nm--;
}
cout << ans;
return 0;
}