题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288
解题思路:http://blog.csdn.net/piaocoder/article/details/47621397
#include <iostream> #include <cstdio> #include <cstring> #define MOD 1000000007 using namespace std; typedef long long ll; const int N = 100005; int n; ll l[N],r[N]; int pre[N],last[N]; int a[N]; int main(){ while(~scanf("%d",&n)){ for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); l[i] = 1;r[i] = n; } memset(pre,0,sizeof(pre)); memset(last,0,sizeof(last)); for(int i = 1; i <= n; i++){ for(int j = a[i]; j <= 10000; j += a[i]) if(pre[j] != 0 && r[pre[j]] == n)//如果已经出现并且在右边最近的因子还没有找到 r[pre[j]] = i-1; pre[a[i]] = i; } for(int i = n; i > 0; i--){ for(int j = a[i]; j <= 10000; j += a[i]) if(last[j] != 0 && l[last[j]] == 1)//如果已经出现并且在左边最近的因子还没有找到 l[last[j]] = i+1; last[a[i]] = i; } /* for(int i=1;i<=n;i++) printf("%d %d %d\n",i,l[i],r[i]); */ ll ans = 0; for(int i = 1; i <= n; i++) ans = (ans+(ll)(i-l[i]+1)*(ll)(r[i]-i+1)%MOD)%MOD; printf("%lld\n",ans); } return 0; }
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289
解题思路:http://blog.csdn.net/piaocoder/article/details/47624009
#include <iostream> #include <cstdio> #include <set> using namespace std; typedef long long ll; multiset<int> minn; multiset<int,greater<int> > maxn; int a[100005]; int main(){ int T; scanf("%d",&T); while(T--){ int n,k; scanf("%d%d",&n,&k); maxn.clear(); minn.clear(); ll ans = 0; int i,j = 0; multiset<int>::iterator it1,it2; for(i = 0; i < n; i++){ scanf("%d",&a[i]); maxn.insert(a[i]); minn.insert(a[i]); while(j <= i && *maxn.begin()-*minn.begin() >= k){ //ans += (i-j); it1 = maxn.find(a[j]); it2 = minn.find(a[j]);//注意:一开始没加上这两句,一直wrong,只能说自己对multiset用的还不是很熟。。。 maxn.erase(it1); minn.erase(it2); j++; } ans += maxn.size(); //cout<<ans<<endl; } /* while(j < n){ ans += (i-j); j++; } */ printf("%lld\n",ans); } return 0; }
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294
解题思路:http://blog.csdn.net/piaocoder/article/details/47656811
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> #define INF 0xfffffff using namespace std; const int MAX_V = 2005; int n,m; //用于表示边的结构体(终点,容量,反向边) struct edge{ int to,cap,rev; }; struct node{ int x,l; }; vector<node> ve[MAX_V]; int dis[MAX_V]; int minv[MAX_V]; vector<edge> G[MAX_V];//图中邻接表表示 bool used[MAX_V];//DFS中用到的访问标记 //向图中增加一条从s到t容量为cap的边 void add_edge(int from,int to,int cap){ G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size()-1}); } void build(){ for(int i = 1; i <= n; i++){ int l = ve[i].size(); for(int j = 0; j < l; j++){ int v = ve[i][j].x,w = ve[i][j].l; if(dis[v] - dis[i] == w){ add_edge(i,v,1); } } } } //通过DFS寻到增广路 int dfs(int v,int t,int f){ if(v == t) return f; used[v] = true; for(int i = 0; i < G[v].size(); i++){ edge &e = G[v][i]; if(!used[e.to] && e.cap > 0){ int d = dfs(e.to,t,min(f,e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } //求解从s到t的最大流 int max_flow(int s,int t){ int flow = 0; while(1){ memset(used,false,sizeof(used)); int f = dfs(s,t,INF); if(f == 0) return flow; flow += f; } } void SPFA(int st){ queue<int> q; int i; memset(used,false,sizeof(used)); memset(minv,0,sizeof(minv)); for(i = 1; i <= n; i++) dis[i] = INF; dis[st] = 0; q.push(st); while(!q.empty()){ int cur = q.front(); q.pop(); used[cur] = 1; int l = ve[cur].size(); for(i = 0; i < l; i++){ int tmp = ve[cur][i].x; if(dis[tmp] == dis[cur] + ve[cur][i].l){ minv[tmp] = min(minv[tmp],minv[cur]+1); if(!used[tmp]){ q.push(tmp); used[tmp] = 1; } } if(dis[tmp] > dis[cur] + ve[cur][i].l){ minv[tmp] = minv[cur]+1; dis[tmp] = dis[cur] + ve[cur][i].l; if(!used[tmp]){ q.push(tmp); used[tmp] = 1; } } } used[cur] = 0; } } int main(){ while(~scanf("%d%d",&n,&m)){ int u,v,w; for(int i = 1; i <= n; i++){ G[i].clear(); ve[i].clear(); } for(int i = 0; i < m; i++){ scanf("%d%d%d",&u,&v,&w); ve[u].push_back((node){v,w}); ve[v].push_back((node){u,w}); } SPFA(1); build(); int ans = max_flow(1,n); printf("%d %d\n",ans,m-minv[n]); } return 0; }
有待更新。。。