UVA 1614 Hell On The Market(貪心&&結論)
題意
給出n個數字a[i], 並且 1 <= a[i] <= i,通過每一項乘以1或-1,使得所有數字的乘積和為0;
分析
題意化簡:從陣列中找出若干個數,使他們的和為所有數和的一半。
想了n種超時的演算法,毫無頭緒。
肯定得用上 1 <= a[i] <= i; 這一個條件。
只是不知從何下手。
原來有個結論: 在前i個數中,能夠組成1~sum[i] 任意一個數。證明過程其實對程式沒有影響,結論只是為了證明倒序構造,結果的正確性。
倒序構造。。。
有一個巨大的坑點。。所有數的和為long long,用int WA了6次。。
程式碼
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+19;
int a[maxn], n;
int r[maxn];
bool cmp(int x, int y)
{
return a[x] < a[y];
}
int main()
{
//freopen("in.txt","r", stdin);
while(cin >> n )
{
long long cnt = 0;
for(int i = 0; i < n;i ++)
{
cin >> a[i];
cnt += a[i];
r[i] = i;
}
if (cnt % 2 == 1)
{
puts("No");
continue;
}
sort(r,r+n,cmp);
cnt >>= 1;
for(int i = n-1; i >= 0; i--)
{
int j = r[i];
if(a[j] <= cnt)
{
cnt -= a[j];
a[j] = 1 ;
}
else
a[j] = -1;
}
puts("Yes");
for(int i = 0; i < n-1; i++)
cout << a[i] << " ";
cout << a[n-1] << endl;
}
return 0;
}
相關推薦
UVA 1614 Hell On The Market(貪心&&結論)
題意 給出n個數字a[i], 並且 1 <= a[i] <= i,通過每一項乘以1或-1,使得所有數字的乘積和為0; 分析 題意化簡:從陣列中找出若干個數,使他們的和為所有數和的一半。 想了n種超時的演算法,毫無頭緒。 肯定得用上 1 &l
1614 Hell on the Markets(貪心 + 結論)
點選開啟題目連結 題目大意 給定n個數,滿足1<=a[i]<=i; 使其中某些數變成倒數使得n個數總和為0; 思路: 結論:n個數可以組成1~sum[n]的任意數 證明: 當n
Uva 1614 - Hell on the Markets
roman family int time pre 個數 所有 sum pos 鏈接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl
Hell on the Markets(貪心)
貪心部分的理論依據:前i個數可以湊出1~sum[i]的所有整數。 證明: 第二類數學歸納,n=1時成立,假設n=k之前所有項都成立,當n=k+1時。sum[k+1]=sum[k]+a[k+1]。只需證明能湊出sum[k]+1~sum[k+1]間的整數即可。設1≤p≤a[k+
UVA 122 -- Trees on the level (二叉樹 BFS)
返回 錯誤 符號 pri false font width else print Trees on the level UVA - 122 解題思路: 首先要解決讀數據問題,根據題意,當輸入為“()”時,結束該組數據讀入,當沒有字符串時,整個輸入結束。因此可以專門編
UVa 122 Trees on the level(鏈式二叉樹的建立和層次遍歷)
構建 void target .net color 鏈接 struct failed div 題目鏈接: https://cn.vjudge.net/problem/UVA-122 1 /* 2 問題 3 給出每個節點的權值和路線,輸出該二叉樹的層次遍歷序列。
uva1614 Hell on the Markets
style esp ons sin ont cst urn div nbsp 貪心部分的理論依據:前i個數可以湊出1~sum[i]的所有整數。 證明:第二類數學歸納,n=1時成立,假設n=k之前所有項都成立,當n=k+1時。sum[k+1]=sum[k]+a[k+1]。只需
How To Find The Best Self Loading Mixer On The Market Available On The Market
The purchase of a self loading mixer is just not easy. You must do a comprehensive market research, in order to find out what are the the best pla
Advice On Purchasing The Best Concrete Block Machine On The Market
Once you buy a concrete block machine, you normally have a construction business of some sort. You are accountable for laying the building blocks
Finding an asphalt mixing plant on the market
If you would like streamline the way you handle your company, its smart to get an asphalt mixing plant on the market. This can help you to produce
The 10 Best Choices On The Market Review 2018
Looking to buy a scan tool or considering one of Autel Scanner impressive product line?. The company makes some of the best OBD scanners available on th
UVa 122 - Trees on the level(BFS層序遍歷 &&strchr&&sscanf)
題目連結: 原型: char *strchr(const char *s,char c); #include<string.h> 查詢字串s中首次出現字元c的位置,返回首次出現c的位置的指標(可以當作陣列
Does there presently exist on the market HUD/AR glasses or contact lens?
I would like to quickly make notes throughout the day and it occurred to me glasses with a HUD (heads up display) or other AR feature would be idea when co
UVa 122 Trees on the level(建樹,層次遍歷)
題意 建樹並層次遍歷輸出 (data,pos) pos表示改節點位置 L代表左兒子 R代表右兒子 建樹很簡單 開始在根節點 遇到L往左走遇到R往右走 節點不存在就新建 走完了就儲存改節點的值 輸出直接bfs就行了了 #include<cs
HDU 4912 Paths on the tree(LCA+貪心)
path 同時 sort 我們 strong ble delta 選擇 所有 題目鏈接 Paths on the tree 來源 2014 多校聯合訓練第5場 Problem B 題意就是給出m條樹上的路徑,讓你求出可以同時選擇的互不相交的路徑最大數目。 我們先求出
AI on the edge will kick IoT market adoption, not 5G
When we talk about the widespread use of the IoT, we must keep in mind three major aspects organisations are considering when they deploy devices: price (i
UVA 10129 Play on Words
bool seq lis rest ble u+ contains con sync Some of the secret doors contain a very interesting word puzzle. The team of archaeologists ha
[轉]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
right clas rup b- row 添加按鈕 n) 1-1 自帶 完整錯誤信息: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"AS IS" AND ANY EXPRESS O
My app status is Ready for Sale but I cannot see my app on the App Store. Why? 為什麽審核通過後 appstore中搜不到我的app
one soci orm event 什麽 live pstore follow following 這是蘋果的官方解答 The following factors could prevent your app from showing up on the App St
Warning: date(): It is not safe to rely on the system's timezone settings.
bsp ron notice zone asi 警告 family one str PHP調試的時候出現了警告: It is not safe to rely on the system解決方法,其實就是時區設置不正確造成的,本文提供了3種方法來解決這個問題。 實際上,