TC 667 #DIV2

250 points:

ou are given two distinct points A and B in the two-dimensional plane. Your task is to find any point C with the following properties:

  • C is different from A and B.
  • Each coordinate of C is an integer between -100 and 100, inclusive.
  • The distance between A and C is strictly greater than the distance between B and C.

You are given four ints: x1, y1, x2, andy2. Point A has coordinates (x1,y1) and point B has coordinates (x2,y2). Find the coordinates (x3,y3) of one possible point C with the above properties. Return these coordinates as a vector <int> with two elements: element 0 is x3 and element 1 is y3. In other words, return the vector <int> {x3,y3}.

For the constraints given below it is guaranteed that a valid point C always exists. If there are multiple solutions, return any of them

分析:直接暴力找到一個合法解

#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;


class PointDistance {
public:
   vector <int> findPoint( int x1, int y1, int x2, int y2 ) {
       vector<int>ans;
       for(int i=-100;i<=100;i++)
       {
           for(int j=-100;j<=100;j++)
           {
               double d1=sqrt((x1-i)*(x1-i)+(y1-j)*(y1-j));
               double d2=sqrt((x2-i)*(x2-i)+(y2-j)*(y2-j));
               if(d1>d2)
               {
                   ans.push_back(i);
                   ans.push_back(j);
                   return ans;
               }
           }
       }
   }
};



// Powered by FileEdit
// Powered by moj 4.12 [modified TZTester]
// Powered by CodeProcessor

500 points:

Cat Noku has just finished writing his first computer program. Noku's computer has m memory cells. The cells have addresses 0 through m-1. Noku's program consists of n instructions. The instructions have mutually independent effects and therefore they may be executed in any order. The instructions must be executed sequentially (i.e., one after another) and each instruction must be executed exactly once.

You are given a description of the n instructions as a vector <string> with n elements. Each instruction is a string of m characters. For each i, character i of an instruction is '1' if this instruction accesses memory cell i, or '0' if it does not.

Noku's computer uses caching, which influences the time needed to execute an instruction. More precisely, executing an instruction takes k^2 units of time, where k is the number ofnew memory cells this instruction accesses. (I.e., k is the number of memory cells that are accessed by this instruction but have not been accessed by any previously executed instruction. Note that k may be zero, in which case the current instruction is indeed executed in 0 units of time.)

Noku's instructions can be executed in many different orders. Clearly, different orders may lead to a different total time of execution. Find and return the shortest amount of time in which it is possible to execute all instructions

分析:N<=20,這個要狀壓一下,令dp[s]爲安排指令狀態爲s的最小代價,f[st]爲當前內存單元佔用情況

轉移就行了,複雜度爲O((1<<N)*n*m)

#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
int dp[(1<<20)+100];
int f[(1<<20)+100];
class OrderOfOperationsDiv2 {
public:
   int minTime( vector <string> s ) {
       CLEAR(dp,INF);
       CLEAR(f,0);
       int m=s[0].size();
       int n=s.size();
       int status=(1<<n);
       dp[0]=0;
       for(int i=1;i<status;i++)
       {
           for(int j=0;j<n;j++)
           {
               if((i&(1<<j))==0) continue;
               int st=i^(1<<j);
               int c=0;
               for(int k=0;k<m;k++)
               {
                   if(s[j][k]=='1')
                   {
                       if((f[st]&(1<<k))==0)
                          c++;
                       f[i]|=(1<<k);
                   }
               }
               dp[i]=min(dp[i],dp[st]+c*c);
           }
       }
       return dp[status-1];
   }
};



// Powered by FileEdit
// Powered by moj 4.12 [modified TZTester]
// Powered by CodeProcessor


1000 points:

Carol is starting a new taco shop business. She is going to open some taco shops in a block of buildings. The blocks consists ofn adjacent buildings in a row. Each building has exactlym floors. The buildings are numbered 0 through n-1 in order.

Carol can open between 0 and m taco shops in each building (as there can be at most one taco shop per floor in each building). For each taco shop, the profit P[x][y] will depend on two factors:

  • the number x of the building that contains this taco shop
  • the total count y of taco shops in that particular building and in buildings adjacent to that building (including this particular taco store)

You are given the ints n and m. You are also given the profits as defined above, encoded into a vector <int>c. For each x between 0 andn-1, and for each y between 1 and 3m, the profit P[x][y] is given inc[x*3*m+y-1].

It is guaranteed that the profits don't increase as y increases. That is, for each valid x and y, P[x][y] will be greater than or equal to P[x][y+1]. Note that the profit is for a single store. For example, if there are three taco stores in building 7 and no other stores in buildings 6 and 8, each of these three taco stores will bring the profit P[7][3].

Determine and return the maximum total profit that Carol can gain from opening the taco shops.

分析:DP,很明顯當前的代價和左右兩邊的shop個數有關

令dp[i][j][k]:前i個建築,當前建築有j個shop,下一個建築有k個shop的方案的

轉移下就行了

#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
int dp[35][35][35];
int mp[35][110];
class ShopPositions {
public:
   int maxProfit( int n, int m, vector <int> c ) {
       int sz=c.size(),k=0;
       for(int i=1;i<=sz/(3*m);i++)
          for(int j=1;j<=3*m;j++)  mp[i][j]=c[k++];
       CLEAR(dp,0);
       for(int i=0;i<=m;i++)
          for(int j=0;j<=m;j++)  dp[1][i][j]=mp[1][i+j]*i;
       for(int i=2;i<=n;i++)
       {
           for(int j=0;j<=m;j++)
           {
               for(int k=0;k<=m;k++)
               {
                   for(int l=0;l<=m;l++)
                      dp[i][j][l]=max(dp[i][j][l],dp[i-1][k][j]+mp[i][j+k+l]*j);
               }
           }
       }
       int ans=0;
       for(int i=0;i<=m;i++)
          ans=max(ans,dp[n][i][0]);
       return ans;
   }
};



// Powered by FileEdit
// Powered by moj 4.12 [modified TZTester]
// Powered by CodeProcessor


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章