Max Sub-matrix

Max Sub-matrix

教练找的题目,目前样列过了

题意:找子矩阵的最大周长

思路:先离散每列,再枚举列(n*n),在当前枚举的两列之间求每行的和(n*n*n),但是开两个数组,一个包含两列上的元素  一个不包含,这样可以处理出前i行当前这两列上的元素和。  当前两列中每行元素和知道   两列上前i项和知道就可以找出最大值

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <list>
  5 #include <map>
  6 #include <stack>
  7 #include <vector>
  8 #include <cstring>
  9 #include <sstream>
 10 #include <string>
 11 #include <cmath>
 12 #include <queue>
 13 #include <stdlib.h>
 14 #include <conio.h>
 15 #include <bits/stdc++.h>
 16 using namespace std;
 17 #define clc(a,b) memset(a,b,sizeof(a))
 18 #define inf 0x3f3f3f3f
 19 const int N=100010;
 20 const int MOD = 1e9+7;
 21 #define LL long long
 22 void fre() {
 23     freopen("in.txt","r",stdin);
 24 }
 25 
 26 inline int r() {
 27     int x=0,f=1;char ch=getchar();
 28     while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
 29     while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
 30 }
 31 int T,R ,s;
 32 
 33 struct node{
 34     int x,y,w;
 35     node(){}
 36     node(int xx,int yy,int ww):x(xx),y(yy),w(ww){}
 37     bool operator <(const node &a)const{
 38         return x<a.x;
 39     }
 40 }p[110];
 41 
 42 int c;
 43 LL sum;
 44 int col[110];
 45 LL l[110],row[110],row1[110];
 46 void init(){
 47     R=r(),s=r();
 48     clc(col,0);
 49     clc(l,0);
 50     clc(row,0);
 51     clc(row1,0);
 52     c=0;
 53     sum=0;
 54     for(int i=0;i<R;i++){
 55         for(int j=0;j<s;j++){
 56             int x;
 57             x=r();
 58             if(x){
 59                 sum+=x;
 60                 p[c]=node(i,j,x);
 61                 col[c++]=j;
 62             }
 63         }
 64     }
 65 }
 66 
 67 LL work(){
 68     sort(p,p+c);
 69     sort(col,col+c);
 70     int n=unique(col,col+c)-col;
 71     if(n<=2) return sum;
 72     LL ans=0;
 73     for(int i=0;i<n;i++){
 74         for(int j=i+1;j<n;j++){
 75             int cmin=col[i],cmax=col[j];
 76             int cnt=0;
 77             for(int k=0;k<c;k++){
 78                 if(k==0||p[k].x!=p[k-1].x){
 79                     cnt++;
 80                     row[cnt]=row1[cnt]=0;
 81                     l[cnt] = (cnt==1)?0:l[cnt-1]+row1[cnt-1]-row[cnt-1];
 82                 }
 83                 int cnow=p[k].y;
 84                 int cost=p[k].w;
 85                 if(cnow>cmin&&cnow<cmax) row[cnt]+=cost;
 86                 if(cnow>=cmin&&cnow<=cmax) row1[cnt]+=cost;
 87             }
 88             // for(int i=1;i<=cnt;i++){
 89             //     printf("%d\n",l[i]);
 90             // }
 91             // cout<<endl;
 92             // getch();
 93             if(cnt<=2) return sum;
 94             LL maxn=0;
 95             for(int k=1;k<=cnt;k++){
 96                 ans=max(ans,l[k]+row1[k]+maxn);
 97                 maxn=max(maxn,row[k]-l[k]);
 98             }
 99         }
100     }
101     return ans;
102 }
103 
104 int main(){
105     // fre();
106     T=r();
107     while(T--){
108         init();
109         LL ans=work();
110         printf("%lld\n",ans);
111     }
112     return 0;
113 }

 

转载于:https://www.cnblogs.com/ITUPC/p/5574571.html

import pandas as pd import numpy as np # 计算用户对歌曲的播放比例 triplet_dataset_sub_song_merged_sum_df = triplet_dataset_sub_song_mergedpd[['user', 'listen_count']].groupby('user').sum().reset_index() triplet_dataset_sub_song_merged_sum_df.rename(columns={'listen_count': 'total_listen_count'}, inplace=True) triplet_dataset_sub_song_merged = pd.merge(triplet_dataset_sub_song_mergedpd, triplet_dataset_sub_song_merged_sum_df) triplet_dataset_sub_song_mergedpd['fractional_play_count'] = triplet_dataset_sub_song_mergedpd['listen_count'] / triplet_dataset_sub_song_merged['total_listen_count'] # 将用户和歌曲编码为数字 small_set = triplet_dataset_sub_song_mergedpd user_codes = small_set.user.drop_duplicates().reset_index() song_codes = small_set.song.drop_duplicates().reset_index() user_codes.rename(columns={'index': 'user_index'}, inplace=True) song_codes.rename(columns={'index': 'song_index'}, inplace=True) song_codes['so_index_value'] = list(song_codes.index) user_codes['us_index_value'] = list(user_codes.index) small_set = pd.merge(small_set, song_codes, how='left') small_set = pd.merge(small_set, user_codes, how='left') # 将数据转换为稀疏矩阵形式 from scipy.sparse import coo_matrix mat_candidate = small_set[['us_index_value', 'so_index_value', 'fractional_play_count']] data_array = mat_candidate.fractional_play_count.values row_array = mat_candidate.us_index_value.values col_array = mat_candidate.so_index_value.values data_sparse = coo_matrix((data_array, (row_array, col_array)), dtype=float) # 使用SVD方法进行矩阵分解并进行推荐 from scipy.sparse import csc_matrix from scipy.sparse.linalg import svds import math as mt def compute_svd(urm, K): U, s, Vt = svds(urm, K) dim = (len(s), len(s)) S = np.zeros(dim, dtype=np.float32) for i in range(0, len(s)): S[i, i] = mt.sqrt(s[i]) U = csc_matrix(U, dtype=np.float32) S = csc_matrix(S, dtype=np.float32) Vt = csc_matrix(Vt, dtype=np.float32) return U, S, Vt def compute_estimated_matrix(urm, U, S, Vt, uTest, K, test): rightTerm = S * Vt max_recommendation = 250 estimatedRatings = np.zeros(shape=(MAX_UID, MAX_PID), dtype=np.float16) recomendRatings = np.zeros(shape=(MAX_UID, max_recommendation), dtype=np.float16) for userTest in uTest: prod = U[userTest, :] * rightTerm estimatedRatings[userTest, :] = prod.todense() recomendRatings[userTest, :] = (-estimatedRatings[userTest, :]).argsort()[:max_recommendation] return recomendRatings K = 50 urm = data_sparse MAX_PID = urm.shape[1] MAX_UID = urm.shape[0] U, S, Vt = compute_svd(urm, K) uTest = [4, 5, 6, 7, 8, 73, 23] # uTest=[1b5bb32767963cbc215d27a24fef1aa01e933025] uTest_recommended_items = compute_estimated_matrix(urm, U, S, Vt 继续将这段代码输出完整
最新发布
05-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值