题意:求时间num是在第几首歌,看到这么大的数据,就用二分做,做不出来,二分实在是掌握的不好,然后就用搜索做,之后有尝试了二分的方法,切记不一样的题,边界的判断是不一样的
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 100005; long long arr[MAXN],brr[MAXN],crr[MAXN]; int n,m; int main(){ scanf("%d%d",&n,&m); arr[0] = 0; for (int i = 1; i <= n; i++){ long long a,n; scanf("%lld%lld",&brr[i],&crr[i]); arr[i] = arr[i-1] + brr[i]*crr[i]; } int cur=0; long long num,top=0; for (int i = 1; i <= m; i++){ scanf("%lld",&num); while (num > top){ cur++; top += brr[cur] * crr[cur]; } printf("%d\n",cur); } return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAXN = 200000; long long a[MAXN]; int n,m; int find(long long num){ int left=0,right=n+1; while (right > left+1){ int mid = (left+right)/2; if (a[mid]<num) left = mid; else right = mid;; } return right; } int main(){ while (scanf("%d%d",&n,&m) != EOF){ long long a,b; a[0] = 0; for (int i = 1; i <= n; i++){ scanf("%lld%lld",&a,&b); a[i] = a[i-1] + a*b; } long long num; for(int i = 1; i <= m; i++){ scanf("%lld",&num); int pos=find(num); printf("%d\n",pos); } } return 0; }