A. Police Recruits
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.
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.
Print a single integer, the number of crimes which will go untreated.
3 -1 -1 1
2
8 1 -1 1 -1 -1 1 1 1
1
11 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1
8
Lets consider the second example:
- Firstly one person is hired.
- Then crime appears, the last hired person will investigate this crime.
- One more person is hired.
- One more crime appears, the last hired person will investigate this crime.
- Crime appears. There is no free policeman at the time, so this crime will go untreated.
- One more person is hired.
- One more person is hired.
- One more person is hired.
The answer is one, as one crime (on step 5) will go untreated.
简单的模拟题,-1代表犯人,没一个正整数可以收服该数值个犯人,问最后有多少个犯人没有被收服,, 收服(想起了神奇宝贝)、、、、、
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int n, tp;
while(~scanf("%d",&n))
{
int sum = 0;
int tot = 0;
for(int i = 0; i < n; i ++)
{
scanf("%d",&tp);
if(tp >0)
tot += tp;
else
{
if(tot > 0)
tot --;
else
sum ++;
}
}
printf("%d\n",sum);
}
}
B. Prison Transfer
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.
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 n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109.
Print a single integer — the number of ways you can choose the c prisoners.
4 3 3 2 3 1 1
2
1 1 1 2
0
11 4 2 2 2 0 7 3 2 2 4 9 1 4
6
求有多少个连续c个的不大于t 的数字。去不大于t 的数字,记录区间即可,注意首尾的标记。
(错了八发,一开始以为只是算区间的时候加一减一的问题,改完发现WA,然后又发现并没有判断区间和c的大小,可能会出现负数,又WA,然后以为自己的思路是错的,最后发现是第一个标记的时候,应该为0,详见代码)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int tt[200100];
int vis[200100]; // 代表大于t 的数的位置
int main()
{
int n, t, c;
while(~scanf("%d%d%d",&n,&t,&c))
{
memset(tt, 0, sizeof(tt));
memset(vis, 0, sizeof(vis));
int cnt = 1;
int sum = 0;
vis[0] = 0; // 首标记
for(int i = 1; i <= n; i ++)
{
scanf("%d",&tt[i]);
if(tt[i] > t)
vis[cnt ++] = i;
}
if(cnt == n + 1)
{
printf("0\n");
continue;
}
if(cnt == 1)
{
if(n >= c) //可能会出现符号,虚判断
printf("%d\n",n - c + 1);
else
printf("0\n");
continue;
}
vis[cnt] = n + 1; // 尾标记
/*for(int i = 0; i <= cnt; i ++)
{
printf("--%d\n", vis[i]);
}*/
for(int i = 1; i <= cnt + 1; i ++)
{
int tm = vis[i] - vis[i - 1] - 1; //区间,加一还是减一还是不操作,需要注意一下
if(tm >= c)
sum += (tm - c + 1);
}
printf("%d\n",sum);
}
}
C. Checkposts
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 ican 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.
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 ith integer 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 ≤ n; u ≠ 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.
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).
3 1 2 3 3 1 2 2 3 3 2
3 1
5 2 8 0 6 0 6 1 4 1 3 2 4 3 4 4 5 5 1
8 2
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
15 6
2 7 91 2 1 2 2 1
7 1
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef __int64 LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 100; //点数
const int MAXM = 3*(1e5)+100; //边数
const LL MOD = 1000000000 + 7;
struct Edge
{
int to, next;
}edge[MAXM];
int in_edge[MAXM];
int out_edge[MAXM];
int head[MAXN],tot;
int val[MAXN];
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
int Index,top;
int scc;
LL cost, pp;
int n, m;
bool Instack[MAXN];
int num[MAXN];
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void Tarjan(int u)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if( !DFN[v] )
{
Tarjan(v);
if( Low[u] > Low[v] )
Low[u] = Low[v];
}
else if(Instack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if(Low[u] == DFN[u])
{
int sum = 0;
int minn = INF;
scc++;
do
{
v = Stack[ --top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
if(minn == val[v])
sum ++;
else if(minn > val[v])
sum = 1, minn = val[v];
}
while( v != u);
cost += minn;
pp = (pp*sum)% MOD;
}
}
void solve(int N)
{
memset(DFN, 0, sizeof(DFN));
memset(Instack, false, sizeof(Instack));
memset(num, 0, sizeof(num));
Index = scc = top = 0;
for(int i = 1;i <= N; i++)
if(!DFN[i])
Tarjan(i);
}
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
void ans()
{
cost = 0;
pp = 1;
for(int i = 1; i <= n; i ++)
{
if(!DFN[i])
Tarjan(i);
}
printf("%I64d %I64d\n",cost, pp);
}
int main()
{
int a, b;
while(~scanf("%d",&n))
{
init();
for(int i = 1; i <= n; i ++)
{
scanf("%d",&val[i]);
}
scanf("%d",&m);
for(int i = 0; i < m; i ++)
{
scanf("%d%d",&a,&b);
addedge(a,b);
}
ans();
}
}