Codeforces Round #481 (Div. 3) C. Letters (模拟,二分)
-
题意:有个\(n\)个公寓,每个公寓\(a_{i}\)代表着编号为\(1-a_{i}\)个房间,给你房间号,问它在第几栋公寓的第几个房间.
-
题解:对每个公寓的房间号记一个前缀和,二分查找属于第几个公寓,然后求个差即可.
-
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <vector> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #define ll long long #define fi first #define se second #define pb push_back #define me memset const int N = 1e6 + 10; const int mod = 1e9 + 7; using namespace std; typedef pair<int,int> PII; typedef pair<long,long> PLL; int n,m; ll a[N],b[N]; int main() { ios::sync_with_stdio(false);cin.tie(0); cin>>n>>m; for(int i=1;i<=n;++i){ cin>>a[i]; a[i]+=a[i-1]; } for(int i=1;i<=m;++i) cin>>b[i]; for(int i=1;i<=m;++i){ ll pos=lower_bound(a+1,a+1+n,b[i])-a; printf("%lld %lld\n",pos,b[i]-a[pos-1]); } return 0; }
𝓐𝓬𝓱𝓲𝓮𝓿𝓮𝓶𝓮𝓷𝓽 𝓹𝓻𝓸𝓿𝓲𝓭𝓮𝓼 𝓽𝓱𝓮 𝓸𝓷𝓵𝔂 𝓻𝓮𝓪𝓵
𝓹𝓵𝓮𝓪𝓼𝓾𝓻𝓮 𝓲𝓷 𝓵𝓲𝓯𝓮