第一题,水题,但是题目读了很久,英语功底果然很差,主要是第二次排序的时候是按位排序,先按个位从小到大,然后是十位从小到大。题目描述太奇葩,这句话领会了很久。。
POJ 4044
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <queue> #include <set> #include <vector> #include <stack> #include <map> #include <iomanip> #define PI acos(-1.0) #define Max 2005 #define inf 1<<28 #define LL(x) (x<<1) #define RR(x) (x<<1|1) #define FOR(i,s,t) for(int i=(s);i<=(t);++i) #define ll long long using namespace std; int a[50],b[50]; int v[200]; bool cmp(int a,int b) { return a>b; } bool cmp1(int a,int b) { if(a%10!=b%10)return (a%10)<(b%10); return (a/10)<(b/10); } int main() { int T; int n,m; cin>>T; while(T--) { cin>>n>>m; int t; memset(v,0,sizeof(v)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int num1=0; for(int i=0;i<n;i++) { cin>>t; if(!v[t]) { a[num1++]=t; v[t]=1; } } memset(v,0,sizeof(v)); int num2=0; for(int i=0;i<m;i++) { cin>>t; if(!v[t]) { v[t]=1; b[num2++]=t; } } sort(a,a+num1,cmp); sort(b,b+num2,cmp); int mm=0; int index=-1; int k; for(int i=0;i<num1;i++) { for(int j=0;j<num2;j++) { if(a[i]==b[j]) { for(k=1;k+i<num1&&k+j<num2;k++) { if(a[i+k]!=b[j+k]) break; } if(mm<k) { mm=k; index=i; } } } } int ans[105]; int ans1=0; if(!mm) { cout<<"NONE"<<endl; continue; } bool flag=0; for(int i=index;i<index+mm;i++) { ans[ans1++]=a[i]; if(flag) cout<<" "; cout<<a[i]; flag=1; } cout<<endl; sort(ans,ans+ans1,cmp1); cout<<ans[0]; for(int i=1;i<ans1;i++) cout<<" "<<ans[i]; cout<<endl; } return 0; } /* 4 4 4 10 10 30 20 50 50 30 20 8 8 10 38 27 55 44 66 75 66 38 27 77 44 55 66 66 98 7 8 50 40 30 20 10 5 25 50 40 30 20 10 5 24 23 4 4 10 20 30 40 50 60 70 80 */
poj 4045 树形DP,下次写注释
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <queue> #include <set> #include <vector> #include <stack> #include <map> #include <iomanip> #define PI acos(-1.0) #define Max 2005 #define inf 1<<28 #define LL(x) (x<<1) #define RR(x) (x<<1|1) #define FOR(i,s,t) for(int i=(s);i<=(t);++i) #define ll long long using namespace std; int head[100005]; struct kdq { int u,v; int next; } edge[100005]; int num=0; int n; ll node[50005],dp[50005]; bool vis[50005]; void addedge(int u,int v) { edge[num].u=u; edge[num].v=v; edge[num].next=head[u]; head[u]=num++; } void dfs(int k) { vis[k]=1; dp[k]=0; node[k]=1; for(int i=head[k]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(!vis[v]) { dfs(v); dp[k]=dp[k]+dp[v]+node[v]; node[k]+=node[v]; } } } void dfs1(int k) { vis[k]=1; for(int i=head[k]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(!vis[v]) { dp[v]=dp[k]-2*node[v]+n; dfs1(v); } } } int main() { int T; cin>>T; int I,R; int a,b; while(T--) { cin>>n>>I>>R; int d=n-1; num=0; memset(head,-1,sizeof(head)); while(d--) { scanf("%d%d",&a,&b); addedge(a,b); addedge(b,a); } memset(vis,0,sizeof(vis)); dfs(1); memset(vis,0,sizeof(vis)); dfs1(1); ll MIN=(((ll)1)<<61); for(int i=1;i<=n;i++) { if(dp[i]<MIN) MIN=dp[i]; } bool flag=0; cout<<I*I*R*MIN<<endl; for(int i=1;i<=n;i++) if(dp[i]==MIN) { if(flag) cout<<" "; cout<<i; flag=1; } cout<<endl<<endl; } return 0; }
POJ 4047 线段树
已经优化不动了。
#include <set> #include <map> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <string> #include <vector> #include <iomanip> #include <cstring> #include <iostream> #include <algorithm> #define Max 2505 #define FI first #define SE second #define ll long long #define PI acos(-1.0) #define inf 0x3fffffff #define LL(x) ( x << 1 ) #define bug puts("here") #define PII pair<int,int> #define RR(x) ( x << 1 | 1 ) #define mp(a,b) make_pair(a,b) #define mem(a,b) memset(a,b,sizeof(a)) #define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i ) using namespace std; #define N 200001 inline void RD(int &ret) { char c , x ; int flag = 1 ; do { x = c ; c = getchar() ; } while(c < '0' || c > '9') ; if(x == '-')flag = -1 ; ret = c - '0'; while((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + ( c - '0' ) ; ret *= flag ; } inline void OT(int a) { if(a >= 10)OT(a / 10); putchar(a % 10 + '0'); } inline int max(const int &a ,const int &b){ return a > b ? a : b ; } int a[N] ; int sum[N] ; int ss[N] ; int n , m , k ; struct KK{ int l , r , add , mx ; }T[N * 3] ; void PU(int x){ T[x].mx = max(T[LL(x)].mx , T[RR(x)].mx) ; } void PD(int x){ if(T[x].add){ T[LL(x)].add += T[x].add ; T[RR(x)].add += T[x].add ; T[LL(x)].mx += T[x].add ; T[RR(x)].mx += T[x].add ; T[x].add = 0 ; } } void build(int l , int r , int x){ T[x].l = l , T[x].r = r ; T[x].add = 0 ; if(l == r){ T[x].mx = ss[l] ; return ; } int mid = l + r >> 1 ; build(l , mid , LL(x)) ; build(mid + 1 , r ,RR(x)) ; PU(x) ; } void update(int l , int r , int x , int num){ if(l > T[x].r || r < T[x].l)return ; if(l == T[x].l && r == T[x].r){ T[x].add += num ; T[x].mx += num ; return ; } PD(x) ; int mid = T[x].l + T[x].r >> 1 ; if(l > mid){ update(l , r , RR(x) , num) ; } else if(r <= mid){ update(l , r , LL(x) , num) ; } else { update(l , mid , LL(x) , num) ; update(mid + 1 , r , RR(x) , num ) ; } PU(x) ; } int query(int l , int r ,int x){ if(l > T[x].r || r < T[x].l)return -inf ; if(l == T[x].l && r == T[x].r){ return T[x].mx ; } PD(x) ; int mid = T[x].l + T[x].r >> 1 ; if(l > mid){ return query(l , r , RR(x)) ; } else if(r <= mid){ return query(l , r , LL(x)) ; } else { return max(query(l , mid , LL(x)) , query(mid + 1 , r , RR(x))) ; } } void input(){ RD(n) ; RD(m) ; RD(k) ; for (int i = 1 ; i <= n ; i ++ )RD(a[i]) ; for (int i = 1 ; i <= n ; i ++ ){ sum[i] = sum[i - 1] + a[i] ; } int nn = 0 ; for (int i = k ; i <= n ; i ++ ){ ss[++ nn] = sum[i] - sum[i - k] ; } } void solve(){ input() ; build(1 , n - k + 1 , 1) ; while(m -- ){ int x , y , z ; RD(x) ;RD(y) ;RD(z) ; if(x == 0){ int spn = z - a[y] ; a[y] = z ; int left = max(y - k + 1 , 1) ; int right = min(y , n - k + 1) ; update(left , right , 1 , spn) ; } if(x == 1){ int spn = a[z] - a[y] ; int left = max(y - k + 1 , 1) ; int right = min(n - k + 1 , y) ; update(left , right , 1 , spn) ; spn = a[y] - a[z] ; left = max(z - k + 1 , 1) ; right = min(n - k + 1 , z) ; update(left , right , 1 , spn) ; swap(a[z] , a[y]) ; } if(x == 2){ int fk = query(y , z - k + 1 , 1) ; if(fk < 0){ putchar('-') ; fk *= -1 ; } OT(fk) ; putchar('\n') ; } } } int main() { int _ ;cin >> _ ;while(_ -- )solve() ; return 0 ; }
(不断更新)