A. Arithmetic Array
分析:
当sum大于n时,那么添加(sum-n)个0,当sum小于n时,只需要添加一个数即可。
#include <iostream>
using namespace std;
int main()
{
int t; cin >> t;
while(t--)
{
int n; cin >> n;
int sum = 0;
for(int i = 0; i < n; i++)
{
int tmp; cin >> tmp;
sum += tmp;
}
cout << ((sum>=n)?sum-n:1) << endl;
}
// system("pause");
}
B. Bad Boy
分析:
走离起始点较远的两角即可。
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int t; cin >> t;
while(t--)
{
int n, m, x, y; cin >> n >> m >> x >> y;
if(x > n/2) x = 1; else x = n;
if(y > m/2) y = 1; else y = m;
int i, j;
i = x==1?n:1;
j = y==1?m:1;
printf("%d %d %d %d\n",x,y,i,j);
}
// system("pause");
}
C. Challenging Cliffs
分析:
排序后,不难发现此时的排列是权重最大的。
但是因为还需要满足约束,开头和末尾的差的绝对值应该最小。
那么我们找到那对数,从中间断开,即可。
#include <algorithm>
#include <iostream>
using namespace std;
int a[300100];
int b[300100];
bool vis[300100];
int abs(int x) {
return x<0?-x:x; }
int