0
点赞
收藏
分享

微信扫一扫

B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)

B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_程序设计

input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1

题意: 给你一个数组,其中数组元素有的锁定了有的未锁定,你可以任意交换未锁定的元素。现在你需要使得前缀和B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_前缀和_02B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_前缀和_03值最大,B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_ios_04即为最大的B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_前缀和_03值,你需要遵守规则更换数组元素使得B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_ios_04值最小。

解题思路: 贪心就完事了, 我们要使得B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_程序设计_07值越小,就要使得前缀和的下标提前,也就是说我们尽量让大的数往前放,那么这样在这个数之后的前缀和都增加了,也就可以使得前缀和小于0的下标值减小,就可以让B. Negative Prefixes(贪心+思维)Educational Codeforces Round 95 (Rated for Div. 2)_程序设计_07值变小。 我们按照这样去构造就行,记得更爱位置,按原位输出即可。

AC代码

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int a[maxn];
int vis[maxn];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
//IOS;
while(cin>>t){
while(t--){
cin>>n;
rep(i,1,n)
scanf("%d",&a[i]);
rep(i,1,n)
scanf("%d",&vis[i]);
int maxx,pos;
rep(i,1,n){
maxx=inf*(-1);
if(vis[i]==0){
per(j,n,i){
if(a[j]>maxx&&vis[j]==0){
pos=j;
maxx=a[j];
}
}
if(maxx!=(-1)*inf&&a[i]<a[pos]){
int temp=a[i];
a[i]=maxx;
a[pos]=temp;
}
}
}
rep(i,1,n){
cout<<a[i]<<" ";
}
cout<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论