題意:html
n條惡龍,m個勇士,用勇士來殺惡龍。一個勇士只能殺一個惡龍。並且勇士只能殺直徑不超過本身能力值的惡龍。每一個勇士須要支付能力值同樣的金幣。問殺掉全部惡龍鬚要的最少金幣。node
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 20000 + 5; 4 int n, m; 5 int night[maxn], dragon[maxn]; 6 void solve() 7 { 8 int cost = 0; 9 int cur = 0; 10 for(int i = 0; i < m; i++) 11 { 12 if(night[i] >= dragon[cur]) 13 { 14 cost += night[i]; 15 if(++cur == n) 16 { 17 printf("%d\n", cost); 18 return ; 19 } 20 } 21 } 22 printf("Loowater is doomed!\n"); 23 return ; 24 } 25 int main() 26 { 27 while(~scanf("%d%d", &n, &m)) 28 { 29 if(n + m == 0) break; 30 for(int i = 0; i < n; i++) 31 scanf("%d", &dragon[i]); 32 for(int j = 0; j < m; j++) 33 scanf("%d", &night[j]); 34 sort(dragon, dragon + n); 35 sort(night, night + m); 36 solve(); 37 } 38 return 0; 39 }
題意:c++
n個任務,須要交代B分鐘,執行J分鐘,讓你合理選擇交代任務的次序,求得n個任務完成的最小總時長less
#include <bits/stdc++.h> using namespace std; const int maxn = 10000 + 5; const int INF = 0x3f3f3f3f; int n, Cas; int sum[maxn]; struct node { int j, b; bool operator < (const node &other)const { return j > other.j; } }a[maxn]; void solve() { Cas ++; sort(a, a + n); sum[0] = a[0].b; for(int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i].b; } int maxx = 0; for(int i = 0; i < n; i++) { sum[i] += a[i].j; maxx = max(maxx, sum[i]); } printf("Case %d: %d\n", Cas, maxx); } int main() { Cas = 0; while(~scanf("%d", &n) && n) { for(int i = 0; i < n; i++) { scanf("%d%d", &a[i].b, &a[i].j); } solve(); } return 0; }
題意:ide
n我的圍成一圈,每一個人都有一些硬幣,,每一個人只能給左右相鄰的人硬幣,問最少交換幾個硬幣,使每一個人硬幣同樣多;post
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1000000 + 5; 5 const int INF = 0x3f3f3f3f; 6 int n, Cas; 7 LL a[maxn], c[maxn]; 8 9 int main() 10 { 11 while(~scanf("%d", &n)) 12 { 13 LL sum = 0; 14 for(int i = 0; i < n; i++) 15 { 16 scanf("%lld", &a[i]); 17 sum += a[i]; 18 } 19 LL ave = sum / n; 20 c[0] = a[0] - ave; 21 for(int i = 1; i < n; i++) 22 { 23 c[i] = c[i - 1] + a[i] - ave; 24 } 25 sort(c, c + n); 26 LL ans = 0, d = c[n / 2]; 27 for(int i = 0; i < n; i++) 28 { 29 ans += abs(c[i] - d); 30 } 31 printf("%lld\n", ans); 32 } 33 return 0; 34 }
題意:ui
有不少只螞蟻在一條直線上,每一個螞蟻移動速度都是1,而且有一個初始方向。而且當相鄰兩個螞蟻相撞時轉向。如今問t時間後各個螞蟻的位置,及狀態。url
1 #include <stdio.h> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int mod = 1e9 + 7; 5 const int maxn = 10000 + 5; 6 const int INF = 0x3f3f3f3f; 7 typedef long long LL; 8 typedef pair<int, int>pii; 9 typedef pair<LL, LL>pLL; 10 typedef unsigned long long ull; 11 12 struct node 13 { 14 int id, pos, d;//這個id是輸入數據的順序 15 bool operator < (const node &other)const 16 { 17 return pos < other.pos; 18 } 19 }before[maxn], after[maxn]; 20 int order[maxn];//這個是螞蟻在繩索上的相對順序 21 const char dirName[][10] = {"L", "Turning", "R"}; 22 23 int main() 24 { 25 int Test; 26 scanf("%d", &Test); 27 for(int cas = 1; cas <= Test; cas++) 28 { 29 int L, T, n; 30 scanf("%d%d%d", &L, &T, &n); 31 for(int i = 0; i < n; i++) 32 { 33 int pos; 34 char dir; 35 scanf("%d %c", &pos, &dir); 36 int d = (dir == 'L') ? -1 : 1;//L -1 R 1 37 before[i] = {i, pos, d}; 38 after[i] = {0, pos + T * d, d}; 39 } 40 41 sort(before, before + n); 42 for(int i = 0; i < n; i++) 43 { 44 order[before[i].id] = i; 45 }//order 記錄 螞蟻在繩索上的順序 46 47 sort(after, after + n); 48 for(int i = 0; i < n - 1; i++) 49 { 50 if(after[i].pos == after[i + 1].pos) after[i].d = after[i + 1].d = 0; 51 } 52 printf("Case #%d:\n", cas); 53 for(int i = 0; i < n; i++) 54 { 55 int num = order[i]; 56 if(after[num].pos < 0 || after[num].pos > L) puts("Fell off"); 57 else printf("%d %s\n", after[num].pos, dirName[after[num].d + 1]); 58 } 59 puts(""); 60 } 61 return 0; 62 }
題意:通過複製/粘貼能夠變成原來的兩倍,求最小複製/粘貼次數。spa
1 #include <stdio.h> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int mod = 1e9 + 7; 5 const int maxn = 1000000 + 5; 6 const int INF = 0x3f3f3f3f; 7 typedef long long LL; 8 typedef unsigned long long ull; 9 10 int main() 11 { 12 int n ,cas = 1; 13 while(~scanf("%d", &n)) 14 { 15 if(n < 0) break; 16 17 int ans = 0, temp = 1; 18 while(n > temp) 19 { 20 temp <<= 1; 21 ans++; 22 } 23 printf("Case %d: %d\n", cas ++, ans); 24 } 25 return 0; 26 }
題意:.net
n個絕對值各不相同的非0整數,選出儘可能多的數,排成一個序列,使得正負號交替且絕對值遞增。
思路:
按絕對值排序,而後間次輸出正負數便可。
1 #include <stdio.h> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int mod = 1e9 + 7; 5 const int maxn = 500000 + 5; 6 const int INF = 0x3f3f3f3f; 7 typedef long long LL; 8 typedef unsigned long long ull; 9 10 int a[maxn]; 11 bool cmp(int &x, int &y) 12 { 13 return abs(x) < abs(y); 14 } 15 int main() 16 { 17 int t; 18 scanf("%d", &t); 19 while(t--) 20 { 21 int n; 22 scanf("%d", &n); 23 for(int i = 0; i < n; i++) 24 { 25 scanf("%d", &a[i]); 26 } 27 sort(a, a + n, cmp); 28 int last = a[0] > 0, len = 1; 29 for(int i = 1; i < n; i++) 30 { 31 int temp = a[i] > 0; 32 if(last != temp) 33 { 34 last = temp; 35 len++; 36 } 37 } 38 cout << len << "\n"; 39 } 40 return 0; 41 }
題意:
給你一塊M*N的巧克力,問把它切成最小單元須要最少切幾刀,分開的就不能一塊兒切了。
思路:
每次下刀,只能多增長一塊,一共須要切成m*n塊,因此顯然答案。
1 #include <stdio.h> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int mod = 1e9 + 7; 5 const int maxn = 1000000 + 5; 6 const int INF = 0x3f3f3f3f; 7 typedef long long LL; 8 typedef unsigned long long ull; 9 10 11 int main() 12 { 13 int n, m; 14 while(cin >> n >> m) cout << n * m - 1 << endl; 15 return 0; 16 }
題意:
從字符串s中刪除一些字符,可否獲得字符串t。
1 #include <stdio.h> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int mod = 1e9 + 7; 5 const int maxn = 1000000 + 5; 6 const int INF = 0x3f3f3f3f; 7 typedef long long LL; 8 typedef unsigned long long ull; 9 10 bool solve(string &s, string &t) 11 {//從t中刪除 可否獲得s 12 int lens = s.length(), lent = t.length(); 13 int cnt = 0; 14 for(int i = 0; i < lent; i++) 15 { 16 if(s[cnt] == t[i]) cnt++; 17 } 18 return cnt == lens; 19 } 20 21 int main() 22 { 23 string a, b; 24 while(cin >> a >> b) 25 { 26 printf("%s\n", solve(a, b) ? "Yes" : "No"); 27 } 28 return 0; 29 }
題意:
有一塊草坪,長爲l,寬爲w,在它的水平中心線上有n個位置能夠安裝噴水裝置,各個位置上的噴水裝置的覆蓋範圍爲以它們本身的半徑ri爲圓。求出最少須要的噴水裝置個數。
思路:
每一個圓,和草坪邊界交點,化成一個線段,而後問題就變成,最少須要幾個小區間覆蓋整個大區間。
1 #include <stdio.h> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int mod = 1e9 + 7; 5 const int maxn = 10000 + 5; 6 const int INF = 0x3f3f3f3f; 7 const double eps = 1e-8; 8 typedef pair<int, int>pii; 9 typedef pair<double, double>pdd; 10 typedef long long LL; 11 typedef unsigned long long ull; 12 13 int n, l; 14 double w; 15 pdd a[maxn]; 16 17 bool cmp(pii x, pii y) 18 { 19 if(x.first != y.first) return x.first < y.first; 20 return x.second < y.second; 21 } 22 23 int solve() 24 { 25 double last, temp; 26 if(a[0].first - 0 > eps) return -1; 27 28 int ans = 0; 29 double cur = 0, far = 0; 30 for(int i = 0; i < n; i++) 31 { 32 if(cur >= l) return ans; 33 if(a[i].first <= cur) 34 { 35 far = max(far, a[i].second); 36 } 37 else if(a[i].first > cur) 38 { 39 ans++; 40 cur = far; 41 if(a[i].first <= cur) 42 { 43 far = max(far, a[i].second); 44 } 45 else 46 { 47 return -1; 48 } 49 } 50 } 51 if(cur < l && far >= l) return ans + 1; 52 if(far < l) return -1; 53 return ans; 54 } 55 56 int main() 57 { 58 59 while(~scanf("%d%d%lf", &n, &l, &w)) 60 { 61 for(int i = 0; i < n; i++) 62 { 63 double pos, r; 64 scanf("%lf%lf", &pos, &r); 65 double d = sqrt(r * r - w / 2 * w / 2); 66 a[i].first = pos - d, a[i].second = pos + d; 67 } 68 sort(a, a + n, cmp); 69 70 cout << solve() << endl; 71 72 73 } 74 return 0; 75 }
題意:
給n個數字,將它們從新排序獲得一個最大的數字,好比給出123 456 789 拼爲 789456123 最大。
思路:
兩兩貪心!
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int mod = 1e9 + 7; 4 const int maxn = 50 + 5; 5 const int INF = 0x3f3f3f3f; 6 typedef long long LL; 7 typedef unsigned long long ull; 8 9 10 string num[maxn]; 11 bool cmp(string &a, string &b) 12 { 13 return a + b > b + a; 14 } 15 int main() 16 { 17 int n; 18 while(~scanf("%d", &n)) 19 { 20 if(n == 0) break; 21 for(int i = 0; i < n; i++) 22 { 23 cin >> num[i]; 24 } 25 sort(num, num + n, cmp); 26 for(int i = 0; i < n; i++) 27 { 28 cout << num[i]; 29 } 30 puts(""); 31 } 32 33 return 0; 34 }
題意:
在一個n*n(1<=n<=5000)的棋盤上放置n個車,每一個車都只能在給定的一個矩形裏放置,使其n個車兩兩不在同一行和同一列,判斷並給出解決方案。
思路:
轉換!這個問題能夠分解成x方向上的問題,和類似的y方向上的問題。而後這個問題就轉換成很經典的問題了,記不記得《挑戰》奶牛擦防曬霜那題,經典的適配有上下邊界的貪心。
1 #include <stdio.h> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int mod = 1e9 + 7; 5 const int maxn = 5000 + 5; 6 const int INF = 0x3f3f3f3f; 7 typedef long long LL; 8 typedef pair<int, int>pii; 9 typedef pair<LL, LL>pLL; 10 typedef unsigned long long ull; 11 12 struct node 13 { 14 int le, ri, order; 15 bool operator < (const node &other)const 16 { 17 return le < other.le || (le == other.le && ri < other.ri); 18 } 19 }x[maxn], y[maxn]; 20 int ansx[maxn], ansy[maxn]; 21 22 bool solve(int n, node z[], int ans[]) 23 { 24 sort(z, z + n); 25 priority_queue<pii, vector<pii>, greater<pii>>que; 26 int cnt = 0, ret = 0; 27 for(int i = 1; i <= n; i++) 28 { 29 while(cnt < n && z[cnt].le <= i) 30 { 31 que.push({z[cnt].ri, z[cnt].order}); 32 cnt++; 33 } 34 if(que.size() == 0) return false; 35 36 pii p = que.top(); 37 if(p.first < i) return false; 38 ans[p.second] = i; 39 que.pop(); 40 ret++; 41 } 42 return ret == n; 43 } 44 45 int main() 46 { 47 int n; 48 while(~scanf("%d", &n)) 49 { 50 if(n == 0) break; 51 for(int i = 0; i < n; i++) 52 { 53 scanf("%d%d%d%d", &x[i].le, &y[i].le, &x[i].ri, &y[i].ri); 54 x[i].order = y[i].order = i; 55 } 56 if(solve(n, x, ansx) && solve(n, y, ansy)) 57 { 58 for(int i = 0; i < n; i++) 59 { 60 printf("%d %d\n", ansx[i], ansy[i]); 61 } 62 } 63 else puts("IMPOSSIBLE"); 64 } 65 return 0; 66 }
題意:
n個旅行箱,形狀相同,尺寸不一樣,尺寸小的能夠放在尺寸大的旅行箱裏。如今要求露在最外面的旅行箱的數量最少的同時知足一個旅行箱裏放的旅行箱的數量最少。求出這樣知足要求的任意一種方案。
思路:
這題很噁心!!!白書上的描述少了一個,一個旅行箱中的旅行箱最少的條件。而後就很水筆了。
vector忘記清了!!!
1 /* 2 vector忘記清了。。 3 */ 4 #include <stdio.h> 5 #include <bits/stdc++.h> 6 using namespace std; 7 const int mod = 1e9 + 7; 8 const int maxn = 10000 + 5; 9 const int INF = 0x3f3f3f3f; 10 typedef pair<int, int>pii; 11 typedef long long LL; 12 typedef unsigned long long ull; 13 14 int a[maxn]; 15 vector<int>vec[maxn]; 16 17 int main() 18 { 19 int n; 20 while(~scanf("%d", &n)) 21 { 22 if(n == 0) break; 23 map<int, int>ma; 24 int num = 0; 25 for(int i = 0; i < n; i++) 26 { 27 scanf("%d", &a[i]); 28 ma[a[i]]++; 29 num = max(num, ma[a[i]]); 30 } 31 for(int i = 0; i < num; i++) vec[i].clear(); 32 sort(a, a + n); 33 for(int i = 0; i < n; i++) 34 { 35 vec[i % num].push_back(a[i]); 36 } 37 38 printf("%d\n", num); 39 for(int i = 0; i < num; i++) 40 { 41 int temp = 0; 42 for(auto o : vec[i]) 43 { 44 printf("%d%c", o, ++temp == vec[i].size() ? '\n' : ' '); 45 } 46 } 47 } 48 return 0; 49 }
題意:
有n個上午的任務和下午的任務,分配給司機,若是工做總時間超過d,超過的部分要給加班費;如今讓你安排任務,問最小的加班分花費。
思路:
排序一下,首尾加一下就行了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int mod = 1e9 + 7; 4 const int maxn = 100 + 5; 5 const int INF = 0x3f3f3f3f; 6 typedef long long LL; 7 typedef unsigned long long ull; 8 9 int day[maxn], night[maxn]; 10 11 int main() 12 { 13 int n, d, r; 14 while(~scanf("%d%d%d", &n, &d, &r)) 15 { 16 if(!n && !d && !r) break; 17 for(int i = 0; i < n; i++) 18 { 19 scanf("%d", &day[i]); 20 } 21 for(int i = 0; i < n; i++) 22 { 23 scanf("%d", &night[i]); 24 } 25 sort(day, day + n, greater<int>()); 26 sort(night, night + n, less<int>()); 27 28 LL sum = 0; 29 for(int i = 0; i < n; i++) 30 { 31 int temp = day[i] + night[i]; 32 if(temp > d) 33 { 34 sum += (temp - d) * r; 35 } 36 } 37 cout << sum << "\n"; 38 } 39 return 0; 40 }