A - Infinite Sequence
CodeForces - 622A
题意:第一个数是1,接下来是1和2,接下来是1,2, 3,接下来是1,2,3, 4,问第n个数是什么
题解:找出第几轮在找出第几个
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #define INF 0x3f3f3f3f #define lowbit(x) (x&(-x)) using namespace std; typedef long long ll; const int maxn = 1e2 + 10; int main() { ll n; while(~scanf("%lld",&n)) { ll tmp; tmp = sqrt(2 * n * 1.0); if(tmp * (tmp + 1) > 2 * n) tmp--; if(n - (tmp * (tmp + 1)) / 2 == 0) printf("%lld\n",tmp); else printf("%lld\n",n - (tmp * (tmp + 1)) / 2); } }
B - Not Equal on a Segment
CodeForces - 622C
题意:给你一个数组,给你m个询问,每个询问一个区间, 一个数值,在区间里哪一个位置不是这个数值,输出任意一个就行
题解:找到区间里的不一样的两个数字比较一下就好了
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #define INF 0x3f3f3f3f #define lowbit(x) (x&(-x)) using namespace std; typedef long long ll; const int maxn = 2e5 + 10; int a[maxn]; struct node{ int st,en; int ans; }b[maxn]; int flag[maxn]; int main() { int n, q; scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int pos = 0; for (int i = 1; i <= n; i++) { if(i == 1) { b[pos].st = i; b[pos].ans = a[i]; } else { if(a[i] != a[i-1]) { b[pos].en = i - 1; pos++; b[pos].st = i; b[pos].ans = a[i]; } } } b[pos].en = n; int id = 1; for(int i=0;i<=pos;i++) { for(int j=b[i].st;j<=b[i].en;j++) flag[j] = id; id++; } // for(int i=0;i<=pos;i++) // printf("%d %d %d\n",b[i].st,b[i].en,b[i].ans); // cout<<endl; // for(int i=1;i<=n;i++) // printf("%d ",flag[i]); while(q--) { int l,r,x; scanf("%d %d %d",&l,&r,&x); if(flag[l] == flag[r] && a[l] == x) { puts("-1"); continue; } else if(flag[l] == flag[r] && a[r] != x) { printf("%d\n",l); continue; } else { int id1 = flag[l]; id1--; if(b[id1].ans != x) printf("%d\n",l); else { printf("%d\n",b[id1 + 1].st); } } } }
C - Optimal Number Permutation
CodeForces - 622D
题意:用1-n组长度为2n的序列,要求每个数出现2次,假设位置分别为xi、yi(xi < yi),定义di = yi-xi。让你找到一种排列使得sigma((n-i) * |di+i-n|)最小。
题解:贪心+找规律
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #define INF 0x3f3f3f3f #define lowbit(x) (x&(-x)) using namespace std; typedef long long ll; const int maxn = 2e6 + 10; int a[maxn]; int main() { int n; memset(a,0,sizeof a); scanf("%d",&n); int posa = 1; int posb = n + 1; for(int i=1;i<n;i++) { if(i % 2 == 1) { a[posa] = i; a[n + 1 - posa] = i; posa++; } else { a[posb] = i; a[3 * n - posb] = i; posb++; } } for(int i = 1;i <= 2 * n;i++) { if(a[i] == 0) printf("%d ",n); else printf("%d ",a[i]); } }
D - Ants in Leaves
CodeForces - 622E
题意:给定一颗树,每个叶子节点上有一蚂蚁,除了根结点的之外的所有节点任意时刻至多只能有一个蚂蚁,每个蚂蚁每秒能移动到相邻的节点上,问所有蚂蚁移动到根结点的最短时间是多少。
题解:深度小的叶子蚂蚁先走,这样深度小的叶子蚂蚁就不用等深度大的叶子蚂蚁。然后算出每个子树走完的所需时间。d[i] = max(d[i - 1] + 1,d[i])
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #define INF 0x3f3f3f3f #define lowbit(x) (x&(-x)) using namespace std; typedef long long ll; const int maxn = 5e5 + 10; int n; vector<int>g[maxn],d; void dfs(int u,int fa,int deep) { if(g[u].size() == 1) { d.push_back(deep); // printf("%d\n",deep); } for(int i=0;i<g[u].size();i++) { int v = g[u][i]; if(v == fa) continue; dfs(v,u,deep+1); } } int main() { while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) g[i].clear(); for(int i=1;i<n;i++) { int u,v; scanf("%d %d",&u,&v); g[u].push_back(v); g[v].push_back(u); } int ans = 0; for(int k = 0; k < g[1].size(); k++) { d.clear(); dfs(g[1][k],1,1); sort(d.begin(),d.end()); for(int i = 1; i < d.size(); i++) { d[i] = max(d[i - 1] + 1,d[i]); //cout<<d[i]<<endl; } ans = max(ans,d.back()); } printf("%d\n",ans); } return 0; }
E - The Sum of the k-th Powers
CodeForces - 622F
题意:求 mod (1e9 + 7)
原文地址:https://www.cnblogs.com/smallhester/p/11173966.html
时间: 2024-10-11 07:21:51