CF-1154

E. Two Teams

题意

一个 $1\sim n$ 的排列, 从大到小删数, 当删一个数时将其左右的 $k$ 个数也删了, 若某个数是第奇数次删的输出 1, 否则输出 2

题解

维护两个数组 $l[i],r[i] $ 分别表示第 $i$ 个数左边, 右边的数所在的地方

代码

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define fro1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef long double db;
typedef long long ll;
typedef int itn;
int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}
int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}
int in(int &a,int &b){return scanf("%d%d",&a,&b);}
int in(ll &a){return scanf("%lld",&a);}
int in(int &a){return scanf("%d",&a);}
int in(char *s){return scanf("%s",s);}
int in(char &s){return scanf("%c",&s);}
int in(db &a){return scanf("%Lf",&a);}
void out(int a){printf("%d",a);}
void outln(int a){printf("%d\n",a);}
void out(ll a){printf("%lld",a);}
void outln(ll a){printf("%lld\n",a);}
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const db eps = 1e-8;
const int N = 2.1e5;
const ll mod = 1e9+7;
int sign(db a) { return a < -eps ? -1 : a > eps;}
int db_cmp(db a, db b){ return sign(a-b); }

int l[N],r[N],Map[N],ans[N],a[N];

int main() {
#ifdef PerpEternal
// freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin);
// freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);
#endif
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,k;
in(n,k);
for1(i,n){
in(a[i]);
Map[a[i]]=i;
l[i]=i-1;
r[i]=i+1;
}
int flag=-1;
for(int i=n;i>=1;i--){
if(Map[i]){
int poi=Map[i],ml=poi,mr=poi;
for(int i=poi,j=0;i<=n&&j<=k;j++,mr=i=r[i]){
ans[i]=flag;
Map[a[i]]=0;
}
ml=poi=l[poi];
for(int i=poi,j=0;i<=n&&j<k;j++,ml=i=l[i]){
ans[i]=flag;
Map[a[i]]=0;
}
r[ml]=mr;
l[mr]=ml;
if(flag==-1)flag=-2;
else flag=-1;
}
}
for1(i,n)out(-ans[i]);
puts("");
return 0;
}

F. Shovels Shop

题意

$n$ 个物品, 其价格分别为 $a_i$ , 有 $m$ 种折扣, 买 $x_i$ 件物品, 便宜的 $y_i$ 件免费, 买 $k(\le 2000)$ 件的最小花费

题解

$dp[i]$ 表示买 $i$ 件物品的最小花费

$dp[i]=min(dp[i-1]+a[i],dp[i-j]+pre[i]-pre[ i-cost[j] ])$

$cost[i]$ 表示买 $i$ 件物品最小需要支付的物品数

代码

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define fro1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef long double db;
typedef long long ll;
typedef int itn;
int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}
int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}
int in(int &a,int &b){return scanf("%d%d",&a,&b);}
int in(ll &a){return scanf("%lld",&a);}
int in(int &a){return scanf("%d",&a);}
int in(char *s){return scanf("%s",s);}
int in(char &s){return scanf("%c",&s);}
int in(db &a){return scanf("%Lf",&a);}
void out(int a){printf("%d",a);}
void outln(int a){printf("%d\n",a);}
void out(ll a){printf("%lld",a);}
void outln(ll a){printf("%lld\n",a);}
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const db eps = 1e-8;
const int N = 2.1e5;
const ll mod = 1e9+7;
int sign(db a) { return a < -eps ? -1 : a > eps;}
int db_cmp(db a, db b){ return sign(a-b); }

int a[N],dp[2003],cost[2003],pre[2003];
int main() {
#ifdef PerpEternal
// freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin);
// freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);
#endif
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,m,k,x,y;
meminf(cost);
in(n,m,k);
for1(i,n)in(a[i]);
for0(i,m){
in(x,y);
if(x<2001)
cost[x]=min(cost[x],x-y);
}
sort(a+1,a+n+1);
for1(i,k)pre[i]=pre[i-1]+a[i];
for1(i,k){
dp[i]=dp[i-1]+a[i];
for1(j,i)
if(cost[j]<inf)
dp[i]=min(dp[i],dp[i-j]+pre[i]-pre[i-cost[j]]);
}
outln(dp[k]);
return 0;
}

G. Minimum Possible LCM

题意

给 $n(2\le n \le 1e6)$ 个数, 求最大的 $lcm(a_i,a_j) (1\le a_i\le 1e7)$

题解

枚举 $gcd(a_i,a_j)$ , 找到最小的 $a_i, a_j$

时间复杂度 $O(\frac {10^7}1+\frac {10^7}2+\frac {10^7}3+\cdots+\frac {10^7}{10^7})\approx O(10^7log(10^7))$

代码

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define fro1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef long double db;
typedef long long ll;
typedef int itn;
int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}
int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}
int in(int &a,int &b){return scanf("%d%d",&a,&b);}
int in(ll &a){return scanf("%lld",&a);}
int in(int &a){return scanf("%d",&a);}
int in(char *s){return scanf("%s",s);}
int in(char &s){return scanf("%c",&s);}
int in(db &a){return scanf("%Lf",&a);}
void out(int a){printf("%d",a);}
void outln(int a){printf("%d\n",a);}
void out(ll a){printf("%lld",a);}
void outln(ll a){printf("%lld\n",a);}
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const db eps = 1e-8;
const int N = 1.1e7;
const ll mod = 1e9+7;
int sign(db a) { return a < -eps ? -1 : a > eps;}
int db_cmp(db a, db b){ return sign(a-b); }

int a[N][3];

int main() {
#ifdef PerpEternal
// freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin);
// freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);
#endif
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,x,maxx=0;
in(n);
for1(i,n){
in(x);
maxx=max(maxx,x);
if(a[x][0]==0){
a[x][1]=i;
a[x][0]=1;
}else if(a[x][0]==1){
a[x][2]=i;
a[x][0]=2;
}
}
ll minn=1e18;
pii ans;
for1(g,maxx){
ll x1=0;
for(int j=g;j<=maxx;j+=g){
if(x1){
if(a[j][0]){
if(x1*j/g<minn){
ans=pii(a[x1][1],a[j][1]);
minn=x1*j/g;
}
break;
}
}else{
if(a[j][0]==1){
x1=j;
}else if(a[j][0]==2){
x1=j;
if(x1*x1/g<minn){
ans=pii(a[x1][1],a[j][2]);
minn=x1*j/g;
}
break;
}
}
}
}
if(ans.fi>ans.se)swap(ans.fi,ans.se);
printf("%d %d\n",ans.fi,ans.se);
return 0;
}