HDU 5000 Clone,hdu5000clone

來源:互聯網
上載者:User

HDU 5000 Clone,hdu5000clone

題意:

每個複製個體有n個屬性  如果對於A、B兩個個體  A的n個屬性均不低於B的n個屬性  那麼B會被淘汰  問最多能有多少個體同時存活

思路:

根據資料大小猜一下是n^2的複雜度  往DP方面考慮一下

DP需要一個想法的支援  就是最優的那個集合中的元素的n個屬性的和一定是一樣的  為什麼呢?  想象一下  假設最優的集合的屬性和是5  這時你選過來一個和為6或者4的元素  那麼至少會造成2個和為5的解從集合中被扔掉  選進1個扔掉2個明顯是不合適的  而且這個最優的屬性和必然為n個屬性最大值的和的一半

假設每個屬性的最大值為ti  那麼sumt/2就是最優的屬性和  那麼問題就變成了  取n個數字  第i個數字在0到ti之間  問和為sumt/2的方案有幾種  這就可以DP了

dp[i][j]表示第i個數字和為j的方案數  那麼轉移就是dp[i][j]=dp[i-1][j-ti]+dp[i-1][j-ti+1]+...+dp[i-1][j]  我們發現如果只用for需要3個迴圈  但是這是一個連續求和式子  用樹狀數組最佳化  就可以O(n^2logn)解決了

PS:這題杭電卡的時間比較死  不要每次迴圈都做到2000  因為sumt/2以後的數字全沒用  所以別算

代碼:

#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<set>#include<vector>#include<cassert>using namespace std;typedef long long LL;#define Q 401#define N 2002#define M 200001#define inf 2147483647#define mod 1000000007#define lowbit(x) (x&(-x))int t, n, p;int d[N];LL f[N][N];void add(int r, int x, int v) {for (; x <= p; x += lowbit(x)) {f[r][x] += v;if (f[r][x] > mod)f[r][x] -= mod;}}LL sum(int r, int x) {LL res = 0;for (; x > 0; x -= lowbit(x))res += f[r][x];return res % mod;}int main() {int i, j, k, tmp;scanf("%d", &t);while (t--) {memset(f, 0, sizeof(f));p = 0;scanf("%d", &n);for (i = 1; i <= n; i++) {scanf("%d", &d[i]);p += d[i];}p = p / 2 + 1;for (i = 1; i <= d[1] + 1; i++)add(1, i, 1);for (i = 2; i <= n; i++) {for (j = 1; j <= p; j++) {k = j - d[i] - 1;tmp = (sum(i - 1, j) - sum(i - 1, k) + mod) % mod;add(i, j, tmp);}}printf("%lld\n", (sum(n, p) - sum(n, p - 1) + mod) % mod);}return 0;}



HDU1102代碼錯誤之處

program hdu1102;type r=record l,r:Longint; length:longint; end;var road:array[1..5000] of r; n,q,i,t,effective:longint; h,u:array[1..5000] of longint; ans:longint; temp:r;function father(n:longint):longint;var f:longint; i,temp:longint;begin i:=n; while u[i]<>i do begin i:=u[i]; end; f:=i; i:=n; while u[i]<>f do begin temp:=u[i]; u[i]:=f; i:=temp; end; father:=f;end;procedure merge(fa,fb:longint);var i,j:longint;begin if h[fa]>=h[fb] then begin u[fb]:=fa; if h[fa]=h[fb] then inc(h[fa]); end else (* 此處應為u[fa]:=fb 而不是 h[fa]:=fb *) u[fa]:=fb;end;procedure qsort(l,r:longint);var i,j,mid:longint;begin i:=l; j:=r; mid:=road[(i+j) div 2].length; while i<=j do begin while road[i].length<mid do inc(i); while road[j].length>mid do dec(j); if i<=j then begin temp:=road[i]; road[i]:=road[j]; road[j]:=temp; inc(i); dec(j); end; end; if i<r then qsort(i,r); if l<j then qsort(l,j);end;procedure init;var i,j,k,a,b:longint; fa,fb:longint;begin ans:=0; effective:=0; readln(n); for i:=1 to n do begin h[i]:=1; u[i]:=i; end; t:=0; for i:=1 to n do begin for j:=1 to n do begin read(k); if (i<j) then begin inc(t); road[t].l:=i; road[t].r:=j; road[t].length:=k; end; end; readln; end; readln(q); for i:=1 to q do begin readln(a,b); fa:=father(a); fb:=father(......餘下全文>>
 
HDU1408看看什地方有錯誤

// 我的解法

#include <stdio.h>

int main()
{
float VUL, D;
int i = 1, t = 0, s = 0, time = 1;
scanf ("%f %f", &VUL, &D);
s = t = D;
while ( (VUL > 0 && VUL < 5000) && (D > 0 && D < VUL) )
{
while ( s < VUL )
{
D += t;
s += D;
time++;
}
break;
}
printf ("%d\n", (int)VUL + time - 1);

return 0;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.