Codeforces Round #481 (Div. 3) C. Letters

题目地址:http://codeforces.com/contest/978/problem/C

题解:有n个宿舍,每个宿舍人不一样多,有m封信,每封信送给对应的第m间房间,问这封信是给第几个宿舍,第几间房的。

方法:做题目的时候没有看到信的编号是不断升高的,把题目想复杂了,wa了两次。这题把寝室的房间累加在一起,然后设一个现在送到那个寝室的变量,逐步累加算法会快很多。

代码:(代码较丑,欢迎大佬们批评指正)

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<iostream>
 7 #include<map>
 8 #include<vector>
 9 #include<set>
10 #include<queue>
11 using namespace std;
12 const int inf = 0x3f3f3f3f;
13 long long int a[250000];
14 int main()
15 {
16     int n, m;
17     scanf("%d %d", &n, &m);
18     a[0] = 0;
19     for (int i = 1; i <= n; i++)
20     {
21         long long int tmp;
22         cin >> tmp;
23         a[i] = tmp + a[i - 1];
24     }
25     long long int now = 1;
26     for (int i = 0; i < m; i++)
27     {
28         long long int b;
29         cin >> b;
30         for (; now <= n;now++)
31         {
32             if (a[now] >= b)
33             {
34                 cout << now << " " << b - a[now - 1] << endl;
35                 break;
36             }
37         }
38         
39     }
40     return 0;
41 }
原文地址:https://www.cnblogs.com/Tangent-1231/p/9041524.html