0%

[題解]背包置物問題

a139. 背包置物問題

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define double long double
#define N 14
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define INF 2e18
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define rrep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
using namespace std;
int n,k;
vector<int> vec,p;

signed main(){
cin>>n;
vec.assign(1,-1);
cin.ignore();
string s;getline(cin,s);
stringstream ss(s);
int temp;
while(ss>>temp)vec.push_back(temp);
cin>>k;

int ans = 0,len = vec.size();
for(int i=1;i<=len-1;i++){
if(find(p.begin(),p.end(),vec[i])!=p.end())continue;
else if(p.size()<k){
p.push_back(vec[i]);
continue;
}
int index = 0, dis = 0, max_dis=0;

for(int j=0;j<p.size();j++){
dis = 0;
for(int q=i+1;q<=len;q++){
if(vec[q]==p[j])break;
dis += 1;
}
if(max_dis < dis){
max_dis = dis;
index = j;
}
}
// cout<<i<<" "<<index<<endl;
p[index] = vec[i];
ans += 1;
}
cout<<ans<<endl;
}