After each PAT, the PAT Center will announce the ranking of institutions based on their students’ performances. Now you are asked to generate the ranklist.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format: ID Score School where “ID” is a string of 6 characters with the first one representing the test level: “B” stands for the basic level, “A” the advanced level and “T” the top level; “Score” is an integer in [0, 100]; and “School” is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that “ID” is unique for each testee.

Output Specification:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format: Rank School TWS Ns where “Rank” is the rank (start from 1) of the institution; “School” is the institution code (all in lower case); “TWS” is the total weighted score which is defined to be the integer part of “ScoreB/1.5 + ScoreA + ScoreT*1.5”, where “ScoreX” is the total score of the testees belong to this institution on level X; and “Ns” is the total number of testees who belong to this institution. The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

Sample Input:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

常规题,使用map映射校名与总分以及人数,之后放到数组里面进行排序输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cctype>

using namespace std;

string tolow(string name) {
for (int i = 0; i < name.size(); i++)
if (isupper(name[i]))
name[i] = tolower(name[i]);
return name;
}

struct school {
string name;
int tws, ns;
} schools[100010];

int main() {
int n = 0, score = 0, rank = 1, index = 0;
scanf("%d", &n);
char id[8], name[8];
map<string, double> mtws;
map<string, int> mns;
for (int i = 0; i < n; i++) {
scanf("%s %d %s", id, &score, name);
string lowname = tolow(name);
if (id[0] == 'A') {
mtws[lowname] += score;
} else if (id[0] == 'B') {
mtws[lowname] += score / 1.5;
} else if (id[0] == 'T') {
mtws[lowname] += score * 1.5;
}
mns[lowname]++;
}
printf("%lu\n", mtws.size());
for (auto it = mtws.begin(); it != mtws.end(); it++) {
schools[index++] = school{it->first, (int) it->second, mns[it->first]};
}
sort(schools, schools + index, [](school a, school b) {
return a.tws == b.tws ? (a.ns == b.ns ? a.name < b.name : a.ns < b.ns) : a.tws > b.tws;
});
for (int i = 0; i < index; i++) {
if (i > 0 && schools[i].tws != schools[i - 1].tws)
rank = i + 1;
printf("%d %s %d %d\n", rank, schools[i].name.c_str(), schools[i].tws, schools[i].ns);
}
return 0;
}

  Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour. Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤10​4​​) - the total number of customers, and K (≤100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time. Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:

7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:

8.2

将给出的数据按照时间的先后顺序排列,可以确定为他们服务的次序。 用优先队列去处理,如果队列还可以插入数据(即窗口没满),就直接插入;如果不能插入数据,就弹出优先队列的第一个元素,将要插入的数据与这个弹出来的元素进行比较,可以确定要插入数据等待的时间。 注意:8点前到达的要累加等待的时间,17点后到达的不处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

struct node {
int time, p;
};

bool cmp(struct node a, struct node b) {
return a.time < b.time;
}

int main() {
int n = 0, k = 0, cnt = 0, waiting = 0;
scanf("%d %d", &n, &k);
struct node *arr = new struct node[n];
for (int i = 0; i < n; i++) {
int hour = 0, minute = 0, second = 0;
scanf("%d:%d:%d %d", &hour, &minute, &second, &arr[i].p);
arr[i].time = hour * 3600 + minute * 60 + second;
arr[i].p *= 60;
}
sort(arr, arr + n, cmp);
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 0; i < n; i++) {
if (arr[i].time > 17 * 3600) {
break;
} else {
cnt++;
if (q.size() < k) {
if (arr[i].time < 8 * 3600) {
waiting += 8 * 3600 - arr[i].time;
arr[i].time = 8 * 3600;
}
q.push(arr[i].time + arr[i].p);
} else {
int temp = q.top();
q.pop();
if (arr[i].time < temp) {
waiting += temp - arr[i].time;
q.push(temp + arr[i].p);
} else {
q.push(arr[i].time + arr[i].p);
}
}
}
}
printf("%.1f", waiting * 1.0 / cnt / 60);
delete[] arr;
return 0;
}

A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Input Specification:

Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records. The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day. The next line contains a positive number N (≤1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (mm:dd:hh:mm), and the word on-lineor off-line. For each test case, all dates will be within a single month. Each on-line record is paired with the chronologically next record for the same customer provided it is an off-line record. Any on-line records that are not paired with an off-line record are ignored, as are off-line records not paired with an on-line record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer. Bills must be printed in alphabetical order of customers’ names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:hh:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

Sample Input:

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line

Sample Output:

CYJJ 01
01:05:59 01:07:00 61 $12.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80

将给出的数据先按照姓名排序,再按照时间的先后顺序排列,这样遍历的时候,前后两个名字相同且前面的状态为on-line后面一个的状态为off-line的就是合格数据~ 计算费用从00:00:00到dd:hh:mm计算可以避免跨天的问题,比如01:12:00到02:02:00(最后一个测试点)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>
#include <vector>

using namespace std;

struct node {
char name[22], status[10];
int month, time, day, hour, minute;
};

int cmp(const void *a, const void *b) {
struct node an = *static_cast<const struct node *>(a);
struct node bn = *static_cast<const struct node *>(b);
int res = strcmp(an.name, bn.name);
return res != 0 ? res : an.time - bn.time;
}

double billFromZero(struct node call, int *rate) {
double total = rate[call.hour] * call.minute;
for (int i = 0; i < call.hour; i++) {
total += rate[i] * 60;
}
total += rate[24] * 60 * call.day;
return total / 100.0;
}

int main() {
int rate[25] = {0};
for (int i = 0; i < 24; i++) {
scanf("%d", &rate[i]);
rate[24] += rate[i];
}
int n = 0;
scanf("%d", &n);
struct node *data = new struct node[n];
for (int i = 0; i < n; i++) {
scanf("%s %d:%d:%d:%d %s", data[i].name,
&data[i].month, &data[i].day, &data[i].hour, &data[i].minute, data[i].status);
data[i].time = data[i].day * 24 * 60 + data[i].hour * 60 + data[i].minute;
}
qsort(data, n, sizeof(data[0]), cmp);

map<string, vector<struct node>> custom;
for (int i = 1; i < n; i++) {
if (strcmp(data[i].name, data[i - 1].name) == 0 &&
strcmp(data[i - 1].status, "on-line") == 0 &&
strcmp(data[i].status, "off-line") == 0) {
custom[data[i - 1].name].push_back(data[i - 1]);
custom[data[i].name].push_back(data[i]);
}
}

for (auto it : custom) {
vector<struct node> temp = it.second;
printf("%s %02d\n", it.first.c_str(), temp[0].month);
double total = 0;
for (int i = 1; i < temp.size(); i += 2) {
double t = billFromZero(temp[i], rate) - billFromZero(temp[i - 1], rate);
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
temp[i - 1].day, temp[i - 1].hour, temp[i - 1].minute,
temp[i].day, temp[i].hour, temp[i].minute,
temp[i].time - temp[i - 1].time, t);
total += t;
}
printf("Total amount: $%.2f\n", total);
}
delete[] data;
return 0;
}

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customer[i] will take T[i] minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done. For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line. At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.

Input

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer queries). The next line contains K positive integers, which are the processing time of the K customers. The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

Output

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output “Sorry” instead.

Sample Input

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

Sample Output

08:07
08:06
08:10
17:00
Sorry

在K个顾客中,前M * N个顾客是在黄线以内的,在窗口的排列顺序如:1号窗口分别是1, 1 + N, 1 + 2N,…, 1+ N * (M - 1);2号窗口是2, 2 + N, 2 + 2N,…, 2 + N * (M - 1); N号窗口是:N, N + N, N + 2N,…,N + N (M - 1)。 在后K - M*N个顾客组成的队列中,队列中第一个顾客总是排在所有窗口前第一个顾客先结束的窗口。 对顾客建立一个结构体custmer,分别包含顾客需要的服务时间st和顾客离开的时间lt,由此可以得到顾客开始被服务的时间为lt - st。如果lt - st >= 540即表示顾客在17点之后被服务,应该输出的是Sorry. popEarlyFormWins函数总是将所有窗口当中,最早结束的那个顾客从窗口队列中弹出,并返回这个窗口的下标。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

struct custmer {
int st, lt; // service and lift time
};

vector<custmer> custmers;
vector<queue<int>> wins;

int popEarlyFormWins() {
int early = 0;
for (int i = 1; i < wins.size(); i++) {
if (custmers[wins[i].front()].lt < custmers[wins[early].front()].lt) {
early = i;
}
}

wins[early].pop();
return early;
}

int main() {
int n = 0, m = 0, k = 0, q = 0;
scanf("%d %d %d %d", &n, &m, &k, &q);
custmers.resize(k);
for (int i = 0; i < k; i++) {
scanf("%d", &custmers[i].st);
}

wins.resize(n);
for (int i = 0; i < m * n && i < k; i++) {
if (i < n) {
custmers[i].lt = custmers[i].st;
} else {
int index = wins[i % n].back();
custmers[i].lt = custmers[index].lt + custmers[i].st;
}
wins[i % n].push(i);
}

for (int i = m * n; i < k; i++) {
int early = popEarlyFormWins();
int index = wins[early].back();
custmers[i].lt = custmers[index].lt + custmers[i].st;
wins[early].push(i);
}

int index = 0;
for (int i = 0; i < q; i++) {
scanf("%d", &index);
if (custmers[index - 1].lt - custmers[index - 1].st >= 540) {
printf("Sorry\n");
} else {
printf("%02d:%02d\n", custmers[index - 1].lt / 60 + 8, custmers[index - 1].lt % 60);
}
}

return 0;
}

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly. For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

Input

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input

3 2 3
1 2
1 3
1 2 3

Sample Output

1
0
0

这个题目的本质是求有几个连通图,每两个连通图添加一条边就可以使图连通了 求连通图可以使用dfs,从某个结点进入,遍历完一圈(可能无法遍历整个图)就是一个连通子图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int> > cities;
int *visited;

void dfs(int cur, int ban) {
for (int i = 0; i < cities[cur].size(); i++) {
int c = cities[cur][i];
if (!visited[c] && c != ban) {
visited[c] = 1;
dfs(c, ban);
}
}
}

int main() {
int n = 0, m = 0, k = 0;
scanf("%d %d %d", &n, &m, &k);
cities.resize(n + 1);
visited = new int[n + 1];
for (int i = 0; i < m; i++) {
int a = 0, b = 0;
scanf("%d %d", &a, &b);
cities[a].push_back(b);
cities[b].push_back(a);
}

for (int i = 0; i < k; i++) {
int t = 0, cnt = -1;
scanf("%d", &t);

fill(visited, visited + n + 1, 0);
for (int j = 1; j <= n; j++) {
if (!visited[j] && j != t) {
cnt++;
visited[j] = 1;
dfs(j, t);
}
}
printf("%d\n", cnt);
}

return 0;
}

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets. Chinese Football Lottery provided a “Triple Winning” game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results – namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner’s odd would be the product of the three odds times 65%. For example, 3 games’ odds are given as the following:

W T L
1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).

Input

Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

Output

For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

Sample Input

1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1

Sample Output

T T W 37.98

每次找到wtl中的最大值,输出字母,累乘它的值,计算结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main() {
double profile = 1, w = 0, t = 0, l = 0;
for (int i = 0; i < 3; i++) {
scanf("%lf %lf %lf", &w, &t, &l);
if (w > t && w > l) {
printf("W ");
profile *= w;
} else if (t > w && t > l) {
printf("T ");
profile *= t;
} else if (l > t && l > w) {
printf("L ");
profile *= l;
}
}
printf("%.2f", (profile * 0.65 - 1) * 2);
return 0;
}

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number. Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers: N1 N2 tag radix Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

convert函数的作用是给定一个数值和一个进制,将它转化为10进制,转化的过程有可能产生溢出。 find_radix函数的作用是找到令两个数值相等的进制数。在查找的过程中,需要使用二分查找算法,如果使用当前进制转化得到数值比另一个大或者小于0说明这个进制太大。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <cmath>

using namespace std;

long long convert(string n, long long radix) {
long long sum = 0;
int index = 0, temp = 0;
for (auto it = n.rbegin(); it != n.rend(); it++) {
temp = isdigit(*it) ? *it - '0' : *it - 'a' + 10;
sum += temp * pow(radix, index++);
}
return sum;
}

long long find_radix(string n, long long num) {
char it = *max_element(n.begin(), n.end());

long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
long long high = max(num, low);

while (low <= high) {
long long mid = (low + high) / 2;
long long t = convert(n, mid);
if (t < 0 || t > num) high = mid - 1;
else if (t == num) return mid;
else low = mid + 1;
}

return -1;
}


int main() {
string n1, n2;
long long tag = 0, radix = 0, result_radix;
cin >> n1 >> n2 >> tag >> radix;

result_radix = tag == 1 ? find_radix(n2, convert(n1, radix)) : find_radix(n1, convert(n2, radix));

if (result_radix != -1) {
printf("%lld", result_radix);
} else {
printf("Impossible");
}

return 0;
}

This time, you are supposed to find A*B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 3 3.6 2 6.0 1 1.6

多项式乘以多项式,多项式的指数最多是1000,则多项式的乘积指数最大应该不超过2000,用数组的下标表示指数,方便计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdio>

int main() {
double poly[1001] = {0.0}, result[2001] = {0.0}, an = 0;
int k = 0, n = 0, cnt = 0;
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d %lf", &n, &an);
poly[n] = an;
}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d %lf", &n, &an);
for (int i = 0; i <= 1000; i++) {
result[i + n] += poly[i] * an;
}
}

for (int i = 2000; i >= 0; i--) {
if (result[i] != 0.0) {
cnt++;
}
}
printf("%d", cnt);

for (int i = 2000; i >= 0; i--) {
if (result[i] != 0.0) {
printf(" %d %.1f", i, result[i]);
}
}

return 0;
}

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop. For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

小模拟题,进行简单的累加即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdio>

int main() {
int n = 0, t = 0, cur = 0, time = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &t);
if (t > cur) {
time += (t - cur) * 6 + 5;
} else {
time += (cur - t) * 4 + 5;
}
cur = t;
}
printf("%d", time);
return 0;
}

Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20. Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

记tsum为部分和,sum为结果和。从下标0开始,部分和累加当前下标的数组值,如果这个部分和小于0,则重置状态(tsum = 0, tstart=i+1)。如果这个部分和比结果和要大,就需要更新结果和的值和状态并记录当前位置为结果和的最终位置。all_neg表示数组中的值是否都是负数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>

int main() {
int k = 0, sum = -1, tsum = 0, start = 0, tstart = 0, end = 0, all_neg = 1;
scanf("%d", &k);
int *num = new int[k];
for (int i = 0; i < k; i++) {
scanf("%d", &num[i]);
if (num[i] >= 0) all_neg = 0;
tsum += num[i];
if (tsum < 0) {
tsum = 0;
tstart = i + 1;
} else if (tsum > sum) {
sum = tsum;
end = i;
start = tstart;
}
}
if (all_neg) printf("0 %d %d", num[0], num[k - 1]);
else printf("%d %d %d", sum, num[start], num[end]);
return 0;
}
0%