第一题
A - Sum of Even Numbers
Time limit : 2sec / Memory limit : 1024MB
Problem Statement
There are N integers: A1,A2,…,AN. We will make Q changes to these integers. The q-th change is as follows:
- Add Xq to the integer AIq.
For each change, find the sum of the even numbers among the N integers A1,A2,…,AN after that change.
Constraints
- 1≤N≤200 000
- 1≤Q≤150 000
- 1≤Ai≤1 000 (1≤i≤N)
- 1≤Iq≤N (1≤q≤Q)
- 1≤Xq≤1 000 (1≤q≤Q)
Input
Input is given from Standard Input in the following format:
N Q
A1 A2 … AN
I1 X1
I2 X2
:
IQ XQ
Output
Print Q lines. The q-th line (1≤q≤Q) should contain the sum after the q-th change.
Sample Input 1
3 4
5 6 7
1 1
2 3
1 5
3 3
Sample Output 1
12
6
0
10
Initially, (A1,A2,A3)=(5,6,7).
- After the first change, (A1,A2,A3)=(6,6,7). The sum of even numbers among these integers is 6+6=12.
- After the second change, (A1,A2,A3)=(6,9,7). The only even number among these integers is 6 and the sum is 6.
- After the third change, (A1,A2,A3)=(11,9,7). There are no even numbers among these integers and the sum is 0.
- After the fourth change, (A1,A2,A3)=(11,9,10). The only even number among these integers is 10 and the sum is 10.
Sample Input 2
4 6
1000 1000 1000 1000
1 1
2 1
3 1
1 999
1 999
1 999
Sample Output 2
3000
2000
1000
3000
1000
4998
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n,q,m,x,fv,fx;
while(cin>>n>>q){
vector<int> v(n);
int sum=0;
for(auto i=0;i<n;++i) {
cin >> v[i];
if(v[i]%2==0)
sum+=v[i];
}
for(auto i=0;i<q;++i){
cin>>m>>x;
m-=1;
fv=v[m]%2;
fx=x%2;
if(fv!=0&&fx!=0){
sum+=(v[m]+x);
}else if(fv==0&&fx!=0){
sum-=v[m];
}else if(fv==0&&fx==0){
sum+=x;
}
v[m]+=x;
cout<<sum<<endl;
}
}
return 0;
}
第二题
B - Making Nondecreasing
Time limit : 2sec / Memory limit : 1024MB
Problem Statement
You are given a sequence of length N: X1,X2,…,XN. Consider performing the following operation some number of times (possibly zero) on this sequence:
- Choose an integer i (1≤i≤N), and multiply the i-th element of the sequence by (−1).
After these operations, let the new sequence be y1,y2,…,yN. Your objective is to satisfy the following condition:
- For any i (1≤i≤N−1), yi≤yi+1 holds.
Find the minimum number of operations needed, or print -1
if no sequence of operations can satisfy the condition.
Constraints
- 2≤N≤100
- −1 000≤Xi≤1 000 (1≤i≤N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
X1 X2 … XN
Output
Print the answer.
Sample Input 1
5
2 -1 1 1 -2
Sample Output 1
2
The given sequence is [2,−1,1,1,−2]. Two operations can satisfy the condition, for example, as follows:
- Multiply the first element of the sequence by (−1). The sequence becomes [−2,−1,1,1,−2].
- Multiply the fifth element of the sequence by (−1). The sequence becomes [−2,−1,1,1,2].
It is impossible to satisfy the condition by not more than one operation, so the answer is 2.
Sample Input 2
3
-1 0 1
Sample Output 2
0
As the given sequence already satisfies the condition, no operation is needed.
Sample Input 3
5
1 0 1 0 1
Sample Output 3
-1
No sequence of operations can satisfy the condition.
Sample Input 4
5
1 2 3 4 -5
Sample Output 4
1
第三题
C - Number of Paths
Time limit : 2sec / Memory limit : 1024MB
Problem Statement
There is a grid of square cells with H rows and W columns. The cell at the i-th row and the j-th column (1≤i≤H,1≤j≤W) will be denoted as (i,j).
Each cell in this grid is painted white or black. The colors of the cells are represented by H strings S1,S2,…,SH, each of length W. The cell (i,j) (1≤i≤H, 1≤j≤W) is painted white if the j-th character of the string Si is .
, and painted black if that character is #
.
Answer Q questions. The q-th question (1≤q≤Q) is as follows:
- A cell (Iq,Jq) is specified. Find the number of ways, modulo 109+7, to travel from the top-left cell (1,1) to the bottom-right cell (H,W) via the cell (Iq,Jq), by repeatedly moving right or down to the adjacent cell, visiting only white cells.
Constraints
- 1≤H≤500
- 1≤W≤500
- For each i (1≤i≤H), the length of Si is W.
- For each i (1≤i≤H), each character of Si is
.
or#
. - 1≤Q≤100 000
- 1≤Iq≤H (1≤q≤Q)
- 1≤Jq≤W (1≤q≤Q)
- For each q (1≤q≤Q), the cell (Iq,Jq) is white.
- The cells (1,1) and (H,W) are white.
Input
Input is given from Standard Input in the following format:
H W
S1
S2
:
SH
Q
I1 J1
I2 J2
:
IQ JQ
Output
Print Q lines. The q-th line (1≤q≤Q) should contain the answer to the q-th question.
Sample Input 1
3 4
..#.
....
#...
3
3 2
2 2
1 4
Sample Output 1
2
6
0
For example, in the first question, there are two ways to travel, as follows:
- (1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4)
- (1,1)→(1,2)→(2,2)→(3,2)→(3,3)→(3,4)
Thus, the first line should contain 2 modulo 109+7, that is, 2.
Sample Input 2
15 25
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
5
1 1
10 1
10 2
10 2
15 2
Sample Output 2
669554037
118755
982800
982800
15
第四题
D - Keys and Rooms
Time limit : 3sec / Memory limit : 1024MB
Problem Statement
In some building, there are N rooms, numbered 1,2,…,N. For each i (1≤i≤N), you can find Key i in Room i. These rooms are connected by M corridors. The i-th corridor (1≤i≤M) connects Room Ui and Room Vi, and requires Key Ki to pass. Keys can be used repeatedly once you find them.
You entered Room 1 without any keys in your hand. Find the number of the rooms that can be reached through corridors using the keys that can be found on the way. Here, Room 1 is counted as a room that can be reached.
Constraints
- 1≤N≤200 000
- 1≤M≤200 000
- 1≤Ui,Vi≤N (1≤i≤M)
- 1≤Ki≤N (1≤i≤M)
- Ui≠Vi (1≤i≤M)
- If i≠j, then (Ui,Vi)≠(Uj,Vj) and (Ui,Vi)≠(Vj,Uj).
- Any room can be reached from any other rooms, if one has all the keys.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
U1 V1 K1
U2 V2 K2
:
UM VM KM
Output
Print the answer.
Sample Input 1
4 4
1 2 1
1 3 2
2 4 4
3 4 4
Sample Output 1
3
Rooms 1,2 and 3 can be reached as follows:
- Initially, you are in Room 1. You can find Key 1 here.
- You can move from Room 1 to Room 2 using Key 1. You can find Key 2 here.
- You can move from Room 2 to Room 1 using Key 1, then from Room 1 to Room 3 using Key 2. You can find Key 3 here.
Room 4 cannot be reached using these keys, so the answer is 3.
Sample Input 2
3 3
1 2 3
2 3 1
3 1 2
Sample Output 2
1
Sample Input 3
4 5
1 2 1
1 3 1
2 4 4
4 3 2
1 4 3
Sample Output 3
4
/**
* OJ显示Runtime Error
*/
#include <iostream>
#include <vector>
#include <stack>
#include <unordered_set>
using namespace std;
int main()
{
int n,m,u,v,k;
while(cin>>n>>m){
vector<unordered_set<int>> p(n);
vector<vector<int>> uvk(n,vector<int>(n));
for(auto i=0;i<m;++i){
cin>>u>>v>>k;
u-=1;
v-=1;
k-=1;
p[u].insert(v);
p[v].insert(u);
uvk[u][v]=k;
uvk[v][u]=k;
}
unordered_set<int> myKey;
myKey.insert(0);
stack<int> DFS;
DFS.push(0);
unordered_set<int> visited = {0};
while(!DFS.empty())
{
int i = DFS.top();
DFS.pop();
for(auto j=p[i].begin();j!=p[i].end();++j)
{
if(myKey.count(uvk[i][*j])!=0) {
if (visited.count(*j) == 0) { //没有到过这个房间中
DFS.push(*j);
for(auto t:visited)
if(t!=i) {
p[t].insert(*j);
p[*j].insert(t);
}
}
visited.insert(*j);
myKey.insert(*j);
}
}
}
cout<<visited.size()<<endl;
}
return 0;
}
嗯,打扰了。