原创 ural 1023 Buttons

看到這個題, 不難讓我們回憶起小學接觸過的拿硬幣問題(是甲和乙拿的, 還是別人拿的, 隨便), 假設一次最多能拿N個, 那麼後者的策略就是拿的硬幣數=N+1-前者的. 這道題就是讓你找到最小的N被給定值整除即可. 至於爲什麼要整除, 顯然

原创 ural 1014 Product of Digits

首先面對此題,最不該做的就是暴搜. U no it. 於是我們想到桶! 首先質因數分解(2,3,5,7), 如果n>1直接output -1. 然後我們就要把桶裏面的數的個數壓縮. 8=2*2*2 當然首當其衝. 然後是 9=3*3 然後

原创 ural 1022 Genealogical Tree

Topological Sort. #include<iostream> using namespace std; int n; int A[101][101]; int f[101]; int seq[101]; int main()

原创 ural 1026 Questions and Answers

這道題的目的很明顯, 就是桶, 當然100,000的數據O(nlogn)的排序也是能做的. #include<iostream> using namespace std; int n,q; int A[5001]; char s[10];

原创 ural 1017 Staircases

http://blog.csdn.net/heartnheart/archive/2010/11/22/6028103.aspx 這裏有O(n^3)和O(n^2)的算法. 下面介紹個O(n^3/2)的. 令T[i][j]=滿足條件的樓房(

原创 ural 1019 Line Painting

一道離散化的題目. 灰常猥瑣的邊界, 導致我WA#3又WA#9又WA#13又WA#14才AC. 所謂離散化, 就是把原先最傻的hash表變小. 因爲我們只用到題目中的某些點, 那麼用這些點爲座標, 記錄顏色情況就好了. 注意邊界: 0 和

原创 ural 1009 1012 1013 K-Based Numbers V1.0/2.0/3.0

看來我就是討厭麻煩的題啊...跳過去這麼多實現題... 不過此題在思路上和實現上均不難. 我的代碼裏: A[i]=滿足條件的i位數中末尾不爲0的, B[i]=滿足條件的i位數中末尾爲0的. 那麼遞推方程就很簡單了: A[i]=(A[i-1

原创 ural 1029 Ministry

DP, 狀態轉移方程爲T[i][j]=max{T[i][j-1],T[i-1][j],T[i][j+1]}+A[i][j]; 由於這個DP方程有後效性, 採用迭代動歸(經典題目參見"滑雪") #include<iostream> usin

原创 ural 1002 phone numbers

這是一道動歸題, 算法複雜度爲 O(n*len). #include<iostream> using namespace std; const int MAX=10000; char str[105]; int n; char s[500

原创 ural 1024 Permutations

這裏要用到點組合的知識了. 引理: 對於P中任意元素i, 存在k, 使得Pk(i)=i. 然後就很好辦了. #include<iostream> const int MAXN=1000; int P[MAXN+1]; int has[MA

原创 ural 1028 Stars

樹狀數組或線段樹. #include<iostream> using namespace std; const int MAXX=32001; const int MAXN=15000; int Sum[MAXN+1],T[MAXX+1]

原创 ural 1005 stone pile

動歸. 注: 我發現ural的時間限制很寬裕但空間限制很BT...所以總是有Crash的現象... #include<iostream> using namespace std; int W[30],n,tot,temp; bool T[

原创 ural 1001 Reverse Root

水題一道. 沉默.    #include<iostream> #include<cmath> __int64 A[700000]; int main() { int i=0; while( scanf("%I64d",&

原创 ural 1025 Democracy in Danger

簡單的貪心. #include<iostream> using namespace std; int A[105]; void swap(int i,int j) { int temp; temp=A[i]; A[i]=A[j];