#include<bits/stdc++.h>
using namespace std;typedeflonglong LL;typedef pair<int,int> PII;constint N =1010, INF =0x3f3f3f3f;int n, a[N];
PII b[N];
bool st[N];intcal(int t1,int t2){int ret =0;while(2* t1 < t2){ret ++;t1 *=2;}return ret;}intmain(){int t; cin >> t;while(t --){cin >> n;LL x =0;for(int i =1; i <= n; i ++){scanf("%d",&a[i]);}double cnt;for(int i =1, t1, t2; i < n; i ++){t1 =min(a[i], a[i +1]), t2 =max(a[i], a[i +1]);if(t1 *2>= t2)continue;else{x +=cal(t1, t2);}}cout << x << endl;}return0;}
直接上公式,沒有過,應該是小數精度會被卡
#include<bits/stdc++.h>
using namespace std;typedeflonglong LL;typedef pair<int,int> PII;constint N =1010, INF =0x3f3f3f3f;int n, a[N];intmain(){int t; cin >> t;while(t --){cin >> n;LL x =0;for(int i =1; i <= n; i ++){scanf("%d",&a[i]);}double cnt;for(int i =1, t1, t2; i < n; i ++){t1 =min(a[i], a[i +1]), t2 =max(a[i], a[i +1]);if(t1 *2>= t2)continue;else{x +=ceil(log(t2 *1.0/ t1)/log(2))-1;}}cout << x << endl;}return0;}
#include<bits/stdc++.h>
using namespace std;typedeflonglong LL;constint N =100010;
LL n;
LL a[N];
map<LL, bool> m;intmain(){int t; cin >> t;for(LL i =1; i <=10010; i ++)// 這里僅僅寫到 10000就會 WA,主要是怕他多往后走了一步,查詢到了0{a[i]= i * i * i;m[a[i]]= true;}// cout << m[0] << endl;while(t --){scanf("%lld",&n);bool flag = false;LL surp;for(int i =1;!flag && a[i]<= n; i ++){surp = n - a[i];if(m[surp]== true){flag = true;}}if(flag)puts("YES");elseputs("NO");}return0;}
D. Permutation Transformation
http://codeforces.com/contest/1490/problem/D
一個簡單的模擬dfs,每次在區間找最大值作為 root 即可
#include<bits/stdc++.h>
using namespace std;constint N =110;int a[N], n, depth[N];voidbuild(int fa,int l,int r){if(l >= r)return;int u =-1, v =-1;// 左子樹for(int i = l; i < fa; i ++){if(u ==-1|| a[u]< a[i])u = i;}depth[u]= depth[fa]+1;build(u, l, fa -1);// 右子樹for(int i = fa +1; i <= r; i ++){if(v ==-1|| a[v]< a[i])v = i;}depth[v]= depth[fa]+1;build(v, fa +1, r);}intmain(){int t; cin >> t;while(t --){scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&a[i]);memset(depth,-1,sizeof depth);int v;for(int i =1; i <= n; i ++){if(a[i]== n){v = i;break;}}depth[v]=0;build(v,1, n);cout << depth[1];for(int i =2; i <= n; i ++)printf(" %d", depth[i]);cout << endl;}return0;}
#include<bits/stdc++.h>
using namespace std;typedeflonglong LL;typedef pair<LL,int> PII;constint N =200010;
vector<int> res;
LL a[N];
LL b[N];
PII c[N];int n;bool cmp(const PII &t1,const PII &t2){return t1.first < t2.first;}intmain(){int t; cin >> t;while(t --){scanf("%d",&n);for(int i =1; i <= n; i ++){scanf("%lld",&a[i]);c[i].first = a[i];c[i].second = i;}sort(c +1, c + n +1, cmp);for(int i =1; i <= n; i ++)b[i]= b[i -1]+ c[i].first;res.clear();res.push_back(c[n].second);for(int i = n -1; i >=1; i --){if(b[i]>= c[i +1].first)res.push_back(c[i].second);elsebreak;}sort(res.begin(), res.end());cout << res.size()<< endl;cout << res[0];for(int i =1; i < res.size(); i ++)printf(" %d", res[i]);cout << endl;}return0;}