码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #244 (Div. 2)

时间:2014-05-05 21:50:56      阅读:795      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

今天是水题集啊。。。。

A. Police Recruits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.

Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.

If there is no police officer free (isn‘t busy with crime) during the occurrence of a crime, it will go untreated.

Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.

Input

The first line of input will contain an integer n (1?≤?n?≤?105), the number of events. The next line will contain n space-separated integers.

If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.

Output

Print a single integer, the number of crimes which will go untreated.

Sample test(s)
input
3
-1 -1 1
output
2
input
8
1 -1 1 -1 -1 1 1 1
output
1
input
11
-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1
output
8

 题意:求最后都几个人没有被抓。

 sl: 简单模拟水题。敲完直接交的那一型。

  1 /* *************

bubuko.com,布布扣
 2 *    by zhuyuqi *
 3 *    2014/5/2   *
 4 *    codeforces *
 5 * **************
 6 */
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 #include<cmath>
11 using namespace std;
12 typedef long long LL;
13 const int inf = 0x3f3f3f3f;
14 const int MAX = 1e5+10;
15 const int MOD = 100000007;
16 int a[MAX];
17 int main()
18 {
19     int n; int tot,sum;
20     while(scanf("%d",&n)==1)
21     {
22         tot=sum=0int ans=0;
23         for(int i=0;i<n;i++) 
24         {
25             scanf("%d",&a[i]);
26             if(a[i]==-1)
27             {
28                 if(sum!=0) sum--;
29                 else ans++;
30             }
31             else 
32             {
33                 sum+=a[i];
34             }
35         }
36         printf("%d\n",ans);
37     }
38     return 0;
39 }
bubuko.com,布布扣

 

 

B. Prison Transfer
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

 

The prison of your city has n prisoners. As the prison can‘t accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city.

For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was.

Then, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are,

  • The chosen c prisoners has to form a contiguous segment of prisoners.
  • Any of the chosen prisoner‘s crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn‘t want to take the risk of his running away during the transfer.

Find the number of ways you can choose the c prisoners.

Input

The first line of input will contain three space separated integers n (1?≤?n?≤?2·105)t (0?≤?t?≤?109) and c (1?≤?c?≤?n). The next line will contain nspace separated integers, the ith integer is the severity ith prisoner‘s crime. The value of crime severities will be non-negative and will not exceed109.

Output

Print a single integer — the number of ways you can choose the c prisoners.

Sample test(s)
input
4 3 3
2 3 1 1
output
2
input
1 1 1
2
output
0
input
11 4 2
2 2 0 7 3 2 2 4 9 1 4
output
6

 题意:选连续的m个数,要求每个所选的数大小不能超过t,求一共有多少种选法。

sl: 可以模拟做,我是用的RMQ sqarse-table裸过去的。

bubuko.com,布布扣
 1 /* *************
 2 *   by zhuyuqi *
 3 *   2014/5/2   *
 4 *   codeforces *
 5 * **************
 6 */
 7 #include <iostream>
 8 #include <math.h>
 9 #include<cstdio>
10 #include<vector>
11 #define max(a,b) ((a>b)?a:b)
12 #define min(a,b) (a<b?a:b)
13 
14 using namespace std;
15 const int maxn = 200000 + 5;
16 const int maxlog = 30;
17 struct RMQ {
18   int d[maxn][maxlog];
19   void init(const vector<int>& A) {
20     int n = A.size();
21     for(int i = 0; i < n; i++) d[i][0] = A[i];
22     for(int j = 1; (1<<j) <= n; j++)
23       for(int i = 0; i + (1<<j) - 1 < n; i++)
24         d[i][j] = max(d[i][j-1], d[i + (1<<(j-1))][j-1]);
25   }
26 
27   int query(int L, int R) {
28     int k = 0;
29     while((1<<(k+1)) <= R-L+1) k++; 
30     return max(d[L][k], d[R-(1<<k)+1][k]);
31   }
32 };
33 int a[maxn];
34 RMQ rmq;
35 int main()
36 {
37     int n,t,c; vector<int> count; int ans=0;
38     scanf("%d%d%d",&n,&t,&c);
39     for(int i=1;i<=n;i++) scanf("%d",&a[i]),count.push_back(a[i]);
40     rmq.init(count);
41     for(int i=0;i<=n-c;i++)
42     {
43        // printf("%d\n",rmq.query(i,i+c-1));
44         if(rmq.query(i,i+c-1)<=t) ans++;
45     }
46     printf("%d\n",ans);
47     return 0;
bubuko.com,布布扣

48 }

 

 

C. Checkposts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

 

Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.

To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either i?=?j or the police patrol car can go to j from i and then come back to i.

Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.

You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.

Input

In the first line, you will be given an integer n, number of junctions (1?≤?n?≤?105). In the next line, n space-separated integers will be given. The ithinteger is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109).

The next line will contain an integer m (0?≤?m?≤?3·105). And each of the next m lines contains two integers ui and vi (1?≤?ui,?vi?≤?nu?≠?v). A pair ui,?vi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.

Output

Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109?+?7).

Sample test(s)
input
3
1 2 3
3
1 2
2 3
3 2
output
3 1
input
5
2 8 0 6 0
6
1 4
1 3
2 4
3 4
4 5
5 1
output
8 2
input
10
1 3 2 2 1 3 1 4 10 10
12
1 2
2 3
3 1
3 4
4 5
5 6
5 7
6 4
7 3
8 9
9 10
10 9
output
15 6
input
2
7 91
2
1 2
2 1
output
7 1

 

 题意:要求修建一些警察局,来保护一些节点 在i处的警察局能保护j 必须是这两个点相互可达。每个节点修建警察局都有相应的花费。求出

修建最少的警察局花费的最少金额,以及修建方案的个数。

sl 很裸的双联通缩点,缩完点之后求出每个连通分量中的最少金额加起来就是总的最少金额。 统计一下每个连通分量中最少金额为x的有几个点

然后乘法原理计数就行。

bubuko.com,布布扣
 1 /* *************
 2 *    by zhuyuqi *
 3 *    2014/5/2   *
 4 *    codeforces *
 5 * **************
 6 */
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 #include<cmath>
11 using namespace std;
12 typedef long long LL;
13 const int inf = 0x3f3f3f3f;
14 const int MAX = 1e5+10;
15 const int MOD = 1e9+7;
16 vector<int> G[MAX],G2[MAX];
17 vector<int> S;
18 int vis[MAX],sccno[MAX],scc_cnt;
19 int cost[MAX];
20 int Min[MAX],num[MAX];
21 void add_edge(int from,int to)
22 {
23     G[from].push_back(to);
24     G2[to].push_back(from);
25 }
26 void dfs(int u)
27 {
28     if(vis[u]) return ;
29     vis[u]=1;
30     for(int i=0;i<G[u].size();i++) dfs(G[u][i]);
31     S.push_back(u);
32 }
33 void rdfs(itn u)
34 {
35     if(sccno[u]) return;
36     sccno[u]=scc_cnt;
37     for(int i=0;i<G2[u].size();i++) dfs2(G2[u][i]);
38 }
39 int main()
40 {
41     int n,m; int a,b;
42     scanf("%d",&n);
43     for(int i=0;i<n;i++) scanf("%d",&cost[i]);
44     scanf("%d",&m);
45     for(int i=0;i<m;i++) 
46     {
47         scanf("%d %d",&a,&b); a--; b--;
48         add_edge(a,b);
49     }
50     S.clear(); scc_cnt=0;
51     memset(sccno,0,sizeof(sccno));
52     memset(vis,0,sizeof(vis));
53     for(int i=0;i<n;i++) dfs(i);
54     for(int i=n-1;i>=0;i--)
55     {
56         if(!sccno[S[i]]) {scc_cnt++; rdfs(S[i]);}
57     }
58     for(int i=0;i<n;i++) Min[i]=inf;
59     memset(num,0,sizeof(num));
60     for(int i=0;i<n;i++)
61     {
62         Min[sccno[i]]=min(cost[i],Min[sccno[i]]);
63     }
64     for(int i=0;i<n;i++)
65     {
66         if(Min[sccno[i]]==cost[i]) num[sccno[i]]++;
67     }
68     int ans=0,ret=1;
69 
70     for(int i=0;i<scc_cnt;i++)
71     {
72         ans+=Min[i];
73         ret=(ret*num[i])%MOD;
74     }
75     printf("%d %d\n",ans,ret);
76 
77     return 0;
bubuko.com,布布扣

78 } 

 

 

D. Match & Catch
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

 

Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings s1 and s2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.

Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.

Given two strings s1 and s2 consist of lowercase Latin letters, find the smallest (by length) common substring p of both s1 and s2, where p is a unique substring in s1 and also in s2. See notes for formal definition of substring and uniqueness.

Input

The first line of input contains s1 and the second line contains s2 (1?≤?|s1|,?|s2|?≤?5000). Both strings consist of lowercase Latin letters.

Output

Print the length of the smallest common unique substring of s1 and s2. If there are no common unique substrings of s1 and s2 print -1.

Sample test(s)
input
apple
pepperoni
output
2
input
lover
driver
output
1
input
bidhan
roy
output
-1
input
testsetses
teeptes
output
3

 

题意:给出两个串求出这两个串的最短公共序列,且这个公共序列在两个串中只出现一次。

sl:我是用字符串hash做的,但是这道题他卡hash常数,一直wa到142,无奈的看了下别人的代码为毛别人能过。

 

 

 

E. Police Patrol
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

 

Imagine that your city is an infinite 2D plane with Cartesian coordinate system. The only crime-affected road of your city is the x-axis. Currently, there are n criminals along the road. No police station has been built on this road yet, so the mayor wants to build one.

As you are going to be in charge of this new police station, the mayor has asked you to choose a suitable position (some integer point) for building it. You should choose the best position for the police station, so that you could minimize the total time of your criminal catching mission. Your mission of catching the criminals will operate only from this station.

The new station will have only one patrol car. You will go to the criminals by this car, carry them on the car, bring them back to the police station and put them in prison. The patrol car can carry at most m criminals at a time. Note that, the criminals don‘t know about your mission. So, they will stay where they are instead of running away.

Your task is to find the position for the police station, so that total distance you need to cover to catch all the criminals will be minimum possible. Note that, you also can built the police station on the positions where one or more criminals already exist. In such a case all these criminals are arrested instantly.

Input

The first line of the input will have two integers n (1?≤?n?≤?106) and m (1?≤?m?≤?106) separated by spaces. The next line will contain n integers separated by spaces. The ith integer is the position of the ith criminal on the x-axis. Absolute value of positions will not exceed 109. If a criminal has position x, he/she is located in the point (x,?0) of the plane.

The positions of the criminals will be given in non-decreasing order. Note, that there can be more than one criminal standing at some point of the plane.

Note: since the size of the input/output could be very large, don‘t use slow input/output techniques in your language. For example, do not use input/output streams (cin, cout) in C++.

Output

Print a single integer, that means the minimum possible distance you need to cover to catch all the criminals.

Sample test(s)
input
3 6
1 2 3
output
4
input
5 5
-7 -6 -3 -1 1
output
16
input
1 369
0
output
0
input
11 2
-375 -108 1336 1453 1598 1892 2804 3732 4291 4588 4822
output
18716

 

 题意:求出一个警察局的修建位置,求出这个点到其他的点来回做多少路。

 sl:直接模拟就行,把警察局建在中间就行,求前缀和模拟也行。

 

  1 /* *************

bubuko.com,布布扣
 2 *    by zhuyuqi *
 3 *    2014/5/3   *
 4 *    codeforces *
 5 * **************
 6 */
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 #include<cmath>
11 using namespace std;
12 typedef long long LL;
13 const int inf = 0x3f3f3f3f;
14 const int MAX = 1e6+10;
15 const int MOD = 100000007;
16 int n,m;
17 int x[MAX];
18 int main()
19 {
20     int n,m; int pos,L,R,md; int begin;
21     LL ans;
22     while(scanf("%d %d",&n,&m)==2)
23     {
24         for(int i=1;i<=n;i++) scanf("%d",&x[i]);
25         sort(x+1,x+n+1);
26         if(n&1) pos=n/2+1,md=x[n/2+1];
27         else pos=n/2,md=x[n/2];
28         L=1; begin=L; ans=0; R=n;
29         while(begin<pos)
30         {
31             while(begin-L<m&&begin<pos) begin++;
32             ans+=(md-x[L])*2;
33               L=begin;
34         }
35         begin=R;
36         while(begin>pos)
37         {
38             while(R-begin<m&&begin>pos) begin--;
39             ans+=(x[R]-md)*2;
40             R=begin;
41         }
42         printf("%I64d\n",ans);
43     }
44     return 0;
45 }
bubuko.com,布布扣

 

 

Codeforces Round #244 (Div. 2),布布扣,bubuko.com

Codeforces Round #244 (Div. 2)

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/acvc/p/3705009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!