#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int n,s,a,b; int ans=-1; cin>>n>>s; s=s*100; bool flag=false; for(int i=0;i<n;i++) { cin>>a>>b; int ts=s; if(ts>=a*100+b) { flag=true; ans=max(ans,0); if(b!=0) ans=max(ans,100-b); } } cout<<ans<<endl; return 0; }
1 second
256 megabytes
standard input
standard output
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in
this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has
height hi. The
goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k)
to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if
this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides
a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal
of the game?
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing
the heights of the pylons.
Print a single number representing the minimum number of dollars paid by Caisa.
5 3 4 3 2 4
4
3 4 4 4
4
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units.
Then he can safely pass to the last pylon.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n,h[100100]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",h+i); int ans=h[1]; int energy=0; for(int i=2;i<=n;i++) { int deta=h[i-1]-h[i]; if(deta>=0) { energy+=deta; } else { deta=-deta; if(energy>=deta) { energy-=deta; } else { int ca=deta-energy; ans+=ca; energy=0; } } } printf("%d\n",ans); return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long int LL; int n; LL a[2200][2200]; LL xie1[6000],xie2[6000]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%I64d",&a[i][j]); xie1[i+j]+=a[i][j]; xie2[i-j+3000]+=a[i][j]; } } LL ans=0;int x1=1,y1=1,x2=1,y2=2; LL temp=-1; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int id=j+i; if(id%2==1) { if(temp<=xie1[i+j]+xie2[i-j+3000]-a[i][j]) { temp=xie1[i+j]+xie2[i-j+3000]-a[i][j]; x1=i;y1=j; } } } } ans+=temp; temp=-1; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int id=j+i; if(id%2==0) { if(temp<=xie1[i+j]+xie2[i-j+3000]-a[i][j]) { temp=xie1[i+j]+xie2[i-j+3000]-a[i][j]; x2=i;y2=j; } } } } ans+=temp; printf("%I64d\n %d %d %d %d\n",ans,x1,y1,x2,y2); return 0; }
预处理+简单DP
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int N,K; int a[11][1100],be[11][1100][1100],before[1100][1100]; int dp[1100]; int main() { scanf("%d%d",&N,&K); for(int i=1;i<=K;i++) for(int j=1;j<=N;j++) scanf("%d",&a[i][j]); for(int i=1;i<=K;i++) { for(int j=1;j<=N;j++) { for(int k=1;k<j;k++) { be[i][a[i][k]][a[i][j]]=1; } } } for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { bool flag=true; for(int k=1;k<=K;k++) { if(be[k][i][j]==0) { flag=false; break; } } if(flag==true) before[i][j]=1; } } int ans=1; for(int i=1;i<=N;i++) { int c1=a[1][i]; dp[c1]=1; for(int j=1;j<i;j++) { int c2=a[1][j]; if(before[c2][c1]) { dp[c1]=max(dp[c1],dp[c2]+1); } } ans=max(dp[c1],ans); } printf("%d\n",ans); return 0; } /* 4 3 1 4 2 3 4 1 2 3 1 2 4 3 */
纯暴力,应为修改次数比较少,DFS预处理出所有的答案,每次有点跟新时就再预处理一次
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <stack> #include <queue> using namespace std; const int maxn=1001000; int n,q; int a[maxn/10]; vector<int> yushu[maxn/10]; void fenjie(int id,int x) { yushu[id].clear(); for(int i=2;i*i<=x;i++) { if(x%i==0) { yushu[id].push_back(i); while(x%i==0) x/=i; } } if(x!=1) yushu[id].push_back(x); } bool vis[2000100]; int prime[2000100],pr=1; void get_prime() { int t=1; bool flag=true; for(int i=2;i<=2000100;i+=t) { if(vis[i]==false) { prime[i]=pr++; for(int j=2;j*i<=2000100;j++) vis[j*i]=true; } if(flag==true) flag=false; else t=2; } } struct Edge { int to,next; }edge[200200]; int Adj[100100],Size=0; void Add_Edge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++; } void init() { memset(Adj,-1,sizeof(Adj)); Size=0; } stack<int> stk[150000]; int ANS[100100]; int dist[100100]; void BFS() { memset(vis,false,sizeof(vis)); queue<int> q; q.push(1); vis[1]=true; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(vis[v]==true) continue; vis[v]=true; dist[v]=dist[u]+1; q.push(v); } } } void DFS(int u,int fa) { for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(v==fa) continue; int temp=-1,maxd=-(1<<30); for(int j=0,sz=yushu[v].size();j<sz;j++) { if(stk[prime[yushu[v][j]]].size()>0) { int k=stk[prime[yushu[v][j]]].top(); if(dist[k]>maxd) { maxd=dist[k]; temp=k; } } stk[prime[yushu[v][j]]].push(v); } ANS[v]=temp; DFS(v,u); for(int j=0,sz=yushu[v].size();j<sz;j++) { stk[prime[yushu[v][j]]].pop(); } } } int main() { get_prime(); init(); scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) { scanf("%d",a+i); fenjie(i,a[i]); } for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); Add_Edge(u,v); Add_Edge(v,u); } Add_Edge(0,1); BFS(); DFS(0,0); while(q--) { int k,a,b; scanf("%d",&k); if(k==1) { scanf("%d",&a); printf("%d\n",ANS[a]); } else if(k==2) { scanf("%d%d",&a,&b); fenjie(a,b); DFS(0,0); } } return 0; }