校内友谊赛中等难度题目小记

Problem E : Easy Project

Time Limit: 2 s


Description

Mia and her friends are preparing for the project that the professor has given them!

They received a huge project, indeed, and this entire project can be divided into n tasks, numbered 1, 2, ..., n. It takes anyone (who is assigned the task) wi units of time to finish the i-th task (1 ≤ i ≤ n).

There are m dependencies between these tasks, i.e. some tasks must be finished before anyone can start a certain task.

It is time to assign the tasks. They want the entire project to be finished as soon as possible. Mia, however, finds that some tasks do not have to start as early as possible, and even some slight delay of these tasks will not cause the delay of the entire project.

Now, Mia wants you to show her how long each task can be delayed at most without affecting the entire project. See Explanations for the Sample Input and Output for details.

To simplify the problem, the dependencies have the following constraints:

  1. You start from task 1. Task 1 is always the first task. And, task 1 is the only task that you do not have to finish any other task before you can start.
  2. The last task is always task n. And, task n is the only task that you do not have to finish before you can start any other task, which means once you have finished task n, the entire project is finished.
  3. There will be no conflicts in the given dependencies. The entire project can always be finished. This can be easily seen from the input.

Explanations for the Sample Input and Output

As is shown below, since we want the entire project to be finished as soon as possible, task 1 and task 9 cannot be delayed.

If we ignore some of the dependencies, task 3, task 6 and task 8 take 29 units of time to finish in total. Compared to task 2, task 4 and task 7 which take 26 units of time, and task 3, task 5 and task 7 which take 22 units of time, task 3, task 6 and task 8 take the longest time to finish, which suggests, in this case, none of them can be delayed or the entire project will definitely be affected.

According to the above calculation, task 3, task 5 and task 7 take 7 units of time less than task 3, task 6 and task 8 take, while task 2, task 4 and task 7 take only 3 units of time less than task 3, task 6 and task 8 take. So, task 2, task 4 and task 7 can be delayed 3 units of time at most, and task 5 can be delayed 7 units of time at most.

Notice that the answer is only the maximum units of time that each task can be delayed, i.e. we consider the delay for each task, separately. For example, we now know that task 4 can be delayed 3 units of time at most. If task 4 is really delayed 3 units of time, task 2 and task 7 cannot be delayed, or the entire project will be affected!

Input

There are multiple test cases (no more than 120).

For each test case, the first line contains two positive integers n and mn is the number of tasks, and m is the number of dependencies. (1 ≤ n ≤ 100,000, 1 ≤ m ≤ 1,000,000)

The second line contains n positive integers w1w2, ..., wn, the i-th of which is the required units of time to finish the i-th task. (1 ≤ wi ≤ 1,000, 1 ≤ i ≤ n)

There are m lines following, the i-th of which contains two positive integers ui and vi, which means you need to finish task ui before you can start task vi (1 ≤ ui < vi ≤ n, 1 ≤ i ≤ m).

It is guaranteed that the sum of n over all test cases is less than 1,000,000, and the sum of m over all test cases is less than 10,000,000.

Output

For each test case, output a single line containing n integers (separated by a space), the i-th of which is how long the i-th task can be delayed at most. Notice that the end of line does not have any extra spaces.

Sample Input

9 10
4 9 4 6 7 12 11 13 3
1 2
1 3
2 4
3 5
3 6
4 7
5 7
6 8
7 9
8 9

Sample Output

0 3 0 3 7 0 3 0 0

代码如下:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn=1e6+5;
 5 int n,m,tot;
 6
 7 int lists[maxn][3];
 8 int connect[maxn][2];
 9
10 struct line{int x,y;};
11 bool cmp(const line a,const line b)
12 {
13     return a.y<b.y;
14 }
15 line s[maxn];
16
17 int main()
18 {
19     while(cin>>n>>m)
20     {
21         memset(lists,0,sizeof(lists));
22         for(int i=1; i<=n; ++i)
23         {
24             cin>>lists[i][0];
25
26         }
27         lists[1][1]=lists[1][2]=0;
28         int a,b;
29         for(int i=0; i<m; ++i)
30         {
31             cin>>a>>b;
32             s[i].x=a;
33             s[i].y=b;
34             lists[b][1]=max(lists[b][1],lists[a][0]+lists[a][1]);
35         }
36         for(int i=2; i<=n; ++i)
37         {
38             lists[i][2]=0x3f3f3f;
39         }
40         lists[n][2]=lists[n][1];
41         lists[1][2]=0;
42         sort(s,s+m,cmp);
43         for(int i=m-1; i>=0; --i)
44         {
45             lists[s[i].x][2]=min(lists[s[i].x][2],lists[s[i].y][2]-lists[s[i].x][0]);
46         }
47         for(int i=1; i<=n; ++i)
48         {
49             if(i==1)
50                 cout<<lists[i][2]-lists[i][1];
51             else
52                 cout<<" "<<lists[i][2]-lists[i][1];
53         }
54         cout<<endl;
55     }
56
57     return 0;
58 }

Problem G : Gaming with Mia

Time Limit: 1 s


Description

Mia is learning data structures, this semester. After her boyfriend, John, has given so many problems to her, this time, Mia gives John an interesting problem, or rather, an intellectual game.

In this problem, you are given an integer sequence a1a2, ..., an (-1 ≤ ai ≤ 1, 1 ≤ i ≤ n). You are going to determine n - 1 operators op1op2, ..., opn - 1 (opi is either ‘+‘ or ‘*‘, 1 ≤ i ≤ n - 1) so that the result of the expression S = a1 op1 a2 op2 a3 op3 ... opn - 2 an - 1 opn - 1 an is maximized.

Mia is only interested in the maximum result S, but John is unable to tell. Now, John has turned to you for help!

Explanations for the Sample Input and Output

For the first test case, there is only one integer, so we just need to output it: S = 1.

For the second test case, S = 1 + 1 + 1 + 0 = 3.

For the third test case, S = (-1) * (-1) + 0 = 1.

Input

There are multiple test cases (no more than 120).

For each test case, the first line contains one integer n (1 ≤ n ≤ 100,000).

The second line contains n integers a1a2, ..., an (-1 ≤ ai ≤ 1, 1 ≤ i ≤ n).

There are no more than 10 test cases with n > 1,000.

Output

For each test case, output one integer which is the maximum result S.

Sample Input

1
1
4
1 1 1 0
3
-1 -1 0

Sample Output

1
3
1

代码如下:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn=1e6+5;
 5 int n,m,tot;
 6
 7 int num[maxn];
 8 int dp[maxn];
 9
10 int multiply(int x,int y)
11 {
12     int ans=1;
13     for(int i=x;i<=y;++i)
14     {
15         ans*=num[i];
16     }
17     return ans;
18 }
19
20 int main()
21 {
22     while(cin>>n)
23     {
24         memset(dp,0,maxn);
25         memset(num,0,maxn);
26         for(int i=1;i<=n;++i)
27         {
28             cin>>num[i];
29         }
30         dp[1]=num[1];
31         dp[2]=max(num[1]+num[2],num[1]*num[2]);
32         for(int i=3;i<=n;++i)
33         {
34             int k=min(10,i);
35             for(int j=1;j<=k;++j)
36             {
37                 dp[i]=max(dp[i],dp[i-j]+multiply(i-j+1,i));
38             }
39         }
40         cout<<dp[n]<<endl;
41     }
42     return 0;
43 }

还有一题过不了,过了再补上来ba~

原文地址:https://www.cnblogs.com/bethebestone/p/12120482.html

时间: 2024-11-08 01:18:18

校内友谊赛中等难度题目小记的相关文章

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

用OC实现实现中等难度通讯录详解

用OC实现中等难度通讯录.需求:      1.定义联系人类Contact.实例变量:姓名(拼音,?字?大写).性别.电话号码. 住址.分组名称.年龄.?法:?定义初始化方法或便利构造器方法.显?联系人信息      2.在main.m中定义字典,分组管理所有联系人.分组名为26个大写的英文字母.      3.可以添加联系?对象,如果姓名或电话号码为空,添加失败.添加联系?到匹配的分组.      4.获取某个分组名称下所有联系人,并且按照姓名升序排列.      5.从通讯录中根据电话号码搜

【bfs】【中等难度】wikioi3055 青铜莲花池

3055 青铜莲花池 题目描述 Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M 行N 列个方格(1 ≤ M, N ≤ 30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是美丽.纯净.湛蓝的水.贝西正在练习芭蕾舞,她站在一朵莲花上,想跳到另一朵莲 花上去,她只能从一朵莲花跳到另一朵莲花上,既不能跳到水里,也不能跳到岩石上.      贝西的舞步很像象棋中的马步:每次总是先横向移动M1 (1 ≤ M1 ≤ 30)格,再纵向移

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

OC 通讯录 中等难度

Contact.h #import <Foundation/Foundation.h> @interface Contact : NSObject { NSString *_name; NSString *_gender; NSInteger _age; NSString *_address; NSString *_phoneNumber; NSString *_group; } - (void)setName:(NSString *)name; - (NSString *)name; - (

【快速幂+中等难度】Calculation 哈工大HITOJ2901

这些天好颓啊..都没有A题,只是各种等着填的坑..简直.. 这道题....其实是快速幂模板题..为了学习矩阵快速幂,顺手复习下快速幂... 哈工大的OJ其实还挺友好的.速度也快..赞一个.. 翻译 给你两个数A,B,要你求(1b + 2b + ... + ab) 这个式子mod a的结果.(b是奇数) 每行一组数据 (我以下的代码和解释都是用大写的A,B来代替原题小写a,b,代码中的小写a只是一个类似tmp的东西) 原题 http://acm.hit.edu.cn/hoj/problem/vie

poj 1062 昂贵的聘礼 Dijkstra算法,中等难度,,,一道让我累觉不爱的题目

昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38474   Accepted: 11132 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000

【二分】【中等难度】noip模拟赛 聪哥的工资

聪哥的工资 (money/money.in/money.out) 时限1000ms 内存256MB 题目描述 lwher: 了体验劳苦大众的生活,聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可以自由安排这些时间,也就是说什么时候拿钱,老板说的不算,聪哥才有发言权! (因为聪哥是土豪,他是老板的老板,你觉得老板敢给聪哥安排任务吗?所以聪哥的工作就是看心情去拿钱拿完就走人啦...) 聪哥不喜欢身上一次性有太多的钱,于是他想安排一下拿钱的时间

Leetcode题解 - 部分中等难度算法题解(56、957、825、781、1324、816)

957. N 天后的牢房 思路: 模拟变换,当N天结合后返回 => 当N非常大的时候,超时 => 一般N很大的时候,这种题目必然存在循环,所以记录找过的状态,一旦出现已经访问过的状态可立即跳出循环. class Solution: def prisonAfterNDays(self, cells, N: int): vis = [] while 1: if not N: return cells if cells in vis: ind = vis.index(cells) break tce