2.3 補題記錄
技術標籤:題目筆記
A. Buying A House
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us.
The girl lives in house m of a village. There are n
houses in that village, lining in a straight line from left to right: house 1, house 2, …, house n. The village is also well-structured: house i and house i + 1 (1 ≤ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased.You will be given n integers a1, a2, …, a**n that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then a**i equals 0. Otherwise, house i can be bought, and a**i represents the money required to buy it, in dollars.
As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush’s house to some house he can afford, to help him succeed in his love.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 100, 1 ≤ m ≤ n, 1 ≤ k ≤ 100) — the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively.
The second line contains n integers a1, a2, …, a**n (0 ≤ a**i ≤ 100) — denoting the availability and the prices of the houses.
It is guaranteed that a**m = 0 and that it is possible to purchase some house with no more than k dollars.
Output
Print one integer — the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy.
Examples
input
Copy
5 1 20 0 27 32 21 19
output
Copy
40
input
Copy
7 3 50 62 0 0 0 99 33 22
output
Copy
30
input
Copy
10 5 100 1 0 1 0 0 0 0 0 1 1
output
Copy
20
Note
In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters.
In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away.
題目大意:
找到一個離公主最近的房子,在能買的範圍內(錢足夠,無人居住)
思路
先按距離從小到大排序,然後看看哪個能買得起
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#define ll long long
#define re return
using namespace std;
int n, m, k; //村子裡的房屋數量,女孩住的房子,和Zane擁有的錢的數量(美元)。
// int a[105];
struct nope{
int len;
int w;
int id;
} a[105];
//每個房子間隔十米
bool cmp(nope a,nope b){
return a.len < b.len;
return a.w < b.w;
}
int main(){
cin >> n >> m >> k;
for (int i = 1; i <= n; i ++){
cin >> a[i].w;
a[i].id = i;
}
for (int i = 1; i <= n; i++){ //算距離
a[i].len = abs(a[i].id - m);
}
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++){
if(a[i].w != 0 && a[i].id != m){
if(a[i].w <= k){
cout << a[i].len * 10;
return 0;
}
}
}
}
B. Find The Bone
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Zane the wizard is going to perform a magic show shuffling the cups.
There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x = i.
The problematic bone is initially at the position x = 1. Zane will confuse the audience by swapping the cups k times, the i-th time of which involves the cups at the positions x = u**i and x = v**i. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations.
Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at x = 4 and the one at x = 6, they will not be at the position x = 5 at any moment during the operation.
Zane’s puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 106, 1 ≤ m ≤ n, 1 ≤ k ≤ 3·105) — the number of cups, the number of holes on the table, and the number of swapping operations, respectively.
The second line contains m distinct integers h1, h2, …, h**m (1 ≤ h**i ≤ n) — the positions along the x-axis where there is a hole on the table.
Each of the next k lines contains two integers u**i and v**i (1 ≤ u**i, v**i ≤ n, u**i ≠ v**i) — the positions of the cups to be swapped.
Output
Print one integer — the final position along the x-axis of the bone.
Examples
input
Copy
7 3 4 3 4 6 1 2 2 5 5 7 7 1
output
Copy
1
input
Copy
5 1 2 2 1 2 2 4
output
Copy
2
Note
In the first sample, after the operations, the bone becomes at x = 2, x = 5, x = 7, and x = 1, respectively.
In the second sample, after the first operation, the bone becomes at x = 2, and falls into the hole onto the ground.
題目大意:
找到骨頭最終掉在哪裡,中途有可能掉進洞裡就不會隨著杯子動了
我寫的程式碼比較複雜,先說一下我的思路,後面放一個剛學的思路
首先不是每一次的操作都會操作帶骨頭的杯子,如果裡面沒有骨頭那麼也沒有什麼繼續進行的價值。如果換之前和換之後都存在在洞裡,那麼我們只要記好答案就可以了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#define ll long long
#define re return
using namespace std;
int n, m, k;
int a[1000000 + 10];
bool hole[1000000 + 10];
int main(){
scanf("%d%d%d", &n, &m, &k);
// cin >> n >> m >> k; 這題卡cin
a[1] = 1;
int keng;
for (int i = 1; i <= m; i++){
scanf("%d", &keng);
// cin >> keng;
hole[keng] = 1;
}
int x, y;
int t = 0; //已經掉洞,已經在洞裡了的話後面的操作也沒有什麼價值
int ans = 1;
for (int i = 1; i <= k; i++){
// cin >> x >> y;
scanf("%d%d", &x, &y);
// swap(x, y);
if(!a[x] && !a[y])
continue;
if(!t){
if(hole[x] && a[x]){ //如果一開始就在洞裡
t = 1;
ans = x;
continue;
}
if(hole[y] && a[y]){
t = 1;
ans = t;
continue;
}
swap(a[x], a[y]);
if(hole[y] && a[y]){
t = 1;
ans = y;
continue;
}
if(hole[x] && a[x]){
t = 1;
ans = x;
continue;
}
if(a[x]){
ans = x;
continue;
}
if(a[y]){
ans = y;
continue;
}
}
}
// if(t){
// cout << ans << endl;
// for (int i = 1; i <= n; i++){
// if(a[i]){
printf("%d\n", ans);
// break;
// }
// }
// }
re 0;
}
// 3 1 2
// 1
// 1 2
// 2 3
這個方法就很簡單了,只需要判斷現在這個位置是不是洞並且現在這個位置裡有骨頭就行
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int h;
bool st[1000020];
int x1,x2;
int main()
{
int n ,m ,k;
int res = 1; //標記骨頭的位置
cin >> n >> m >> k;
for(int i = 1 ;i <= m ; i ++ ) cin >> h , st[h] = true;
for(int i = 1 ; i <= k ; i ++ )
{
cin >> x1 >> x2;
if(x1 == res && st[x1] == false)
res = x2;
else if(x2 == res && st[x2] == false)
res = x1;
}
cout << res << endl;
return 0;
}