// Vijos / Test bank /
Water King
Sort can be customized in structure sorting. I also know that it can be customized before, but I didn't expect such a wide range of applications! ! !
This question is well-known. I just started custom sorting, and I haven't found an error yet, but I haven't. . .
#include<iostream> #include<queue> #include<cstring> #include<algorithm> using namespace std; #define maxn 11000 struct BigNum { int len; char num[maxn]; char ID[maxn]; }n[maxn]; bool compare(BigNum a,BigNum b) { if(a.len>b.len) return a.len>b.len; if(a.len==b.len){ if(strcmp(a.num,b.num)==0){ return strcmp(a.ID,b.ID)<0; } else return strcmp(a.num,b.num)>0; } return false; } int main() { int m; cin>>m; for(int i=1;i<=m;i++) {
cin>>n[i].ID>>n[i].num; for(int j=0;n[i].num[j]!='\0';j++,n[i].len++); } sort(n+1,n+1+m,compare); for(int i=1;i<=m;i++) cout<<n[i].ID<<endl; return 0; }
King game
// Greedy is not very familiar, come back and learn again. This question needs to be proved with greed
The proof written by the big brother, a previous question on the Niuke online is also a similar proof:
The first version of the vijos problem solution is the proof written by this big guy, quote it (prove you can learn it, the next question will quote the problem on the cattle guest):
### Greedy proofs
assume that the left and right hands of two adjacent people are (a, b), (A, B). Let a * b <= A * B, the left-hand product of everyone before i is S. i + 1 position (A, B), i position is (a, b),
then ans1 = max {S / b, S * a / B}
If exchange,
then ans2 = max {S / B, S * A / b)
Because, a * b <= A * B,
so S * a / B <= S * A / b
and because, S / B <= S * a / B,
so ans2 = S * A / b
ans1 = max {S / B [i], S * a / B}
So, ans1 <= ans2
So you need to sort, custom sort + sort
According to the data range, n <= 1000, a, b <= 10000, the maximum product of the data is 10 ^ 12, so high precision * low precision + high precision divided by low precision
To be honest, this question can be a distress to me; some people say it is a water question, but I still have more knowledge. learned. .
After I first saw the greedy proof, I didn't think about it so much. I thought that the last one was the solution, but I didn't know that the optimal solution might be in the middle. ~~~
#include <iostream> #include <queue> #include <cstring> #include <algorithm> using namespace std; #define maxn 11000 #define p 1e9 struct BigNum { long long left, right; } n [maxn]; long long m, max1 [maxn]; struct Big { int len; int num [maxn]; }; Big a, b, ans; bool compare (BigNum x, BigNum y) { return x.left*x.right<y.left*y.right; }//sort比较,进行排序 void multi(int h) { for(int i=0;i<a.len;i++) a.num[i]=a.num[i]*h; for(int i=0;i<a.len;i++) { a.num[i+1]+=a.num[i]/10; a.num[i]%=10; if(i==a.len-1&&a.num[i+1]) ++a.len; } }//高精乘以低精 void dev(int h) { int t=0; ans.len=a.len; for(int i=ans.len-1;i>=0;i--) { t=t*10+a.num[i]; ans.num[i]=t/h; t=t%h; } while(ans.len>1&&!ans.num[ans.len-1]) --ans.len; }//高精除以低精 bool com(Big x,Big y) { if(x.len<y.len) return 1 ; if (x.len == y.len) { for ( int i = x.len- 1 ; i> = 0 ; i-- ) if (x.num [i] <y.num [i] ) return 1 ; } return false ; } // The solution may also be in the middle, you need to compare int main () { cin >> m; cin >> n [ 0 ] .left >> n [ 0 ] .right; for ( int i = 1 ; i <= m; i ++ ) { cin >> n [i] .left >> n [i] .right; } sort (n {+ 1 , n + 1 + m, compare); int h = n [ 0 ] .left; // The king stays in the first place while (h) { a.len ++ ; a.num [a.len - 1 ] = h% 10 ; h = h / 10 ; } for ( int i = 1 ; i <m; i ++ ) // Find the optimal solution { multi (n [i] .left); dev (n [i + 1 ] .right); if (com (b, ans)) b.len =ans.len; for(int i=ans.len-1;i>=0;i--) b.num[i]=ans.num[i]; } } for(int i=b.len-1;i>=0;i--) cout<<b.num[i]; return 0; }
// Off topic, the following has nothing to do with high precision, it is a greedy proof
Link: https://ac.nowcoder.com/acm/contest/3003/F
Source: Niuke.com
Title description
When looking at the question, pit yourself, a, b are input separately.
Assuming that Niu Niu chooses ai, bi, Niu Cola chooses ai + 1, bi + 1, ai + bi <ai + 1 + bi + 1, the
same as the above proof
#include <algorithm > #include <iostream> using namespace std; struct bel { int a, b; int num; } B [ 1000000 ]; bool compare (bel x, bel y) { return x.a + xb> y.a + y.b; } // sort int main () { int n; cin >> n; for ( int i = 1 ; i <= n; i ++) B [i] .num = i, cin >> B [i] .a; for(int i=1;i<=n;i++) cin>>B[i].b; sort(B+1,B+1+n,compare); for(int i=1;i<=n;i+=2) { cout<<B[i].num<<' '; } cout<<endl; for(int i=2;i<=n;i+=2) { cout<<B[i].num<<' '; } return 0; }