QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#93830#5517. Adjacent Product SumAsad_BinAC ✓35ms6576kbC++174.4kb2023-04-03 03:59:042023-04-03 03:59:08

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-03 03:59:08]
  • 评测
  • 测评结果:AC
  • 用时:35ms
  • 内存:6576kb
  • [2023-04-03 03:59:04]
  • 提交

answer

// . . . Bismillahir Rahmanir Rahim . . .
 
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#ifndef ONLINE_JUDGE
#define dbg_out cout
#define debug(...) dbg_out << "DBG )> "; __f(#__VA_ARGS__, __VA_ARGS__);
template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> pr) { out << "{ " << pr.first << ", " << pr.second << " }"; return out; }
template<typename T1> ostream& operator<<(ostream& out, vector<T1> vec) { out << "{ "; for (auto &x: vec) out << x << ", "; out << "}"; return out; }
template<typename T1, size_t size> ostream& operator<<(ostream& out, array<T1, size> arr) { out << "{ "; for (auto &x: arr) out << x << ", "; out << "}"; return out; }
template<typename T1, typename T2> ostream& operator<<(ostream& out, map<T1, T2> mp) { out << "{ ";for (auto &x: mp) out << x.first << ": " << x.second <<  ", "; out << "}"; return out; }
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { while (isspace(name[0])) name++; (isalpha(name[0]) || name[0] == '_') ? dbg_out << name << ": " << arg1 << "\n" : dbg_out << arg1 << "\n"; dbg_out.flush();}
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) { const char *comma = strchr(names + 1, ','); while (isspace(names[0])) names++; (isalpha(names[0]) || names[0] == '_') ? dbg_out.write(names, comma - names) << ": " << arg1 << " | " : dbg_out << arg1 << " | "; __f(comma + 1, args...);}
#else
#define debug(...)
#endif
 
ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;}
ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);}
ll ncr(ll a, ll b){ ll x = max(a-b, b), ans=1; for(ll K=a, L=1; K>=x+1; K--, L++){ ans = ans * K; ans /= L;} return ans;}
ll bigmod(ll a,ll b,ll mod){ if(b==0){ return 1;} ll tm=bigmod(a,b/2,mod); tm=(tm*tm)%mod; if(b%2==1) tm=(tm*a)%mod; return tm;}
ll egcd(ll a,ll b,ll &x,ll &y){ if(a==0){ x=0; y=1; return b;} ll x1,y1; ll d=egcd(b%a,a,x1,y1); x=y1-(b/a)*x1; y=x1; return d;}
ll modpow(ll a,ll p,ll mod) {ll ans=1;while(p){if(p%2)ans=(ans*a)%mod;a=(a*a)%mod;p/=2;} return ans;}
ll inverse_mod(ll n,ll mod) {return modpow(n,mod-2,mod);}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int t; cin >> t;
	while(t--){
		int n; cin >> n;
		
		ll ara[n];
		for(int K = 0; K < n; K++) cin >> ara[K];
		sort(ara, ara+n);
		
		int i = 0, j = -1;
		for(int K = 0; K < n; K++){
			if(ara[K] >= 0) break;
			j = K;
		}
		
		if(j == n-1 || j == -1){
			if(j == n-1) reverse(ara, ara+n);
			
			ll cop[n];
			i = 0; j = n-1;
			for(int K = 0; K < n; K+=2){
				cop[i++] = ara[K];
				if(K+1 < n) cop[j--] = ara[K+1];
			}
			
			ll ans = 0;
			for(int K = 1; K < n; K++) ans += cop[K]*cop[K-1];
			ans += cop[0]*cop[n-1];
			
			cout << ans << "\n";
			
			continue;
		}
		
		
		reverse(ara, ara+j+1);
		
		//for(int K = 0; K < n; K++) cout << ara[K] << ' ';
		//cout << "\n";
			
		ll cop[n];
		//i = 0; j = n-1;
		int x = j;
		//cout << i << ' '<< j << ' ' << x << "\n";
		for(int K = 0; K <= x; K+=2){
			cop[i++] = ara[K];
			if(K+1 <= x) cop[j--] = ara[K+1];
		}
		ll ans = 0;
		for(int K = 1; K <= x; K++) ans += cop[K]*cop[K-1];
		//ans += cop[0]*cop[n-1];
		//for(int K = 0; K <= x; K++) cout <<  cop[K] << ' ';
		//cout<< ans << "\n";
		
		i = x+1; j = n-1; int y = n-1;
		//cout << i << ' ' << j << ' ' << x << ' ' << y << "\n"; 
		for(int K = i; K <= y; K+=2){
			cop[i++] = ara[K];
			if(K+1 <= y) cop[j--] = ara[K+1];
		}
		
		for(int K = x+2; K <= y; K++) ans += cop[K]*cop[K-1];
		
		
		//for(int K = x+1; K <= y; K++) cout <<  cop[K] << ' ';
		//cout<< ans << "\n";
		
		ans += max(cop[0]*cop[x+1] + cop[x]*cop[y], cop[0]*cop[y] + cop[x]*cop[x+1]);
		
		cout << ans << "\n";
		
		
		
		//int ans = 0;
		//if(i <= j){
			//ans += min(ara[i]*ara[min(n-1, j+1)]+ara[min(j, i+1)]*ara[min(n-1, j+2)], ara[i]*ara[min(n-1, j+2)]+ara[min(j, i+1)]*ara[min(n-1, j+1)]);
		//}
		
		//while(i <= j){
			//if(i+2 <= j){
				//ans += ara[i]*ara[i+2];
			//}
			//if(i+3 <= j){
				//ans += ara[i+1] * ara[i+3];
			//}
			
			//i += 2;
		//}
		
		//i = j+1; j = n-1;
		//while(i <= j){
			//if(i+2 <= j){
				//ans += ara[i]*ara[i+2];
			//}
			//if(i+3 <= j){
				//ans += ara[i+1] * ara[i+3];
			//}
			
			//i += 2;
		//}
		
		//cout << ans << "\n";
	}
	
	return 0;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3356kb

input:

4
3
1 2 3
6
1 1 1 1 0 0
5
100000 100000 100000 100000 -100000
5
1 2 3 4 5

output:

11
3
10000000000
48

result:

ok 4 number(s): "11 3 10000000000 48"

Test #2:

score: 0
Accepted
time: 29ms
memory: 6576kb

input:

1
200000
11009 633591 -419208 -664908 731171 -774644 -878270 656078 -38057 -220602 -897906 670165 -765931 -612936 -583782 -549624 -644245 137209 -983054 -110583 349193 699723 -412876 -417691 810865 -474314 -200632 570810 -283481 39600 20940 218215 -408751 -507326 -961614 600863 499517 -538207 767155...

output:

66608463123493911

result:

ok 1 number(s): "66608463123493911"

Test #3:

score: 0
Accepted
time: 25ms
memory: 6464kb

input:

1
200000
7902 376928 977876 -664787 382287 -671247 263725 -875047 554163 59899 -976624 726497 617682 -387432 -960499 -703748 -644991 72374 -564962 -569121 -412123 907945 -379338 915496 665461 389485 -742294 942448 -862668 -677446 -72510 -382856 893536 128827 -596269 -572440 -532339 536145 -521524 -3...

output:

66581505433554478

result:

ok 1 number(s): "66581505433554478"

Test #4:

score: 0
Accepted
time: 32ms
memory: 6500kb

input:

1
200000
969944 -942957 346587 328855 61775 -596222 -622652 -504245 181233 -694452 -90194 -350098 1294 844554 -273993 205351 -674109 35911 887981 -929585 826563 179388 689051 -716467 450354 -746718 -220733 348937 529775 -331268 868892 -885852 -839030 834684 803928 225887 435807 575645 189798 -255705...

output:

66721135858270454

result:

ok 1 number(s): "66721135858270454"

Test #5:

score: 0
Accepted
time: 30ms
memory: 6564kb

input:

1
200000
-68015 -262842 778522 -607801 -287109 542027 -515508 -105072 871526 -483654 865940 -391840 419758 -929943 -650710 -885551 359996 -965702 340923 611878 -899901 387610 -214190 616720 235247 117080 300828 -342648 20292 -83165 747072 -521774 561331 505688 -830728 -877713 438804 -419707 -35659 7...

output:

66646767806203928

result:

ok 1 number(s): "66646767806203928"

Test #6:

score: 0
Accepted
time: 35ms
memory: 6504kb

input:

1
200000
894027 417274 -887618 -579309 -635992 645424 598116 363804 -536255 -203152 787222 531567 866594 -732810 35796 -941601 -703973 -65388 759014 -811810 -724439 595832 -180652 -78465 -945009 113803 -212463 994139 377883 -800211 -311527 -59622 828766 -788457 -493755 885764 372098 -380207 675663 8...

output:

66669581129323609

result:

ok 1 number(s): "66669581129323609"

Test #7:

score: 0
Accepted
time: 25ms
memory: 4904kb

input:

2
100000
169603 145427 -202283 -480350 -856872 -65853 -442884 -773569 -275747 -953075 873381 -155156 -519569 -351127 558958 -345448 461553 927180 -310163 -46521 -857521 -906097 -91734 875600 836439 39554 488295 162237 -570813 -456645 -876308 254421 93745 689934 -712525 31372 536079 487786 191237 747...

output:

33392172147649469
33329865049707147

result:

ok 2 number(s): "33392172147649469 33329865049707147"

Test #8:

score: 0
Accepted
time: 23ms
memory: 5036kb

input:

2
100000
166496 -139607 131578 -451857 794246 -927605 601038 660456 316473 -672574 -170486 -196898 -101105 909229 -754537 535280 -602416 -74433 -857221 -435356 381164 365348 -58196 208786 621332 -998574 -990145 -431275 -213222 -110468 65094 716574 430883 -604211 -375551 829699 -495776 -535937 -13229...

output:

33445326050536015
33363059196148572

result:

ok 2 number(s): "33445326050536015 33363059196148572"

Test #9:

score: 0
Accepted
time: 28ms
memory: 5084kb

input:

2
100000
-871463 568880 -401636 611487 445363 -852579 777884 -870669 -56457 -461776 -249205 824583 -717493 71511 868747 -555622 -603163 825881 595722 -893894 -380151 573570 -24659 548454 377854 -134776 496566 -157711 242444 -827514 -56727 -919349 698318 -933207 954943 -343604 472370 -496437 579028 -...

output:

33375458994693108
33453260311973619

result:

ok 2 number(s): "33375458994693108 33453260311973619"

Test #10:

score: 0
Accepted
time: 26ms
memory: 5004kb

input:

2
100000
90579 -751006 -67776 -423243 124850 -749182 856656 -471496 -429386 783875 637226 782840 666120 -668134 -444748 353477 430943 -175732 -986188 745643 -204689 781792 -927900 -118360 162747 -236127 -981874 213927 669738 -579410 884675 577656 604 737799 355139 -510426 -587857 -456937 325201 -996...

output:

33287772768665659
33313392958107202

result:

ok 2 number(s): "33287772768665659 33313392958107202"

Test #11:

score: 0
Accepted
time: 21ms
memory: 4940kb

input:

2
100000
-947380 -70891 -670693 -394750 -224033 389067 -1349 -2620 -773945 -935625 656582 -293754 -915416 494148 -821465 -737425 -633027 -212195 466755 287104 68848 53235 -894362 249678 982492 -309107 -460313 557194 -972672 -331307 860928 -23415 -668739 408803 692113 316272 380289 -417437 -963479 -9...

output:

33288924160392129
33379620728513712

result:

ok 2 number(s): "33288924160392129 33379620728513712"

Test #12:

score: 0
Accepted
time: 26ms
memory: 3788kb

input:

10
20000
841746 527518 595261 331297 -946901 129987 670374 -140388 -684770 309555 -302589 415564 -387435 613331 -624940 -95922 945847 -199224 24636 -565799 -72069 -395358 -523453 -511446 854898 -846967 -749453 -341866 173256 -508156 574073 878761 984359 798117 -622388 434663 264157 607113 -38776 139...

output:

6622802477773024
6640013265208007
6592100254591181
6640170208895030
6688143425864831
6705222845668074
6676830146528095
6618221005278570
6714940493280121
6724345935461679

result:

ok 10 numbers

Test #13:

score: 0
Accepted
time: 26ms
memory: 3484kb

input:

100
2000
-607224 -718287 -433045 -816611 -935719 -559217 508630 -485197 134273 -520442 886499 318527 104344 -376092 -146638 -921227 98459 -324526 -396653 39142 -536124 114612 -22769 215450 -806035 217692 -758435 -360933 -470886 -271877 -839014 683804 138195 405799 -385833 481109 198084 -145350 -4353...

output:

664320265599433
656060796084111
670847172001984
693285015575982
641806321082038
686775083044219
660836368097072
665848189708176
656774554796514
640515026177509
637922445211883
651793096312130
668305333192730
680836044550146
642790615977139
659533778528338
689986245290621
679990801733695
664398460938...

result:

ok 100 numbers

Test #14:

score: 0
Accepted
time: 28ms
memory: 3392kb

input:

1000
200
324840 875913 -750586 872247 891834 -802538 -394930 -39271 -402853 -935536 -148671 -311516 -470147 168411 428251 -412526 736882 93448 384219 -987587 -924419 974032 864821 448111 409149 -889736 17366 -198877 -314710 -952652 406856 124043 183144 -182906 -79075 308212 785198 -276953 260766 178...

output:

65021379321819
61482831940623
64907628507685
65762816459484
64637099110104
70521910685375
71017401353273
63188959323659
64923749759877
78536500128365
72214363259863
72551077515316
64319147242944
70483971638209
60920280492111
74602439250852
66241024614625
66265066071760
69062565668032
71163034443457
...

result:

ok 1000 numbers

Test #15:

score: 0
Accepted
time: 18ms
memory: 3456kb

input:

10000
20
15812 -152393 101507 981503 -762519 70570 197040 710069 767152 -683226 691070 -756515 -396347 -388139 -327351 -323136 -451642 -327842 989160 548358
20
-344746 439864 -408284 787179 -526193 -898718 -1701 -843020 956421 697486 -822953 243028 -209174 82020 -997778 242139 -932413 -673559 176880...

output:

5836057833484
7900249490263
6741513108893
7695904868186
5905346687260
6210447934702
6528577603481
6366200793553
7285941052913
6695898759021
5336229787408
6603459571509
5516442382707
6068193077387
6836405277420
5809766642183
7914983783571
7722262525971
6863867878728
5452027662683
6336759366520
571716...

result:

ok 10000 numbers

Test #16:

score: 0
Accepted
time: 27ms
memory: 3600kb

input:

42
4761
-884836 423256 -16540 5180 626061 574189 64375 565165 891155 631808 84833 -143882 -909496 758173 204660 -700050 163672 -867390 920820 848717 -347766 -768001 437344 -21942 403333 -220324 388822 84096 -938705 527686 841315 876833 131270 836458 700753 -740379 253394 -432058 494815 -255931 -8361...

output:

1568197940334474
1627465779774764
1613876915865833
1579828038154862
1563836977436851
1608582638636216
1589836095120383
1577031292775532
1608544317984962
1617042083628598
1589929522676958
1588048186120208
1562897607148914
1551101839004540
1582371865754027
1590036248776725
1606863383861959
16117573084...

result:

ok 42 numbers

Test #17:

score: 0
Accepted
time: 23ms
memory: 3428kb

input:

69
2898
-826877 -729045 -951445 -391404 197462 798213 -288928 -157886 631013 -85218 377079 -128051 -661781 -189943 -900471 43621 -847997 400563 -695730 -932224 -245215 -395642 -559167 -118464 920363 252772 -623730 -286403 -674675 -560306 -448483 924941 210935 -834513 608008 -136257 -953207 665428 29...

output:

942714868546250
946859857816400
969037992012804
977484785384248
919489346073972
969868311124320
933818196842132
977503973543166
967593973040804
982214889485424
963286569050653
970847618560724
957508838495683
945357209274080
933598867828567
978233850926901
975379109413588
1000443792815115
98105291396...

result:

ok 69 numbers