测试2.12

E - 简单的dp

 CodeForces - 1791E 

思路:因为要构造成俩数相加的形式,并且数位和差距要小于1,于是可以使每位互相差一,

比如个位为9,则俩数一个个位为4,一个为5,下次当十位为9时,一个十位为5,一个为4,存入一个数组中,由于数组首位可能为0,故先确定一个首位数字将答案接在数字后,通过取余得出答案(应该要开long long 前面没开好像卡我了)

The sum of digits of a non-negative integer aa is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of 123123 is 66 and the sum of digits of 1010 is 11. In a formal way, the sum of digits of \displaystyle a=\sum_{i=0}^{\infty} a_i \cdot 10^ia=i=0∑∞​ai​⋅10i, where 0 \leq a_i \leq 90≤ai​≤9, is defined as \displaystyle\sum_{i=0}^{\infty}{a_i}i=0∑∞​ai​.

Given an integer nn, find two non-negative integers xx and yy which satisfy the following conditions.

  • x+y=nx+y=n, and
  • the sum of digits of xx and the sum of digits of yy differ by at most 11.

It can be shown that such xx and yy always exist.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1 \le t \le 10\,0001≤t≤10000).

Each test case consists of a single integer nn (1 \leq n \leq 10^91≤n≤109)

Output

For each test case, print two integers xx and yy.

If there are multiple answers, print any.

Sample 1

InputcopyOutputcopy
5
1
161
67
1206
19
1 0
67 94
60 7
1138 68
14 5

Note

In the second test case, the sum of digits of 6767 and the sum of digits of 9494 are both 1313.

In the third test case, the sum of digits of 6060 is 66, and the sum of digits of 77 is 77.

#include<bits/stdc++.h>
using namespace std;
long long int a[15],b[15],c[15]={0,10};
void add(long long int m)
{
   long long int t1=10,t2=10,t;
   long long int ans=0,flag=0;
    while(m){
        t=m%10;
        m/=10;
        ans++;
        if(t%2==1){
        if(flag==0){
            a[ans]=t/2;
            b[ans]=t/2+1;
        flag++;
        }
        else{
            a[ans]=t/2+1;
            b[ans]=t/2;
        flag--;
        }
    }
    else{
         a[ans]=t/2;
            b[ans]=t/2;
    }
}
for(int i=ans;i>=1;i--){
    t1+=a[i];
    t2+=b[i];
    t1*=10;
    t2*=10;
}
printf("%lld %lld\n",t1/10%c[ans],t2/10%c[ans]);
}



int main()
{
    int T;
   long long int n,m;

   for(int i=2;i<=11;i++){
    c[i]=c[i-1]*10;
   }
    scanf("%d",&T);
    while(T--){
            scanf("%d",&m);
            add(m);
        }
    }

E - 简单的dp

 CodeForces - 1791E 

思路:根据题设,可判断出当出现负数次数为单数,且无0出现时会有一个数小于0无法变为正数,故可以将绝对值排序后其他数的和单独减去最小值的绝对值。其他所有情况都是所有数绝对值之和。(貌似也要开long long)

Given an array aa consisting of nn elements, find the maximum possible sum the array can have after performing the following operation any number of times:

  • Choose 22 adjacent elements and flip both of their signs. In other words choose an index ii such that 1 \leq i \leq n - 11≤i≤n−1 and assign a_i = -a_iai​=−ai​ and a_{i+1} = -a_{i+1}ai+1​=−ai+1​.

Input

The input consists of multiple test cases. The first line contains an integer tt (1 \leq t \leq 10001≤t≤1000) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains an integer nn (2 \leq n \leq 2\cdot10^52≤n≤2⋅105) — the length of the array.

The following line contains nn space-separated integers a_1,a_2,\dots,a_na1​,a2​,…,an​ (-10^9 \leq a_i \leq 10^9−109≤ai​≤109).

It is guaranteed that the sum of nn over all test cases does not exceed 2\cdot10^52⋅105.

Output

For each test case, output the maximum possible sum the array can have after performing the described operation any number of times.

Sample 1

InputcopyOutputcopy
5
3
-1 -1 -1
5
1 5 -5 0 2
3
1 2 3
6
-1 10 9 8 7 6
2
-1 -1
1
13
6
39
2

Note

For the first test case, by performing the operation on the first two elements, we can change the array from [-1, -1, -1][−1,−1,−1] to [1, 1, -1][1,1,−1], and it can be proven this array obtains the maximum possible sum which is 1 + 1 + (-1) = 11+1+(−1)=1.

For the second test case, by performing the operation on -5−5 and 00, we change the array from [1, 5, -5, 0, 2][1,5,−5,0,2] to [1, 5, -(-5), -0, 2] = [1, 5, 5, 0, 2][1,5,−(−5),−0,2]=[1,5,5,0,2], which has the maximum sum since all elements are non-negative. So, the answer is 1 + 5 + 5 + 0 + 2 = 131+5+5+0+2=13.

For the third test case, the array already contains only positive numbers, so performing operations is unnecessary. The answer is just the sum of the whole array, which is 1 + 2 + 3 = 61+2+3=6.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    int n,m;
    int a[200005];
    scanf("%d",&T);
    while(T--){
    long long int sum=0,ans=0,flag=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
       scanf("%d",&m);
       if(m<0)ans++;
       if(m==0)flag=1;
       a[i]=abs(m);
        }
    if(ans%2==1&&flag==0){
        sort(a+1,a+1+n);
        for(int i=2;i<=n;i++){
            sum+=a[i];
        }
        sum-=a[1];
    }
    else
    {
        for(int i=1;i<=n;i++){
            sum+=a[i];
        }
    }


     printf("%lld\n",sum);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值