CodeForces 1417B Two Arrays
題目連線
題意:
給你一個組數和一個不幸數字T,要求把陣列分成兩部分,標記為1or0,同為1或者0的兩個數相加儘可能的不等於T,求任意一種分法。
解題思路:
T/2+T/2=T,既然這樣,把所有小於T/2的數字放在一邊,大於T/2的數放在另一邊,這樣無論怎麼加都不會加到T。剛好等於T/2的資料就一個為0一個為1這樣交替分開放。
#include<bits/stdc++.h>
#define maxn 1000001
using namespace std;
long long int a[maxn];//int會TE
int main()
{
int num;
cin>>num;
while(num--)
{
long long int n,T;
cin>>n>>T;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int x=1;
for(int i=1;i<=n;i++)
{
if(T%2!=0)
{
int t=T/2;
if(a[i]>t)
{
a[i]=1;
}
else
{
a[i]=0;
}
}
else
{
int t=T/2;
if(a[i]>t)
{
a[i]=1;
}
else if(a[i]<t)
{
a[i]=0;
}
else
{
a[i]=x;
x=1-x;
}
}
}
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}
相關文章
- LeetCode | 349 Intersection Of Two ArraysLeetCode
- Leetcode 4 Median of Two Sorted ArraysLeetCode
- LeetCode 4. Median of Two Sorted ArraysLeetCode
- leetcode 350. Intersection of Two Arrays IILeetCode
- LeetCode Median of Two Sorted Arrays(004)解法總結LeetCode
- [LeetCode] 1385. Find the Distance Value Between Two ArraysLeetCode
- CF 773 (Div. 1) D. Two Arrays 雙指標 容斥指標
- Educational Codeforces Round 98 (Rated for Div. 2) E. Two Editorials 細節題
- Arrays
- leetcode 4. Median of Two Sorted Arrays 尋找兩個正序陣列的中位數(困難)LeetCode陣列
- Arrays Basics
- 六,Arrays
- Arrays工具類
- Arrays+Collections
- 【java】Arrays類Java
- coca after two months vs in two months
- Sorting arrays in NumPy by column
- Java中Arrays作用Java
- 18_Arrays類
- Java Arrays.sort()Java
- Java容器工具類ArraysJava
- JAVA基礎--Arrays類Java
- Arrays.copyOf 函式函式
- Two Pirates - 2
- Two Pointer Method
- List.of() VS Arrays.asList()
- Arrays.asList():使用指南
- Arrays.asList存在的坑
- Arrays.asList()是個坑
- 集合框架3-Arrays 類框架
- Leetcode 231 Power of TwoLeetCode
- Leetcode 1 two sumLeetCode
- Merge Two Sorted List
- Tokitsukaze and Two Colorful Tapes
- LeetCode | 1 Two SumLeetCode
- 11.23 Two Different Worlds
- F - Two Sequence Queries
- Codeforces