https://codeforces.com/contest/1447
A题
除了选中的数其他的都加上j等价于当前数减去j,因此只需从前往后依次输出即可。
1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5
6 using namespace std;
7
8 const int N=1010;
9 int n,t;
10
11 int main()
12 {
13 cin>>t;
14
15 while(t--)
16 {
17 cin>>n;
18 printf("%d\n",n);
19 for(int i=1;i<=n;i++) printf("%d ",i);
20 printf("\n");
21 }
22 return 0;
23 }
B题
由于操作次数无限,因此你可以将每偶数个负数都消掉,奇数个负数剩下最后一个可以使整个数组中绝对值最小的数。
1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5
6 using namespace std;
7
8 const int N=15;
9 int map[N][N];
10 int n,t,m;
11
12 int main()
13 {
14 cin>>t;
15
16 while(t--)
17 {
18 cin>>n>>m;
19
20 int cnt=0;
21 int mint=110;
22 for(int i=1;i<=n;i++)
23 for(int j=1;j<=m;j++)
24 {
25 scanf("%d",&map[i][j]);
26 if(map[i][j]<0) cnt++,map[i][j]*=-1;
27 mint=min(mint,map[i][j]);
28 }
29
30 int ans=0;
31 for(int i=1;i<=n;i++)
32 for(int j=1;j<=m;j++) ans+=map[i][j];
33
34 if(cnt%2==0) printf("%d\n",ans);
35 else printf("%d\n",ans-2*mint);
36 }
37 }
C题
贪心,对所有的数排个序然后从大到小贪即可。
1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5
6 using namespace std;
7
8 typedef long long ll;
9 const int N=200020;
10 struct it
11 {
12 ll w;
13 int cnt;
14 }thing[N];
15 int t,n;
16 ll W;
17 int ne[N];
18
19 bool cmp(struct it a,struct it b)
20 {
21 if(a.w!=b.w) return a.w>b.w;
22 }
23
24 int main()
25 {
26 cin>>t;
27
28 while(t--)
29 {
30 cin>>n>>W;
31
32 for(int i=1;i<=n;i++)
33 {
34 ll x;
35 cin>>x;
36 thing[i].cnt=i;thing[i].w=x;
37 }
38
39 sort(thing+1,thing+1+n,cmp);
40
41 ll twoW;
42 if(W%2==1) twoW=W/2+1;
43 else twoW=W/2;
44
45 ll res=0;
46 int out=0;
47 for(int i=1;i<=n;i++)
48 {
49 if(thing[i].w>W) continue;
50 if(thing[i].w<=W&&thing[i].w>=twoW)
51 {
52 res+=thing[i].w;
53 ne[++out]=thing[i].cnt;
54 break;
55 }
56 if(thing[i].w<twoW)
57 {
58 res+=thing[i].w;
59 ne[++out]=thing[i].cnt;
60 if(res>=twoW) break;
61 }
62 }
63
64 if(res<twoW) printf("-1");
65 else
66 {
67 printf("%d\n",out);
68 for(int i=1;i<=out;i++) printf("%d ",ne[i]);
69 }
70 printf("\n");
71 }
72 }