收获了8个hack,本来排名50+,结果C题没有用long long ,错过了一次绝佳的变紫机会...
A题:简单题。快速幂水过。
View Code
B题:简单题。没看清题目WA了一次。
View Code
1 /* 2 Author:Zhaofa Fang 3 Lang:C++ 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 #include <sstream> 8 #include <iostream> 9 #include <cmath> 10 #include <cstring> 11 #include <algorithm> 12 #include <string> 13 #include <utility> 14 #include <vector> 15 #include <queue> 16 #include <stack> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define REP(i,n) for(int i=0;i < (n);i++) 24 #define REPD(i,n) for(int i=(n-1);i >= 0;i--) 25 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 26 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 35 36 char str[200005]; 37 int main(){ 38 //freopen("in","r",stdin); 39 //freopen("out","w",stdout); 40 int n; 41 while(~scanf("%d",&n)){ 42 scanf("%s",str); 43 int len = strlen(str); 44 int cnt = 0; 45 REP(i,len){ 46 if(str[i] == 'I'){ 47 cnt++; 48 } 49 } 50 int ans = 0; 51 if(!cnt) 52 REP(i,len){ 53 if(str[i] == 'A')ans++; 54 55 } 56 else if(cnt == 1)ans = 1; 57 printf("%d\n",ans); 58 } 59 return 0; 60 }
C题:其实不难,但是long long 和操作1的成段更新卡死了一堆人,用了一个大数据收获8hack。我做法是用线段树来维护,但不用这么复杂也能做。
线段树
1 /* 2 Author:Zhaofa Fang 3 Lang:C++ 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 #include <sstream> 8 #include <iostream> 9 #include <cmath> 10 #include <cstring> 11 #include <algorithm> 12 #include <string> 13 #include <utility> 14 #include <vector> 15 #include <queue> 16 #include <stack> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define REP(i,n) for(int i=0;i < (n);i++) 24 #define REPD(i,n) for(int i=(n-1);i >= 0;i--) 25 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 26 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 35 #define lson l , m , rt << 1 36 #define rson m + 1 , r , rt << 1 | 1 37 38 const int maxn = 200011; 39 ll sum[maxn<<2]; 40 ll add[maxn<<2]; 41 42 void PushUp(int rt) 43 { 44 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 45 } 46 void PushDown(int rt,int m) 47 { 48 if(add[rt]) 49 { 50 add[rt<<1] += add[rt]; 51 add[rt<<1|1] += add[rt]; 52 sum[rt<<1] += add[rt]*(m - (m >> 1)); 53 sum[rt<<1|1] += add[rt]*(m >> 1); 54 add[rt] = 0; 55 } 56 } 57 58 void update(int L,int R,int val,int l,int r,int rt) 59 { 60 if(L <= l && r <= R) 61 { 62 add[rt] +=val; 63 sum[rt] += val*(r - l + 1); 64 return; 65 } 66 PushDown(rt , r - l + 1); 67 int m =(l+r) >> 1; 68 if(L <= m)update(L,R,val,lson); 69 if(m < R)update(L,R,val,rson); 70 PushUp(rt); 71 } 72 ll query(int L,int R,int l,int r,int rt) 73 { 74 if(L <= l && r <= R) 75 { 76 return sum[rt]; 77 } 78 if(l == r)return 0; 79 PushDown(rt , r - l + 1); 80 int m = (l + r) >> 1; 81 ll ret = 0; 82 if(L <= m)ret += query(L,R,lson); 83 if(m<R)ret += query(L,R,rson); 84 return ret; 85 } 86 int main(){ 87 //freopen("in","r",stdin); 88 //freopen("out","w",stdout); 89 int n; 90 stack<int>Q; 91 scanf("%d",&n); 92 memset(sum,0,sizeof(sum)); 93 memset(add,0,sizeof(add)); 94 int op,k,a,x; 95 int len = 1; 96 ll sum = 0; 97 REP(i,n){ 98 scanf("%d",&op); 99 double ans; 100 if(op == 1){ 101 scanf("%d%d",&a,&x); 102 update(1,min(a,len),x,1,n,1); 103 }else if(op == 2){ 104 scanf("%d",&k); 105 Q.push(k); 106 len ++;//DEBUG(sum); 107 sum+=k; 108 }else { 109 if(len>=2){ 110 int tmp = query(len,len,1,n,1); 111 update(len,len,-tmp,1,n,1); 112 sum -= Q.top(); 113 Q.pop();len--; 114 } 115 } 116 ans = 1.0*(query(1,len,1,n,1)+sum)/len; 117 printf("%.6f\n",ans); 118 } 119 return 0; 120 }
D题:只是a1未知,设状态dp[i][0]为从step2开始的dp值,dp[i][1]则为step3,则有
dp[i][0] = dp[i+ai][1] + ai;
dp[i][1] = dp[i-ai][0] + ai;
记忆化搜素一下,存在环则无解,当a1 = i;答案为dp[i+1][1]+i;
View Code
1 /* 2 Author:Zhaofa Fang 3 Lang:C++ 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 #include <sstream> 8 #include <iostream> 9 #include <cmath> 10 #include <cstring> 11 #include <algorithm> 12 #include <string> 13 #include <utility> 14 #include <vector> 15 #include <queue> 16 #include <stack> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define REP(i,n) for(int i=0;i < (n);i++) 24 #define REPD(i,n) for(int i=(n-1);i >= 0;i--) 25 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 26 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 35 #define lson l , m , rt << 1 36 #define rson m + 1 , r , rt << 1 | 1 37 38 const int maxn = 200011; 39 ll dp[maxn][2],a[maxn]; 40 int n; 41 bool vist[maxn][2]; 42 ll dfs(int p,int q){ 43 if(p<=0 || p>n)return 0; 44 if(p == 1)return -1; 45 if(vist[p][q])return dp[p][q]; 46 vist[p][q] = 1; 47 int tmp1; 48 if(!q)tmp1 = p + a[p]; 49 else tmp1 = p - a[p]; 50 ll tmp2 = dfs(tmp1,q^1); 51 if(tmp2 == -1)dp[p][q] = -1; 52 else dp[p][q] = tmp2 + a[p]; 53 return dp[p][q]; 54 } 55 56 int main(){ 57 //freopen("in","r",stdin); 58 //freopen("out","w",stdout); 59 memset(dp,-1,sizeof(dp)); 60 scanf("%d",&n); 61 FOR(i,2,n)scanf("%I64d",a+i); 62 FOR(i,2,n){ 63 if(!vist[i][0])dfs(i,0); 64 if(!vist[i][1])dfs(i,1); 65 } 66 FOR(i,1,n-1) 67 if(dp[i+1][1] == -1)puts("-1"); 68 else printf("%I64d\n",dp[i+1][1]+i); 69 return 0; 70 }