A题,水题,还是无法1Y.
B题,题意是类似背包的问题,在v的容量下,有1重量和2重量的,如果达到价值最大。
贪心,写的很恶心。看着数据过了。
奇数的时候,先选一个1。之后然后1+1 和 2 比较就行了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <string> #include <cstring> #include <cmath> #include <cstdlib> #include <ctime> #include <algorithm> #include <iostream> using namespace std; #define eps 1e-10 int flag[100001]; struct node { int t,p,id; } s[100001]; int cmp(node a,node b) { if(a.t == b.t) return a.p > b.p; else return a.t < b.t; } int main() { int i,n,v,s1,s2,e1,e2,sum,ans; scanf("%d%d",&n,&v); for(i = 0; i < n; i ++) { scanf("%d%d",&s[i].t,&s[i].p); s[i].id = i; } sort(s,s+n,cmp); sum = 0; for(i = 0; i < n; i ++) { if(s[i].t == 2) break; } if(v%2 == 1&&i > 0) { sum = 1; ans = s[0].p; s1 = 1; e1 = i-1; flag[s[0].id] = 1; } else { sum = 0; ans = 0; s1 = 0; e1 = i-1; } s2 = i; e2 = n-1; for(;;) { if(sum == v) break; if(s1 > e1&&s2 > e2) break; if(s1 > e1) { if(sum + 2 > v) break; else { flag[s[s2].id] = 1; sum += 2; ans += s[s2].p; s2 ++; } } else if(s2 > e2) { flag[s[s1].id] = 1; sum ++; ans += s[s1].p; s1 ++; } else { if(sum + 2 > v) { flag[s[s1].id] = 1; sum ++; ans += s[s1].p; s1 ++; } else { if(s1 + 1 <= e1) { if(s[s1].p + s[s1+1].p > s[s2].p) { flag[s[s1].id] = 1; flag[s[s1+1].id] = 1; sum += 2; ans += s[s1].p + s[s1+1].p; s1 += 2; } else { flag[s[s2].id] = 1; sum += 2; ans += s[s2].p; s2 ++; } } else { if(s[s1].p > s[s2].p) { flag[s[s1].id] = 1; sum += 1; ans += s[s1].p; s1 += 1; } else { flag[s[s2].id] = 1; sum += 2; ans += s[s2].p; s2 ++; } } } } } printf("%d ",ans); for(i = 0;i < n;i ++) { if(flag[i]) printf("%d ",i+1); } return 0; }
C题,错了,好多次,胜利的时候,保证胜利一方走最后一步,注意非法的判断,最后,认真检查代码。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <string> #include <cstring> #include <cmath> #include <cstdlib> #include <ctime> #include <algorithm> #include <iostream> using namespace std; char str[10][10]; int judge(char s) { int i; for(i = 0;i < 3;i ++) { if(str[i][0] == s&&str[i][1] == s &&str[i][2] == s) return 1; } for(i = 0;i < 3;i ++) { if(str[0][i] == s&&str[1][i] == s &&str[2][i] == s) return 1; } if(str[0][0] == s&&str[1][1] == s&&str[2][2] == s) return 1; if(str[0][2] == s&&str[1][1] == s&&str[2][0] == s) return 1; return 0; } int main() { int i,a,b,j,f1,f2; for(i = 0;i < 3;i ++) scanf("%s",str[i]); a = b = 0; for(i = 0;i < 3;i ++) { for(j = 0;j < 3;j ++) { if(str[i][j] == '0') b ++; else if(str[i][j] == 'X') a ++; } } f1 = f2 = 0; if(a - b > 1||b > a) { printf("illegal"); return 0; } f1 = judge('X'); f2 = judge('0'); if(f1&&f2) printf("illegal"); else if(f1) { if(a > b) printf("the first player won "); else printf("illegal"); } else if(f2) { if(a == b) printf("the second player won "); else printf("illegal"); } else if(a + b == 9) printf("draw "); else if(a > b) printf("second "); else if(a == b) printf("first "); return 0; }
D题,神贪心题,题意很简单。
又是,看的题解。。。先把?当做右括号来处理,将b[i]-a[i]放入优先队列(大顶堆)里,然后如果出现括号不匹配,判断堆里是否有元素,将堆定 变成(,顺便记录一下路径。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <string> #include <cstring> #include <cmath> #include <cstdlib> #include <ctime> #include <queue> #include <vector> #include <algorithm> #include <iostream> using namespace std; char str[100000]; int a[100000]; int b[100000]; int s[100000]; struct node { int w,id; }; struct cmp { bool operator ()(const node &a,const node b) { return a.w < b.w; } }; int main() { int i,len,flag,num; long long ans; cin>>str; len = strlen(str); priority_queue<node,vector<node>,cmp> que; flag = 0; num = 0; ans = 0; for(i = 0;i < len;i ++) { if(str[i] == '(') flag ++; else if(str[i] == ')') flag --; else { flag -- ; scanf("%d%d",&a[num],&b[num]); node temp; temp.w = b[num] - a[num]; ans += b[num]; s[num] = 1; temp.id = num; que.push(temp); num ++; } if(flag < 0) { if(que.empty()) break; else { flag += 2; node temp; temp = que.top(); s[temp.id] = 0; que.pop(); ans -= temp.w; } } } if(flag == 0) cout<<ans<<endl; else { cout<<"-1"<<endl; return 0; } num = 0; for(i = 0;i < len;i ++) { if(str[i] == '?') { if(s[num]) cout<<")"; else cout<<"("; num ++; } else cout<<str[i]; } printf(" "); return 0; }