排排序,找找做题的感觉...
对了,longlong用C++ 错了几次,我也是linux选手了....
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <vector> 6 #include <queue> 7 #include <algorithm> 8 using namespace std; 9 #define MOD 1000000007 10 #define LL __int64 11 struct node 12 { 13 int u,c,num; 14 LL l; 15 }p[500001]; 16 int flag[500001]; 17 LL sum[500001]; 18 int cmp(node a,node b) 19 { 20 if(a.u == b.u) 21 return a.c > b.c; 22 else 23 return a.u < b.u; 24 } 25 int cmp1(node a,node b) 26 { 27 return a.num < b.num; 28 } 29 int main() 30 { 31 int t,cas = 1,n,m,q,i,st,maxz; 32 scanf("%d",&t); 33 while(t--) 34 { 35 scanf("%d%d%d",&n,&m,&q); 36 for(i = 0;i < n;i ++) 37 flag[i] = 0; 38 for(i = 0;i < m;i ++) 39 scanf("%d%d%I64d",&p[i].u,&p[i].c,&p[i].l); 40 sort(p,p+m,cmp); 41 p[0].num = 1; 42 for(i = 1;i < m;i ++) 43 { 44 if(p[i].u != p[i-1].u) 45 p[i].num = 1; 46 else 47 p[i].num = p[i-1].num + 1; 48 } 49 sort(p,p+m,cmp1); 50 sum[0] = p[0].l; 51 flag[p[0].num] = 0; 52 maxz = 1; 53 for(i = 1;i < m;i ++) 54 { 55 if(p[i].num != p[i-1].num) 56 flag[p[i].num] = i; 57 sum[i] = sum[i-1] + p[i].l; 58 maxz = max(maxz,p[i].num); 59 } 60 printf("Case #%d: ",cas++); 61 for(i = 0;i < q;i ++) 62 { 63 scanf("%d",&st); 64 if(st <= 0) 65 printf("0 "); 66 else if(st >= maxz) 67 printf("%I64d ",sum[m-1]); 68 else 69 printf("%I64d ",sum[flag[st+1]-1]); 70 } 71 } 72 return 0; 73 }