ACM-ICPC(11/8)
阿新 • • 發佈:2017-11-09
精度 close txt ble splay rip display esp ast URAL 1005
給定一些石頭的重量,要求分成兩個部分最相近。二進制暴力枚舉。
#include <bits/stdc++.h> using namespace std; int w[25]; int main() { // freopen("in.txt","r",stdin); int n; scanf("%d",&n); int sum = 0; for(int i = 0; i < n; i++) { scanf("%d",&w[i]); sumView Code+=w[i]; } int ans = sum; for(int s = 0; s < 1<<n; s++) { int tmp = 0; for(int i = 0; i < n; i++) { if(s&(1<<i)) tmp+=w[i]; } ans = min(ans,abs(2*tmp-sum)); } cout<<ans<<endl; return 0; }
URAL 1009
k進制,n位數,要求沒有前導0,並且沒有兩個連續的0.
數據範圍很小,暴力竟然超時了,隊友的公式找了半天。要崩~~~
還好找出來了。
n = 1 ans = k-1
n = 2 ans = k*k -k
n = 3 ans = (k-1)*(ans2+ans1)
#include <bits/stdc++.h> using namespace std; int ans; int n,k; void dfs(int u,int x) { if(u==n) { ansView Code++; return ; } if(x==0) { for(int i = 1; i <k; i++) dfs(u+1,i); } else { for(int i = 0;i<k; i++) { dfs(u+1,i); } } } int a[20]; int main() { scanf("%d%d",&n,&k); a[1] = k - 1; a[2] = k*k - k; for(int i = 3; i <= n; i++) a[i] = (k-1)*(a[i-1]+a[i-2]); printf("%d\n",a[n]); return 0; }
URAL 1011
售票員的人數是p%~q%不包含邊界。求城市最少人數。
精度卡到炸~~~
#include <cstdio> #include <cmath> const double eps = 1e-9; int main() { double p, q; int ans; scanf("%lf%lf", &p, &q); for (ans = 1; ; ++ans) { double least = ans * p / 100.0; int realLeast = (int) ceil(least); double most = ans * q / 100.0; int realMost = (int) floor(most); if (realMost >= realLeast && most - realMost > eps && realLeast - least > eps) break; } printf("%d\n", ans); return 0; }View Code
URAL 1010
橫坐標1~n,縱坐標給出,求兩個點,兩點之間的點都在下方,兩點的斜率絕對值最大。
其實就是相鄰的點。
#include <bits/stdc++.h> using namespace std; long long a[100005]; int main() { int n; scanf("%d",&n); for(int i = 1; i <= n;i++) cin>>a[i]; long long ans = 0; int p; for(int i = 1; i <= n-1; i++) { if(abs(a[i+1]-a[i])>ans) { p = i; ans = abs(a[i+1] - a[i]); } } printf("%d %d\n",p,p+1); return 0; }View Code
ACM-ICPC(11/8)