A. Toy Cars
Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.
There are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n × n matrix А: there is a number on the intersection of the і-th row and j-th column that describes the result of the collision of the і-th and the j-th car:
- - 1: if this pair of cars never collided. - 1 occurs only on the main diagonal of the matrix.
- 0: if no car turned over during the collision.
- 1: if only the i-th car turned over during the collision.
- 2: if only the j-th car turned over during the collision.
- 3: if both cars turned over during the collision.
Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of cars.
Each of the next n lines contains n space-separated integers that determine matrix A.
It is guaranteed that on the main diagonal there are - 1, and - 1 doesn't appear anywhere else in the matrix.
It is guaranteed that the input is correct, that is, if Aij = 1, then Aji = 2, if Aij = 3, then Aji = 3, and if Aij = 0, then Aji = 0.
Output
Print the number of good cars and in the next line print their space-separated indices in the increasing order.
Examples
input
Copy
3 -1 0 0 0 -1 1 0 2 -1
output
Copy
2 1 3
input
Copy
4 -1 3 3 3 3 -1 3 3 3 3 -1 3 3 3 3 -1
output
Copy
0
【方法】
两辆车相撞,等于1的话本辆车被撞翻,2的话相撞的那辆车撞翻,3的话都被撞翻,模拟就行,从1到n都存入set当中,按照规则操作,输出。
#include <bits/stdc++.h>
using namespace std;
const int N=111;
int a[N][N];
int main()
{
int n;
cin>>n;
set<int>s;
for(int i=1;i<=n;i++)
{
s.insert(i);
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(a[i][j]==1)
s.erase(i);
if(a[i][j]==2)
s.erase(j);
if(a[i][j]==3)
s.erase(i),s.erase(j);
}
if(s.size()==0)
puts("0");
else
{
cout<<s.size()<<endl;
for(set<int>::iterator it=s.begin();it!=s.end();it++)
cout<<*it<<" ";
cout<<endl;
}
return 0;
}
C. Woodcutters
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.
There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of trees.
Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree.
The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.
Output
Print a single number — the maximum number of trees that you can cut down by the given rules.
Examples
input
Copy
5 1 2 2 1 5 10 10 9 19 1
output
Copy
3
input
Copy
5 1 2 2 1 5 10 10 9 20 1
output
Copy
4
Note
In the first sample you can fell the trees like that:
- fell the 1-st tree to the left — now it occupies segment [ - 1;1]
- fell the 2-nd tree to the right — now it occupies segment [2;3]
- leave the 3-rd tree — it occupies point 5
- leave the 4-th tree — it occupies point 10
- fell the 5-th tree to the right — now it occupies segment [19;20]
In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].
【方法】
类似于DP思想,这棵树砍不砍取决于它倒下能不能碰到两边的树。
#include <bits/stdc++.h>
using namespace std;
int x[100005],h[100005];
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
if(n==1)
{
puts("1");
return 0;
}
for(int i=1;i<=n;i++)
cin>>x[i]>>h[i];
int sum=0;
for(int i=2;i<n;i++)
{
if(x[i]-x[i-1]>h[i])
sum++;
else if(x[i+1]-x[i]>h[i])
{
sum++;
x[i]+=h[i];
}
}
cout<<sum+2<<endl;
return 0;
}
D. Queue
Little girl Susie went shopping with her mom and she wondered how to improve service quality.
There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.
Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
Input
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.
Output
Print a single number — the maximum number of not disappointed people in the queue.
Examples
input
Copy
5 15 2 1 5 3
output
Copy
4
Note
Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.
【题意】
贪心,要求最大的不失望的人数,从小到大排序,第一个人肯定不失望,后面与他前面所有人的时间比较。
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
int sum=a[1],cnt=1;
for(int i=2;i<=n;i++)
{
if(a[i]>=sum)
{
sum+=a[i];
cnt++;
}
}
cout<<cnt<<endl;
return 0;
}